[pypy-commit] cffi default: Shut down a warning from recent CPython versions: int() should

arigo pypy.commits at gmail.com
Tue Apr 2 10:40:11 EDT 2019


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r3258:d5c083291044
Date: 2019-04-02 16:39 +0200
http://bitbucket.org/cffi/cffi/changeset/d5c083291044/

Log:	Shut down a warning from recent CPython versions: int() should
	always return an int, not a bool

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -2193,7 +2193,10 @@
         return PyInt_FromLong(value);
     }
     if (cd->c_type->ct_flags & (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_UNSIGNED)) {
-        return convert_to_object(cd->c_data, cd->c_type);
+        PyObject *result = convert_to_object(cd->c_data, cd->c_type);
+        if (result != NULL && PyBool_Check(result))
+            result = PyInt_FromLong(PyInt_AsLong(result));
+        return result;
     }
     else if (cd->c_type->ct_flags & CT_PRIMITIVE_CHAR) {
         /*READ(cd->c_data, cd->c_type->ct_size)*/
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -4356,3 +4356,8 @@
     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


More information about the pypy-commit mailing list