this time with a subject, the message, and my apologies for the noise, renaud On 12/1/10, renaud blanch <rndblnch@gmail.com> wrote:
hi,
i'm trying to make some pyopengl [0] -based code [1] run on top of pypy. this is partially successful, but i need some advice to progress further. pyopengl 3.x makes use of ctypes to provide the opengl binding, and it works out of the box for simple functions (those that do not takes c-pointer to buffers of data as arguments). for the rest, the first issue is that pyopengl use two functions from the ctypes.pythonapi lib, namely PyString_AsString and PyBuffer_FromMemory. any advice on how to replace those functions to make them compatible with pypy?
Mike Fletcher (pyopengl author) gave me some hints about that point:
For the first issue, those are going to require some reworking, in essence those are "C" implemented code that happens to use Python/ctypes as the implementation language and makes assumptions about the data-storage for the objects (e.g. that a string is internally a contiguous series of bytes, which is *not necessarily* true in PyPy). We'd need to find a mechanism in PyPy that would give us that direct memory-pointer access to be able to use it. Note: a compacting garbage collector (or anything else that can move memory locations) will cause problems there, so we may need to find a way to signal PyPy not to move a given object, and to use contiguous data-arrays for their storage.
thanks a lot for any advice,
renaud
0. PyOpenGL 3.x / The Python OpenGL® Binding <http://pyopengl.sourceforge.net/>
1. opengl-programmable / a short step by step tutorial to OpenGL programmable pipeline <http://bitbucket.org/rndblnch/opengl-programmable/>
Hi Renaud, On Wed, Dec 1, 2010 at 2:21 PM, renaud blanch <rndblnch@gmail.com> wrote:
We'd need to find a mechanism in PyPy that would give us that direct memory-pointer access to be able to use it. Note: a compacting garbage collector (or anything else that can move memory locations) will cause problems there, so we may need to find a way to signal PyPy not to move a given object, and to use contiguous data-arrays for their storage.
Indeed, this part from Mike Fletcher got the cause right, but the solution we recommend is different. It's kind of impossible in PyPy to mark objects as non-moving ("pin" them, as the garbage collection language goes). Instead you need to just use the official ctypes API, create_string_buffer(). It's equivalent, but requires one extra copy of the data. A bientôt, Armin.
Hi, 2010/12/1 renaud blanch <rndblnch@gmail.com>
i'm trying to make some pyopengl [0] -based code [1] run on top of pypy. this is partially successful, but i need some advice to progress further. pyopengl 3.x makes use of ctypes to provide the opengl binding, and it works out of the box for simple functions (those that do not takes c-pointer to buffers of data as arguments). for the rest, the first issue is that pyopengl use two functions from the ctypes.pythonapi lib, namely PyString_AsString and PyBuffer_FromMemory. any advice on how to replace those functions to make them compatible with pypy?
Mike Fletcher (pyopengl author) gave me some hints about that point:
For the first issue, those are going to require some reworking, in essence those are "C" implemented code that happens to use Python/ctypes as the implementation language and makes assumptions about the data-storage for the objects (e.g. that a string is internally a contiguous series of bytes, which is *not necessarily* true in PyPy). We'd need to find a mechanism in PyPy that would give us that direct memory-pointer access to be able to use it. Note: a compacting garbage collector (or anything else that can move memory locations) will cause problems there, so we may need to find a way to signal PyPy not to move a given object, and to use contiguous data-arrays for their storage.
Don't worry, with PyPy's ctypes you'll never get the address of a moving object. c_char_p makes a non-moving copy of the string. But IMO the call to pythonapi.PyString_AsString could be removed in OpenGL/arrays/strings.py: def dataPointer(value): return ctypes.cast(ctypes.c_char_p(value), ctypes.c_void_p).value I don't know about the other function, though. -- Amaury Forgeot d'Arc
hi, Amaury Forgeot d'Arc <amauryfa <at> gmail.com> writes:
But IMO the call to pythonapi.PyString_AsString could be removed in OpenGL/arrays/strings.py:
def dataPointer(value): return ctypes.cast(ctypes.c_char_p(value), ctypes.c_void_p).value
I don't know about the other function, though.
great, this replacement (and also the one suggested by Armin with create_string_buffer instead of c_char_p) seams to works (i.e., runs fine on cpython). this one is only in experimental code of pyopengl, so i won't bother much for now. the next issue is now that the array data types used by pyopengl are not compatible with pypy ctypes: they are lacking (at least) _CData_input as shown in the stack trace below. any advice on that one? should i try to fix PyOpenGL ArrayDatatype, or try to patch pypy's ctypes function.py to work around that one? thanks again, renaud % pypy 01-direct.py Traceback (most recent call last): File "app_main.py", line 33, in run_toplevel File "01-direct.py", line 268, in <module> sys.exit(main()) File "01-direct.py", line 262, in main init_texture() File "01-direct.py", line 48, in init_texture "".join(pixel(i, j, k) for i in range(width) File "/Users/Shared/src/pypy-1.4-osx/site-packages/OpenGL/latebind.py", line 45, in __call__ return self._finalCall( *args, **named ) File "/Users/Shared/src/pypy-1.4-osx/site-packages/OpenGL/wrapper.py", line 784, in wrapperCall result = self.wrappedOperation( *cArguments ) File "/Users/Shared/src/pypy-1.4-osx/site-packages/OpenGL/platform/baseplatform.py", line 335, in __call__ return self( *args, **named ) File "/Users/Shared/src/pypy-1.4-osx/lib_pypy/_ctypes/function.py", line 166, in __call__ argtypes, argsandobjs = self._wrap_args(argtypes, args) File "/Users/Shared/src/pypy-1.4-osx/lib_pypy/_ctypes/function.py", line 281, in _wrap_args wrapped = argtype._CData_input(arg) AttributeError: type object 'ArrayDatatype' has no attribute '_CData_input'
participants (4)
-
Amaury Forgeot d'Arc
-
Armin Rigo
-
renaud blanch
-
rndblnch