[pypy-svn] r50369 - in pypy/branch/applevel-ctypes2/pypy/module/_ffi: . test
cfbolz at codespeak.net
cfbolz at codespeak.net
Sun Jan 6 12:51:17 CET 2008
Author: cfbolz
Date: Sun Jan 6 12:51:16 2008
New Revision: 50369
Removed:
pypy/branch/applevel-ctypes2/pypy/module/_ffi/app_ffi.py
Modified:
pypy/branch/applevel-ctypes2/pypy/module/_ffi/__init__.py
pypy/branch/applevel-ctypes2/pypy/module/_ffi/array.py
pypy/branch/applevel-ctypes2/pypy/module/_ffi/test/test__ffi.py
Log:
refactor arrays in the same way as I did with structures: now Array lives at
interplevel and W_ArrayInstance has a reference to an W_Array which keeps all
the common information. So far there is not much common information.
Modified: pypy/branch/applevel-ctypes2/pypy/module/_ffi/__init__.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/module/_ffi/__init__.py (original)
+++ pypy/branch/applevel-ctypes2/pypy/module/_ffi/__init__.py Sun Jan 6 12:51:16 2008
@@ -14,6 +14,7 @@
'FuncPtr' : 'interp_ffi.W_FuncPtr',
'Structure' : 'structure.W_Structure',
'StructureInstance' : 'structure.W_StructureInstance',
+ 'Array' : 'array.W_Array',
'ArrayInstance' : 'array.W_ArrayInstance',
'_get_type' : 'interp_ffi._w_get_type',
'sizeof' : 'interp_ffi.sizeof',
@@ -22,5 +23,4 @@
}
appleveldefs = {
- 'Array' : 'app_ffi.Array',
}
Modified: pypy/branch/applevel-ctypes2/pypy/module/_ffi/array.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/module/_ffi/array.py (original)
+++ pypy/branch/applevel-ctypes2/pypy/module/_ffi/array.py Sun Jan 6 12:51:16 2008
@@ -6,7 +6,7 @@
from pypy.interpreter.baseobjspace import W_Root, ObjSpace, Wrappable,\
Arguments
from pypy.interpreter.gateway import interp2app
-from pypy.interpreter.typedef import TypeDef, GetSetProperty
+from pypy.interpreter.typedef import TypeDef, GetSetProperty, interp_attrproperty
from pypy.rpython.lltypesystem import lltype, rffi
from pypy.interpreter.error import OperationError, wrap_oserror
from pypy.module._ffi.structure import native_fmttable
@@ -23,27 +23,49 @@
return ll_array[pos]
get_elem._annspecialcase_ = 'specialize:arg(2)'
-class W_ArrayInstance(Wrappable):
- def __init__(self, space, of, w_length_or_iterable):
- self.ll_array = lltype.nullptr(rffi.VOIDP.TO)
+class W_Array(Wrappable):
+ def __init__(self, space, of):
+ self.space = space
+ self.of = of
+ self.itemsize = native_fmttable[of]['size']
+
+ def descr_call(self, space, w_length_or_iterable):
if space.is_true(space.isinstance(w_length_or_iterable, space.w_int)):
length = space.int_w(w_length_or_iterable)
- items_w = None
+ return space.wrap(W_ArrayInstance(space, self, length))
else:
items_w = space.unpackiterable(w_length_or_iterable)
length = len(items_w)
+ result = W_ArrayInstance(space, self, length)
+ for num in range(len(items_w)):
+ w_item = items_w[num]
+ unwrap_value(space, push_elem, result.ll_array, num, self.of,
+ w_item, None)
+ return space.wrap(result)
+
+def descr_new_array(space, w_type, of):
+ _get_type(space, of)
+ return space.wrap(W_Array(space, of))
+
+W_Array.typedef = TypeDef(
+ 'Array',
+ __new__ = interp2app(descr_new_array, unwrap_spec=[ObjSpace, W_Root, str]),
+ __call__ = interp2app(W_Array.descr_call,
+ unwrap_spec=['self', ObjSpace, W_Root]),
+ of = interp_attrproperty('of', W_Array),
+)
+W_Array.typedef.acceptable_as_base_class = False
+
+
+class W_ArrayInstance(Wrappable):
+ def __init__(self, space, shape, length):
+ self.ll_array = lltype.nullptr(rffi.VOIDP.TO)
self.alloced = False
- self.of = of
- _get_type(space, of)
+ self.shape = shape
self.length = length
- size = native_fmttable[of]['size'] * length
+ size = shape.itemsize * length
self.ll_array = lltype.malloc(rffi.VOIDP.TO, size, flavor='raw',
zero=True)
- if items_w:
- for num in range(len(items_w)):
- w_item = items_w[num]
- unwrap_value(space, push_elem, self.ll_array, num, self.of,
- w_item, None)
# XXX don't allow negative indexes, nor slices
@@ -51,7 +73,7 @@
if num >= self.length or num < 0:
raise OperationError(space.w_ValueError, space.wrap(
"Setting element %d of array sized %d" % (num, self.length)))
- unwrap_value(space, push_elem, self.ll_array, num, self.of, w_value,
+ unwrap_value(space, push_elem, self.ll_array, num, self.shape.of, w_value,
None)
setitem.unwrap_spec = ['self', ObjSpace, int, W_Root]
@@ -59,7 +81,7 @@
if num >= self.length or num < 0:
raise OperationError(space.w_ValueError, space.wrap(
"Getting element %d of array sized %d" % (num, self.length)))
- return wrap_value(space, get_elem, self.ll_array, num, self.of)
+ return wrap_value(space, get_elem, self.ll_array, num, self.shape.of)
getitem.unwrap_spec = ['self', ObjSpace, int]
def getbuffer(space, self):
@@ -69,9 +91,10 @@
if self.ll_array:
lltype.free(self.ll_array, flavor='raw')
-def descr_new_array_instance(space, w_type, of, w_size_or_iterable):
- return space.wrap(W_ArrayInstance(space, of, w_size_or_iterable))
-descr_new_array_instance.unwrap_spec = [ObjSpace, W_Root, str, W_Root]
+def descr_new_array_instance(space, w_type, w_shape, w_size_or_iterable):
+ w_shape = space.interp_w(W_Array, w_shape)
+ return space.wrap(W_ArrayInstance(space, w_shape, w_size_or_iterable))
+descr_new_array_instance.unwrap_spec = [ObjSpace, W_Root, W_Root, W_Root]
W_ArrayInstance.typedef = TypeDef(
'ArrayInstance',
@@ -80,3 +103,5 @@
__getitem__ = interp2app(W_ArrayInstance.getitem),
buffer = GetSetProperty(W_ArrayInstance.getbuffer),
)
+
+
Modified: pypy/branch/applevel-ctypes2/pypy/module/_ffi/test/test__ffi.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/module/_ffi/test/test__ffi.py (original)
+++ pypy/branch/applevel-ctypes2/pypy/module/_ffi/test/test__ffi.py Sun Jan 6 12:51:16 2008
@@ -263,9 +263,10 @@
raises(ValueError, "_ffi.Structure(['x1', 'xx'])")
raises(ValueError, _ffi.Structure, [('x1', 'xx')])
raises(ValueError, "_ffi.Array('xx')")
- A = _ffi.Array('i')
- A.of = 'xx'
- raises(ValueError, 'A(1)')
+ # XXX I don't think this should be allowed at all:
+ #A = _ffi.Array('i')
+ #A.of = 'xx'
+ #raises(ValueError, 'A(1)')
def test_implicit_structure(self):
skip("Does not work yet")
More information about the Pypy-commit
mailing list