Add PEP 3118 (new) buffer support to Pygame surfaces

Issue #173 resolved
Lenard Lindstrom
created an issue

This issue documents the reasons to implement a PEP 3118 (new) buffer interface for pygame.Surface pixel access.

PEP 3118 was finalized in Python 3.3, making it a stable protocol for exchanging array buffers. Most important, the Py_buffer stays unchanged. Pygame PEP 3118 buffer code developed for Python 3.3 can be backported to earlier Python releases without modification.

The Py_buffer struct is a convenient array representation, useful in passing array information internally within Pygame.

The PEP 3118 protocol includes a release mechanism, a bf_releasebuffer callback invoked by PyBuffer_Release. A buffer consumer is required to call PyBuffer_Release when finished with a buffer view. This provides a rebust way to release a buffer related surface lock. Before this, the only way to unlock a surface was within a deallocation handler, such as the del Python method or a tp_dealloc slot function. Prompt garbage collection via reference counting is no longer ensured. Using Python contexts and close method calls are not the prefered way to release resources. It is expected that a buffer consumers will have a way to explicitly release a buffer from Python code.

As of writing this issue, 3118 support was added to Python 1.9.2 alpha, and is undergoing testing.