Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r96405:afdbea198bb5
Date: 2019-04-02 16:57 +0200
http://bitbucket.org/pypy/pypy/changeset/afdbea198bb5/

Log:    update to cffi/e43fdc644918

diff --git a/pypy/module/_cffi_backend/ctypeprim.py 
b/pypy/module/_cffi_backend/ctypeprim.py
--- a/pypy/module/_cffi_backend/ctypeprim.py
+++ b/pypy/module/_cffi_backend/ctypeprim.py
@@ -401,15 +401,22 @@
         # bypass the method 'string' implemented in W_CTypePrimitive
         return W_CType.string(self, cdataobj, maxlen)
 
-    def convert_to_object(self, cdata):
-        space = self.space
+    def _read_bool_0_or_1(self, cdata):
+        """Read one byte, check it is 0 or 1, but return it as an integer"""
         value = ord(cdata[0])
-        if value < 2:
-            return space.newbool(value != 0)
-        else:
-            raise oefmt(space.w_ValueError,
+        if value >= 2:
+            raise oefmt(self.space.w_ValueError,
                         "got a _Bool of value %d, expected 0 or 1",
                         value)
+        return value
+
+    def convert_to_object(self, cdata):
+        value = self._read_bool_0_or_1(cdata)
+        return self.space.newbool(value != 0)
+
+    def cast_to_int(self, cdata):
+        value = self._read_bool_0_or_1(cdata)
+        return self.space.newint(value)
 
     def unpack_list_of_int_items(self, ptr, length):
         return None
diff --git a/pypy/module/_cffi_backend/test/_backend_test_c.py 
b/pypy/module/_cffi_backend/test/_backend_test_c.py
--- a/pypy/module/_cffi_backend/test/_backend_test_c.py
+++ b/pypy/module/_cffi_backend/test/_backend_test_c.py
@@ -4343,3 +4343,14 @@
     release(p)   # no effect
     a += b'w' * 1000
     assert a == bytearray(b"xyz" + b't' * 10 + b'v' * 100 + b'w' * 1000)
+
+def test_int_doesnt_give_bool():
+    BBool = new_primitive_type("_Bool")
+    x = int(cast(BBool, 42))
+    assert type(x) is int and x == 1
+    x = long(cast(BBool, 42))
+    assert type(x) is long and x == 1
+    with pytest.raises(TypeError):
+        float(cast(BBool, 42))
+    with pytest.raises(TypeError):
+        complex(cast(BBool, 42))
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to