[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Porting to Python 3.0 - the python3 branch (Was: pygame for portable python)



Hi everyone,

An update -- The surface, gfxdraw, display, key, mouse, image and imageext modules now compile with Python 3.1 on linux. All surface_test.py unit tests pass except for the get_buffer test since it has been disabled for now. The aacircle.py example runs. So that leaves these modules to do before the deadline:

sprite   - sprite.py
time   - time.c
transform   - transform.c
mixer   - mixer.c
mixer.music   - music.c
font   - font.c

Lenard.


Lenard Lindstrom wrote:

Hi,

I am proposing a game plan for getting a basic Pygame for Python 3.0. First the goal could be to get the chimp.py and aliens.py examples working. So the following modules are needed:

pygame   - __init__.py (done)
sprite   - sprite.py
image   - image.c, imageext.c
locals   - locals.py
display   - display.c
event   - event.c
mouse   - mouse.c
key   - key.c
time   - time.c
transform   - transform.c
mixer   - mixer.c [*]
mixer.music   - music.c
font   - font.c
base   - base.c (done)
surface   - surface.c (partially done [*])
surflock   - surflock.c (done)
rect   - rect.c (done)
color   - color.c (done), colordict.py (done)
constants   - constants.c (done)
rwobject   - rwobject.c

I suggest altering the branch's Setup.in and __init__.py to comment out those parts not yet updated. This way anyone can check out a copy and it will build for Python 3, though incompletely. As modules are updated, the corresponding unit tests should also be updated. So far rect_test.py and color_test.py pass when run as standalone programs with Python 3.0. Others, like surface_test.py, need supporting modules, like display, working first. Modules passing their unit tests can be considered for merging back into the trunk.

Here are some things to consider when modifying an extension module. These differ: the init function, PyTypeObject, PyNumberMethods and object structures. All Python 3 text strings are unicode. The PyString_ api is renamed to PyBytes_ and is for binary data or encoded text only. All Python 3 integer are Python long. The PyInt_ functions have been completely removed. Python 3 provides a mechanism for keeping module state on a per-instance bases. This means global C variables should be avoided when possible. This is not alway practical though (see base.c). Coercing and comparison are gone. Replace with rich comparison. There will undoubtedly be other incompatibilities.

For .py modules the 'L' integer suffix is no longer valid. The 'u' string prefix is invalid, so remove. Conversion from text to byte data requires explicit encoding and decoding. The print command is replaced with a function. The raise and except commands have changed. Calls to the map function should be replaced with list comprehensions. Some functions and methods which once returned lists, such as range and dict.keys, now return iterators. Regular division always returns a float. Replace with floor division '//' for integers. The list goes on. However, for most Pygame .py modules the changes needed are straight forward.

To facilitate the changes the pygame.compat.py and pgcompat.h (adapted from pgreloaded) are available. See color.c for an example of handling the init function, extension type declaration, Python integers and Python strings. __init__.py shows modified prints and excepts. color_test.py has examples of using compat.long_ to remove 'L' integer suffixes.

For the moment I cannot do any further Python 3 building or testing. But I will continue to check that the Python 3 branch is backward compatible with Python 2.5.

Lenard


[*] For now bufferproxy will be disabled for Python 3.0. This means no surfarray and sndarray support for numpy. The problem is the buffer interface differs between Python 2.x and 3. I don't know by how much exactly, maybe not enough to be a concern. If the differences are significant one solution is to replace the buffer interface with the array interface. The advantage is the array interface should be the same for Python 2.x and 3. One disadvantage is the array interface may be phased out over time in favor of the new buffer interface. An advantage of the new buffer interface is it provides explicit releasing of a buffer object. This means a surface can be unlocked with a callback rather than a bufferproxy's deallocator.


Andre Krause wrote:
(http://www.mail-archive.com/pygame-users@xxxxxxxx/msg11196.html)

i only need surfaces, image loading and mouse events. thats all you need for a simple baloon
pop and arkanoid game.