[pypy-svn] r50625 - in pypy/branch/applevel-ctypes2/pypy/module/_rawffi: . test
arigo at codespeak.net
arigo at codespeak.net
Tue Jan 15 13:14:55 CET 2008
Author: arigo
Date: Tue Jan 15 13:14:52 2008
New Revision: 50625
Modified:
pypy/branch/applevel-ctypes2/pypy/module/_rawffi/array.py
pypy/branch/applevel-ctypes2/pypy/module/_rawffi/structure.py
pypy/branch/applevel-ctypes2/pypy/module/_rawffi/test/test__rawffi.py
Log:
(fijal, arigo)
Some nice __repr__'s.
Modified: pypy/branch/applevel-ctypes2/pypy/module/_rawffi/array.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/module/_rawffi/array.py (original)
+++ pypy/branch/applevel-ctypes2/pypy/module/_rawffi/array.py Tue Jan 15 13:14:52 2008
@@ -50,6 +50,10 @@
self.itemtp, w_item)
return space.wrap(result)
+ def descr_repr(self, space):
+ return space.wrap("<_rawffi.Array '%s' (%d, %d)>" % self.itemtp)
+ descr_repr.unwrap_spec = ['self', ObjSpace]
+
def fromaddress(self, space, address, length):
return space.wrap(W_ArrayInstance(space, self, length, address))
fromaddress.unwrap_spec = ['self', ObjSpace, int, int]
@@ -88,6 +92,7 @@
unwrap_spec=[ObjSpace, W_Root, W_Root]),
__call__ = interp2app(W_Array.descr_call,
unwrap_spec=['self', ObjSpace, int, W_Root]),
+ __repr__ = interp2app(W_Array.descr_repr),
fromaddress = interp2app(W_Array.fromaddress),
gettypecode = interp2app(W_Array.descr_gettypecode),
)
@@ -101,6 +106,12 @@
self.length = length
self.shape = shape
+ def descr_repr(self, space):
+ addr = rffi.cast(lltype.Signed, self.ll_buffer)
+ return space.wrap("<_rawffi array %d of length %d>" % (addr,
+ self.length))
+ descr_repr.unwrap_spec = ['self', ObjSpace]
+
# XXX don't allow negative indexes, nor slices
def setitem(self, space, num, w_value):
@@ -129,6 +140,7 @@
W_ArrayInstance.typedef = TypeDef(
'ArrayInstance',
+ __repr__ = interp2app(W_ArrayInstance.descr_repr),
__setitem__ = interp2app(W_ArrayInstance.setitem),
__getitem__ = interp2app(W_ArrayInstance.getitem),
__len__ = interp2app(W_ArrayInstance.getlength),
Modified: pypy/branch/applevel-ctypes2/pypy/module/_rawffi/structure.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/module/_rawffi/structure.py (original)
+++ pypy/branch/applevel-ctypes2/pypy/module/_rawffi/structure.py Tue Jan 15 13:14:52 2008
@@ -78,6 +78,13 @@
return space.wrap(W_StructureInstance(space, self, 0, kwargs_w))
descr_call.unwrap_spec = ['self', ObjSpace, Arguments]
+ def descr_repr(self, space):
+ fieldnames = ' '.join(["'%s'" % name for name, _ in self.fields])
+ return space.wrap("<_rawffi.Structure %s (%d, %d)>" % (fieldnames,
+ self.size,
+ self.alignment))
+ descr_repr.unwrap_spec = ['self', ObjSpace]
+
def fromaddress(self, space, address):
return space.wrap(W_StructureInstance(space, self, address, None))
fromaddress.unwrap_spec = ['self', ObjSpace, int]
@@ -99,6 +106,7 @@
'Structure',
__new__ = interp2app(descr_new_structure),
__call__ = interp2app(W_Structure.descr_call),
+ __repr__ = interp2app(W_Structure.descr_repr),
fromaddress = interp2app(W_Structure.fromaddress),
size = interp_attrproperty('size', W_Structure),
alignment = interp_attrproperty('alignment', W_Structure),
@@ -128,6 +136,11 @@
for field, w_value in fieldinits_w.iteritems():
self.setattr(space, field, w_value)
+ def descr_repr(self, space):
+ addr = rffi.cast(lltype.Signed, self.ll_buffer)
+ return space.wrap("<_rawffi struct %d>" % (addr,))
+ descr_repr.unwrap_spec = ['self', ObjSpace]
+
def getattr(self, space, attr):
if not self.ll_buffer:
raise segfault_exception(space, "accessing NULL pointer")
@@ -153,6 +166,7 @@
W_StructureInstance.typedef = TypeDef(
'StructureInstance',
+ __repr__ = interp2app(W_StructureInstance.descr_repr),
__getattr__ = interp2app(W_StructureInstance.getattr),
__setattr__ = interp2app(W_StructureInstance.setattr),
buffer = GetSetProperty(W_StructureInstance.getbuffer),
Modified: pypy/branch/applevel-ctypes2/pypy/module/_rawffi/test/test__rawffi.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/module/_rawffi/test/test__rawffi.py (original)
+++ pypy/branch/applevel-ctypes2/pypy/module/_rawffi/test/test__rawffi.py Tue Jan 15 13:14:52 2008
@@ -438,3 +438,20 @@
arg1.free()
arg2.free()
a.free()
+
+ def test_repr(self):
+ import _rawffi, struct
+ s = struct.calcsize("i")
+ assert (repr(_rawffi.Array('i')) ==
+ "<_rawffi.Array 'i' (%d, %d)>" % (s, s))
+ assert repr(_rawffi.Array((18, 2))) == "<_rawffi.Array '?' (18, 2)>"
+ assert (repr(_rawffi.Structure([('x', 'i'), ('yz', 'i')])) ==
+ "<_rawffi.Structure 'x' 'yz' (%d, %d)>" % (2*s, s))
+
+ s = _rawffi.Structure([('x', 'i'), ('yz', 'i')])()
+ assert repr(s) == "<_rawffi struct %d>" % (s.buffer,)
+ s.free()
+ a = _rawffi.Array('i')(5)
+ assert repr(a) == "<_rawffi array %d of length %d>" % (a.buffer,
+ len(a))
+ a.free()
More information about the Pypy-commit
mailing list