[pypy-commit] pypy ffi-backend: Next test.

arigo noreply at buildbot.pypy.org
Thu Jun 21 13:16:21 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: ffi-backend
Changeset: r55748:497d9fce6929
Date: 2012-06-20 18:40 +0200
http://bitbucket.org/pypy/pypy/changeset/497d9fce6929/

Log:	Next test.

diff --git a/pypy/module/_ffi_backend/cdataobj.py b/pypy/module/_ffi_backend/cdataobj.py
--- a/pypy/module/_ffi_backend/cdataobj.py
+++ b/pypy/module/_ffi_backend/cdataobj.py
@@ -44,6 +44,11 @@
         keepalive_until_here(self)
         return w_result
 
+    def str(self):
+        w_result = self.ctype.try_str(self.cdata)
+        keepalive_until_here(self)
+        return w_result or self.repr()
+
     def read_raw_signed_data(self):
         result = misc.read_raw_signed_data(self.cdata, self.ctype.size)
         keepalive_until_here(self)
@@ -91,6 +96,7 @@
     __int__ = interp2app(W_CData.int),
     __long__ = interp2app(W_CData.long),
     __float__ = interp2app(W_CData.float),
+    __str__ = interp2app(W_CData.str),
     )
 W_CData.acceptable_as_base_class = False
 
diff --git a/pypy/module/_ffi_backend/ctypeobj.py b/pypy/module/_ffi_backend/ctypeobj.py
--- a/pypy/module/_ffi_backend/ctypeobj.py
+++ b/pypy/module/_ffi_backend/ctypeobj.py
@@ -36,6 +36,9 @@
     def convert_to_object(self, cdata):
         raise NotImplementedError
 
+    def try_str(self, cdata):
+        return None
+
 
 class W_CTypePrimitive(W_CType):
 
@@ -44,7 +47,12 @@
         if cdataobj.check_cdata(space, w_ob):
             xxx
         elif space.isinstance_w(w_ob, space.w_str):
-            xxx
+            s = space.str_w(w_ob)
+            if len(s) != 1:
+                raise operationerrfmt(space.w_TypeError,
+                    "cannot cast string of length %d to ctype '%s'",
+                                      len(s), self.name)
+            value = ord(s[0])
         elif space.is_w(w_ob, space.w_None):
             value = 0
         else:
@@ -57,7 +65,10 @@
 class W_CTypePrimitiveChar(W_CTypePrimitive):
 
     def int(self, cdata):
-        xxx
+        return self.space.wrap(ord(cdata[0]))
+
+    def try_str(self, cdata):
+        return self.space.wrap(cdata[0])
 
 
 class W_CTypePrimitiveSigned(W_CTypePrimitive):
diff --git a/pypy/module/_ffi_backend/test/test_c.py b/pypy/module/_ffi_backend/test/test_c.py
--- a/pypy/module/_ffi_backend/test/test_c.py
+++ b/pypy/module/_ffi_backend/test/test_c.py
@@ -124,6 +124,8 @@
             assert repr(float(cast(p, -0.0))) == '-0.0'
 
     def test_character_type(self):
+        new_primitive_type = self.b.new_primitive_type
+        cast = self.b.cast
         p = new_primitive_type("char")
         assert bool(cast(p, '\x00'))
         assert cast(p, '\x00') != cast(p, -17*256)


More information about the pypy-commit mailing list