[pypy-commit] cffi sirtom67/float_complex: Minor style clean-ups. Update the test to conform more closely to the

arigo pypy.commits at gmail.com
Mon May 29 13:11:18 EDT 2017


Author: Armin Rigo <arigo at tunes.org>
Branch: sirtom67/float_complex
Changeset: r2935:9a8456642302
Date: 2017-05-29 18:36 +0200
http://bitbucket.org/cffi/cffi/changeset/9a8456642302/

Log:	Minor style clean-ups. Update the test to conform more closely to
	the newer version of test_float_types(). Add a few abort() in the
	source for places missing complex support.

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -62,8 +62,6 @@
 
 #include "malloc_closure.h"
 
-#include <complex.h>
-
 #if PY_MAJOR_VERSION >= 3
 # define STR_OR_BYTES "bytes"
 # define PyText_Type PyUnicode_Type
@@ -118,34 +116,33 @@
 /************************************************************/
 
 /* base type flag: exactly one of the following: */
-#define CT_PRIMITIVE_SIGNED   1    /* signed integer */
-#define CT_PRIMITIVE_UNSIGNED 2    /* unsigned integer */
-#define CT_PRIMITIVE_CHAR     4    /* char, wchar_t */
-#define CT_PRIMITIVE_FLOAT    8    /* float, double, long double */
-#define CT_POINTER           16    /* pointer, excluding ptr-to-func */
-#define CT_ARRAY             32    /* array */
-#define CT_STRUCT            64    /* struct */
-#define CT_UNION            128    /* union */
-#define CT_FUNCTIONPTR      256    /* pointer to function */
-#define CT_VOID             512    /* void */
-
-#define CT_PRIMITIVE_COMPLEX 16777216  /* float _Complex, double _Complex */
+#define CT_PRIMITIVE_SIGNED   0x001   /* signed integer */
+#define CT_PRIMITIVE_UNSIGNED 0x002   /* unsigned integer */
+#define CT_PRIMITIVE_CHAR     0x004   /* char, wchar_t */
+#define CT_PRIMITIVE_FLOAT    0x008   /* float, double, long double */
+#define CT_POINTER            0x010   /* pointer, excluding ptr-to-func */
+#define CT_ARRAY              0x020   /* array */
+#define CT_STRUCT             0x040   /* struct */
+#define CT_UNION              0x080   /* union */
+#define CT_FUNCTIONPTR        0x100   /* pointer to function */
+#define CT_VOID               0x200   /* void */
+#define CT_PRIMITIVE_COMPLEX  0x400   /* float _Complex, double _Complex */
 
 /* other flags that may also be set in addition to the base flag: */
-#define CT_IS_VOIDCHAR_PTR       1024
-#define CT_PRIMITIVE_FITS_LONG   2048
-#define CT_IS_OPAQUE             4096
-#define CT_IS_ENUM               8192
-#define CT_IS_PTR_TO_OWNED      16384   /* only owned if CDataOwning_Type */
-#define CT_CUSTOM_FIELD_POS     32768
-#define CT_IS_LONGDOUBLE        65536
-#define CT_IS_BOOL             131072
-#define CT_IS_FILE             262144
-#define CT_IS_VOID_PTR         524288
-#define CT_WITH_VAR_ARRAY     1048576
-#define CT_IS_UNSIZED_CHAR_A  2097152
-#define CT_LAZY_FIELD_LIST    4194304
-#define CT_WITH_PACKED_CHANGE 8388608
+#define CT_IS_VOIDCHAR_PTR     0x00001000
+#define CT_PRIMITIVE_FITS_LONG 0x00002000
+#define CT_IS_OPAQUE           0x00004000
+#define CT_IS_ENUM             0x00008000
+#define CT_IS_PTR_TO_OWNED     0x00010000 /* only owned if CDataOwning_Type */
+#define CT_CUSTOM_FIELD_POS    0x00020000
+#define CT_IS_LONGDOUBLE       0x00040000
+#define CT_IS_BOOL             0x00080000
+#define CT_IS_FILE             0x00100000
+#define CT_IS_VOID_PTR         0x00200000
+#define CT_WITH_VAR_ARRAY      0x00400000
+#define CT_IS_UNSIZED_CHAR_A   0x00800000
+#define CT_LAZY_FIELD_LIST     0x01000000
+#define CT_WITH_PACKED_CHANGE  0x02000000
 #define CT_PRIMITIVE_ANY  (CT_PRIMITIVE_SIGNED |        \
                            CT_PRIMITIVE_UNSIGNED |      \
                            CT_PRIMITIVE_CHAR |          \
@@ -1567,6 +1564,9 @@
         }
         return convert_struct_from_object(data, ct, init, NULL);
     }
+    if (ct->ct_flags & CT_PRIMITIVE_COMPLEX) {
+        abort(); // XXX
+    }
     PyErr_Format(PyExc_SystemError,
                  "convert_from_object: '%s'", ct->ct_name);
     return -1;
@@ -2004,6 +2004,9 @@
                 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;
         }
+        if (cd->c_type->ct_flags & CT_PRIMITIVE_COMPLEX) {
+            abort(); // XXX
+        }
     }
     return cd->c_data != NULL;
 }
@@ -2072,11 +2075,6 @@
         }
         return PyFloat_FromDouble(value);
     }
-    if ((cd->c_type->ct_flags & CT_PRIMITIVE_COMPLEX) &&
-        (cd->c_type->ct_size<16)) {
-        double value = read_raw_float_data(cd->c_data, cd->c_type->ct_size);
-        return PyComplex_FromDoubles(value, 0.0);
-    }
     PyErr_Format(PyExc_TypeError, "float() not supported on cdata '%s'",
                  cd->c_type->ct_name);
     return NULL;
@@ -2978,6 +2976,7 @@
         value.imag = 0.0;
         return PyComplex_FromCComplex(value);
     }
+    abort(); // XXX ints, etc.
     PyErr_Format(PyExc_TypeError, "complex() not supported on cdata '%s'",
                  cd->c_type->ct_name);
     return NULL;
@@ -3792,8 +3791,6 @@
         }
         return (PyObject *)cd;
     }
-
-
     else if (ct->ct_flags & CT_PRIMITIVE_COMPLEX) {
         /* cast to a complex */
         Py_complex value;
@@ -3835,8 +3832,6 @@
         }
         return (PyObject *)cd;
     }
-
-
     else {
         PyErr_Format(PyExc_TypeError, "cannot cast to ctype '%s'",
                      ct->ct_name);
@@ -6738,6 +6733,8 @@
         return 1000 * p[0];
     return -42;
 }
+
+#if 0   /* libffi doesn't properly support complexes currently */
 static float _Complex _testfunc24(float a, float b)
 {
     return a + I*2.0*b;
@@ -6746,6 +6743,7 @@
 {
     return a + I*2.0*b;
 }
+#endif
 
 static PyObject *b__testfunc(PyObject *self, PyObject *args)
 {
@@ -6779,8 +6777,10 @@
     case 21: f = &_testfunc21; break;
     case 22: f = &_testfunc22; break;
     case 23: f = &_testfunc23; break;
+#if 0
     case 24: f = &_testfunc24; break;
     case 25: f = &_testfunc25; break;
+#endif
     default:
         PyErr_SetNone(PyExc_ValueError);
         return NULL;
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -188,12 +188,13 @@
     INF = 1E200 * 1E200
     for name in ["float", "double"]:
         p = new_primitive_type(name + " _Complex")
-        assert bool(cast(p, 0))
+        assert bool(cast(p, 0)) is False
         assert bool(cast(p, INF))
         assert bool(cast(p, -INF))
-        assert bool(cast(p, 0j))
+        assert bool(cast(p, 0j)) is False
         assert bool(cast(p, INF*1j))
         assert bool(cast(p, -INF*1j))
+        # "can't convert complex to float", like CPython's "float(0j)"
         py.test.raises(TypeError, int, cast(p, -150))
         py.test.raises(TypeError, long, cast(p, -150))
         py.test.raises(TypeError, float, cast(p, -150))
@@ -209,13 +210,24 @@
         assert cast(p, -1.1j) == cast(p, -1.1j)
         assert repr(complex(cast(p, -0.0)).real) == '-0.0'
         #assert repr(complex(cast(p, -0j))) == '-0j'   # http://bugs.python.org/issue29602
-        assert complex(cast(p, b'\x09')) == 9.0
-        assert complex(cast(p, u+'\x09')) == 9.0
-        assert complex(cast(p, True)) == 1.0
+        assert complex(cast(p, b'\x09')) == 9.0 + 0j
+        assert complex(cast(p, u+'\x09')) == 9.0 + 0j
+        assert complex(cast(p, True)) == 1.0 + 0j
         py.test.raises(TypeError, cast, p, None)
         #
-        py.test.raises(TypeError, cast, new_primitive_type(name), 1+2j)
-    py.test.raises(TypeError, cast, new_primitive_type("int"), 1+2j)
+        py.test.raises(TypeError, cast, new_primitive_type(name), 1+0j)
+        #
+        assert complex(cast(new_primitive_type("char"), "A")) == 65 + 0j
+        assert complex(cast(new_primitive_type("int"), 65)) == 65 + 0j
+        assert complex(cast(new_primitive_type("uint64_t"), 65)) == 65 + 0j
+        assert complex(cast(new_primitive_type("float"), 65.5)) == 65.5 + 0j
+        #
+        BArray = new_array_type(new_pointer_type(p), 10)
+        x = newp(BArray, None)
+        x[5] = 12.34 + 56.78j
+        assert type(x[5]) is complex
+        assert x[5] == 12.34 + 56.78j
+    py.test.raises(TypeError, cast, new_primitive_type("int"), 1+0j)
 
 def test_character_type():
     p = new_primitive_type("char")


More information about the pypy-commit mailing list