[pypy-issue] [issue1498] Problems subclassing from a defined ctypes Structure

Nicolas Lelong tracker at bugs.pypy.org
Sat May 25 17:58:23 CEST 2013


New submission from Nicolas Lelong <rotoglup at gmail.com>:

We currently try to run some of our code which relies heavily on ctypes.

We define our C structures the usual way, and then 'augment' them by subclassing 
the C definition. Something along the 
lines of :

import ctypes

class cStruct( ctypes.Structure ):  
  _fields_ = [
    ( "x", ctypes.c_float )
  , ( "y", ctypes.c_float )
  ]

class pyStruct( cStruct ):
  def __init__(self, *args):
    super(pyStruct, self).__init__(*args)

Pypy behaviour is different from CPython. The following code runs fine with 
CPython :

assert cStruct._fields_ == pyStruct._fields_
a = cStruct()
b = pyStruct()
print "a._fields_ =", a._fields_
print "b._fields_ =", b._fields_
b = pyStruct(1.0, 2.0)

And gives the following output :

a._fields_ = [('x', <class 'ctypes.c_float'>), ('y', <class 'ctypes.c_float'>)]
b._fields_ = [('x', <class 'ctypes.c_float'>), ('y', <class 'ctypes.c_float'>)]

With pypy, it seems that _fields_ is empty for 'b' ; see below. Is this to be 
expected ?

Thanks

==

This gives the following output / exception :

a._fields_ = [('x', <class 'ctypes.c_float'>), ('y', <class 'ctypes.c_float'>)]
b._fields_ = []
Traceback (most recent call last):
  File "app_main.py", line 72, in run_toplevel
  File "D:\rotoglup-scratchpad\experiments\pypy_ctypes\test_struct_inherit.py", 
line 18, in <module>
    b = pyStruct(1.0, 2.0)
  File "D:\rotoglup-scratchpad\experiments\pypy_ctypes\test_struct_inherit.py", 
line 11, in __init__
    super(pyStruct, self).__init__(*args)
  File "D:\pypy\pypy-2.0.2\lib_pypy\_ctypes\structure.py", line 232, in __init__
    self.__setattr__(name, arg)
  File "D:\pypy\pypy-2.0.2\lib_pypy\_ctypes\structure.py", line 116, in __set__
    obj._buffer.__setattr__(self.name, arg)
AttributeError: C Structure has no attribute x

pypy-2.0-beta1.1 failed with another exception :

a._fields_ = [('x', <class 'ctypes.c_float'>), ('y', <class 'ctypes.c_float'>)]
b._fields_ = []
Traceback (most recent call last):
  File "app_main.py", line 52, in run_toplevel
  File "D:\rotoglup-scratchpad\experiments\pypy_ctypes\test_struct_inherit.py", 
line 18, in <module>
    b = pyStruct(1.0, 2.0)
  File "D:\rotoglup-scratchpad\experiments\pypy_ctypes\test_struct_inherit.py", 
line 11, in __init__
    super(pyStruct, self).__init__(*args)
  File "D:\pypy\pypy-2.0-beta1.1-win32\pypy-2.0-
beta1\lib_pypy\_ctypes\structure.py", line 228, in __init__
    raise TypeError("too many initializers")
TypeError: too many initializers

----------
messages: 5762
nosy: pypy-issue, rotoglup
priority: bug
status: unread
title: Problems subclassing from a defined ctypes Structure

________________________________________
PyPy bug tracker <tracker at bugs.pypy.org>
<https://bugs.pypy.org/issue1498>
________________________________________


More information about the pypy-issue mailing list