Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r2691:e7ca388b0197
Date: 2016-05-07 14:15 +0200
http://bitbucket.org/cffi/cffi/changeset/e7ca388b0197/

Log:    Issue #255: `bool(ffi.cast("primitive", x))` is now True or False
        depending on whether the value is zero or not. It used to always be
        True for any value.

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -1858,6 +1858,18 @@
 
 static int cdata_nonzero(CDataObject *cd)
 {
+    if (cd->c_type->ct_flags & CT_PRIMITIVE_ANY) {
+        if (cd->c_type->ct_flags & (CT_PRIMITIVE_SIGNED |
+                                    CT_PRIMITIVE_UNSIGNED |
+                                    CT_PRIMITIVE_CHAR))
+            return read_raw_unsigned_data(cd->c_data, cd->c_type->ct_size) != 
0;
+
+        if (cd->c_type->ct_flags & CT_PRIMITIVE_FLOAT) {
+            if (cd->c_type->ct_flags & CT_IS_LONGDOUBLE)
+                return read_raw_longdouble_data(cd->c_data) != 0.0;
+            return read_raw_float_data(cd->c_data, cd->c_type->ct_size) != 0.0;
+        }
+    }
     return cd->c_data != NULL;
 }
 
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -152,7 +152,10 @@
     INF = 1E200 * 1E200
     for name in ["float", "double"]:
         p = new_primitive_type(name)
-        assert bool(cast(p, 0))
+        assert bool(cast(p, 0)) is False      # since 1.7
+        assert bool(cast(p, -0.0)) is False   # since 1.7
+        assert bool(cast(p, 1e-42)) is True
+        assert bool(cast(p, -1e-42)) is True
         assert bool(cast(p, INF))
         assert bool(cast(p, -INF))
         assert int(cast(p, -150)) == -150
@@ -213,7 +216,8 @@
 
 def test_character_type():
     p = new_primitive_type("char")
-    assert bool(cast(p, '\x00'))
+    assert bool(cast(p, 'A')) is True
+    assert bool(cast(p, '\x00')) is False    # since 1.7
     assert cast(p, '\x00') != cast(p, -17*256)
     assert int(cast(p, 'A')) == 65
     assert long(cast(p, 'A')) == 65
@@ -2569,7 +2573,8 @@
     BBoolP = new_pointer_type(BBool)
     assert int(cast(BBool, False)) == 0
     assert int(cast(BBool, True)) == 1
-    assert bool(cast(BBool, False)) is True    # warning!
+    assert bool(cast(BBool, False)) is False    # since 1.7
+    assert bool(cast(BBool, True)) is True
     assert int(cast(BBool, 3)) == 1
     assert int(cast(BBool, long(3))) == 1
     assert int(cast(BBool, long(10)**4000)) == 1
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to