Author: Armin Rigo <ar...@tunes.org>
Branch: ffi-backend
Changeset: r55805:5dac7ab941a2
Date: 2012-06-24 18:21 +0200
http://bitbucket.org/pypy/pypy/changeset/5dac7ab941a2/

Log:    Fixes.

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
@@ -59,8 +59,7 @@
                               self.ctype.name)
 
     def str(self):
-        w_result = self.ctype.try_str(self._cdata)
-        keepalive_until_here(self)
+        w_result = self.ctype.try_str(self)
         return w_result or self.repr()
 
     @specialize.arg(2)
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
@@ -54,7 +54,7 @@
                               "cdata of type '%s' cannot be indexed",
                               self.name)
 
-    def try_str(self, cdata):
+    def try_str(self, cdataobj):
         return None
 
     def add(self, cdata, i):
@@ -109,6 +109,13 @@
             extra = " *"
         W_CTypePtrOrArray.__init__(self, space, size, extra, 2, ctitem)
 
+    def try_str(self, cdataobj):
+        if isinstance(self.ctitem, W_CTypePrimitiveChar):
+            s = rffi.charp2str(cdataobj._cdata)
+            keepalive_until_here(cdataobj)
+            return self.space.wrap(s)
+        return None
+
     def cast(self, w_ob):
         space = self.space
         ob = space.interpclass_w(w_ob)
@@ -164,6 +171,13 @@
         self.length = length
         self.ctptr = ctptr
 
+    def try_str(self, cdataobj):
+        if isinstance(self.ctitem, W_CTypePrimitiveChar):
+            s = rffi.charp2strn(cdataobj._cdata, cdataobj.get_array_length())
+            keepalive_until_here(cdataobj)
+            return self.space.wrap(s)
+        return None
+
     def _alignof(self):
         return self.ctitem.alignof()
 
@@ -296,7 +310,10 @@
     def convert_to_object(self, cdata):
         return self.space.wrap(cdata[0])
 
-    try_str = convert_to_object
+    def try_str(self, cdataobj):
+        w_res = self.convert_to_object(cdataobj._cdata)
+        keepalive_until_here(cdataobj)
+        return w_res
 
     def _convert_to_char(self, w_ob):
         space = self.space
diff --git a/pypy/module/_ffi_backend/test/_backend_test_c.py 
b/pypy/module/_ffi_backend/test/_backend_test_c.py
--- a/pypy/module/_ffi_backend/test/_backend_test_c.py
+++ b/pypy/module/_ffi_backend/test/_backend_test_c.py
@@ -205,9 +205,17 @@
     BChar = new_primitive_type("char")
     BInt = new_primitive_type("int")
     BFloat = new_primitive_type("float")
-    assert (hash(cast(BChar, 'A')) !=
-            hash(cast(BInt, 65)))
-    assert hash(cast(BFloat, 65)) != hash(65.0)
+    for i in range(1, 20):
+        if (hash(cast(BChar, chr(i))) !=
+            hash(cast(BInt, i))):
+            break
+    else:
+        raise AssertionError("hashes are equal")
+    for i in range(1, 20):
+        if hash(cast(BFloat, i)) != hash(float(i)):
+            break
+    else:
+        raise AssertionError("hashes are equal")
 
 def test_no_len_on_nonarray():
     p = new_primitive_type("int")
@@ -933,3 +941,13 @@
     u1 = newp(BUnionPtr, 42)
     u2 = newp(BUnionPtr, u1[0])
     assert u2.a1 == 42
+
+def test_str():
+    BChar = new_primitive_type("char")
+    BCharP = new_pointer_type(BChar)
+    BArray = new_array_type(BCharP, 10)
+    a = newp(BArray, "hello")
+    assert len(a) == 10
+    assert str(a) == "hello"
+    p = a + 2
+    assert str(p) == "llo"
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to