[pypy-svn] r36002 - in pypy/dist/pypy/rlib/rctypes: . test
arigo at codespeak.net
arigo at codespeak.net
Wed Dec 27 19:40:00 CET 2006
Author: arigo
Date: Wed Dec 27 19:39:58 2006
New Revision: 36002
Added:
pypy/dist/pypy/rlib/rctypes/rstruct.py (contents, props changed)
pypy/dist/pypy/rlib/rctypes/test/__init__.py (contents, props changed)
Modified:
pypy/dist/pypy/rlib/rctypes/implementation.py
pypy/dist/pypy/rlib/rctypes/rarray.py
pypy/dist/pypy/rlib/rctypes/test/test_rprimitive.py
Log:
Start some ctypes.Structure support.
Modified: pypy/dist/pypy/rlib/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/implementation.py (original)
+++ pypy/dist/pypy/rlib/rctypes/implementation.py Wed Dec 27 19:39:58 2006
@@ -149,3 +149,4 @@
import pypy.rlib.rctypes.rprimitive
import pypy.rlib.rctypes.rarray
import pypy.rlib.rctypes.rpointer
+import pypy.rlib.rctypes.rstruct
Modified: pypy/dist/pypy/rlib/rctypes/rarray.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/rarray.py (original)
+++ pypy/dist/pypy/rlib/rctypes/rarray.py Wed Dec 27 19:39:58 2006
@@ -18,15 +18,15 @@
self.itemcontroller.knowntype,
self.length)
- def new(self, *args_s):
- if len(args_s) > self.length:
- raise ValueError("too many arguments for an array of length %d" % (
- self.length,))
+ def new(self, *args):
obj = self.knowntype.allocate()
- if len(args_s) > 0:
- lst_s = list(args_s)
- for i in range(len(args_s)):
- self.setitem(obj, i, lst_s[i])
+ if args:
+ if len(args) > self.length:
+ raise ValueError("too many arguments for an array of "
+ "length %d" % (self.length,))
+ lst = list(args)
+ for i in range(len(args)):
+ self.setitem(obj, i, lst[i])
return obj
new._annspecialcase_ = 'specialize:arg(0)'
Added: pypy/dist/pypy/rlib/rctypes/rstruct.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rlib/rctypes/rstruct.py Wed Dec 27 19:39:58 2006
@@ -0,0 +1,54 @@
+from pypy.rlib.rctypes.implementation import CTypeController, getcontroller
+from pypy.rlib.rctypes import rctypesobject
+from pypy.rpython.lltypesystem import lltype
+from pypy.rlib.unroll import unrolling_iterable
+
+from ctypes import Structure
+
+StructType = type(Structure)
+
+
+class StructCTypeController(CTypeController):
+
+ def __init__(self, ctype):
+ CTypeController.__init__(self, ctype)
+
+ # Map the field names to their controllers
+ controllers = []
+ fields = []
+ for name, field_ctype in ctype._fields_:
+ controller = getcontroller(field_ctype)
+ controllers.append((name, controller))
+ fields.append((name, controller.knowntype))
+ self.fieldcontrollers = dict(controllers)
+ external = getattr(ctype, '_external_', False)
+ self.knowntype = rctypesobject.RStruct(ctype.__name__, fields,
+ c_external = external)
+
+ # Build a custom new() method where the setting of the fields
+ # is unrolled
+ unrolled_controllers = unrolling_iterable(controllers)
+
+ def new(*args):
+ obj = self.knowntype.allocate()
+ if len(args) > len(fields):
+ raise ValueError("too many arguments for this structure")
+ for name, controller in unrolled_controllers:
+ if args:
+ value = args[0]
+ args = args[1:]
+ itemobj = getattr(obj, 'ref_' + name)()
+ controller.set_value(itemobj, value)
+ return obj
+
+ self.new = new
+
+
+ def getattr(self, obj, attr):
+ controller = self.fieldcontrollers[attr]
+ itemobj = getattr(obj, 'ref_' + attr)()
+ return controller.return_value(itemobj)
+ getattr._annspecialcase_ = 'specialize:arg(2)'
+
+
+StructCTypeController.register_for_metatype(StructType)
Added: pypy/dist/pypy/rlib/rctypes/test/__init__.py
==============================================================================
Modified: pypy/dist/pypy/rlib/rctypes/test/test_rprimitive.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/test/test_rprimitive.py (original)
+++ pypy/dist/pypy/rlib/rctypes/test/test_rprimitive.py Wed Dec 27 19:39:58 2006
@@ -158,7 +158,6 @@
t.view()
def test_annotate_primitive_structfield(self):
- py.test.skip("in-progress")
class S(Structure):
_fields_ = [('cs', c_short)]
def func(x):
More information about the Pypy-commit
mailing list