[pypy-commit] cffi sirtom67/float_complex: "c" tests pass!
sirtom67
pypy.commits at gmail.com
Sun Feb 19 20:52:36 EST 2017
Author: Tom Krauss <thomas.p.krauss at gmail.com>
Branch: sirtom67/float_complex
Changeset: r2891:3ce9e47d35f0
Date: 2017-02-19 19:51 -0600
http://bitbucket.org/cffi/cffi/changeset/3ce9e47d35f0/
Log: "c" tests pass!
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -3536,6 +3536,36 @@
return cd;
}
+static void check_bytes_for_float_compatible(
+ PyObject *io,
+ double * value,
+ int * got_value_indicator,
+ int * cannot_cast_indicator)
+{
+ *got_value_indicator = 0;
+ *cannot_cast_indicator = 0;
+ if (PyBytes_Check(io)) {
+ if (PyBytes_GET_SIZE(io) != 1) {
+ Py_DECREF(io);
+ *cannot_cast_indicator = 1;
+ }
+ *value = (unsigned char)PyBytes_AS_STRING(io)[0];
+ *got_value_indicator = 1;
+ }
+#if HAVE_WCHAR_H
+ else if (PyUnicode_Check(io)) {
+ wchar_t ordinal;
+ if (_my_PyUnicode_AsSingleWideChar(io, &ordinal) < 0) {
+ Py_DECREF(io);
+ *cannot_cast_indicator = 1;
+ }
+ *value = (long)ordinal;
+ *got_value_indicator = 1;
+ }
+#endif
+
+}
+
static PyObject *do_cast(CTypeDescrObject *ct, PyObject *ob)
{
CDataObject *cd;
@@ -3592,23 +3622,16 @@
Py_INCREF(io);
}
- if (PyBytes_Check(io)) {
- if (PyBytes_GET_SIZE(io) != 1) {
- Py_DECREF(io);
- goto cannot_cast;
- }
- value = (unsigned char)PyBytes_AS_STRING(io)[0];
- }
-#if HAVE_WCHAR_H
- else if (PyUnicode_Check(io)) {
- wchar_t ordinal;
- if (_my_PyUnicode_AsSingleWideChar(io, &ordinal) < 0) {
- Py_DECREF(io);
- goto cannot_cast;
- }
- value = (long)ordinal;
- }
-#endif
+ int got_value_indicator;
+ int cannot_cast_indicator;
+ check_bytes_for_float_compatible(io, &value,
+ &got_value_indicator, &cannot_cast_indicator);
+ if (cannot_cast_indicator) {
+ goto cannot_cast;
+ }
+ if (got_value_indicator) {
+ // got it from string
+ }
else if ((ct->ct_flags & CT_IS_LONGDOUBLE) &&
CData_Check(io) &&
(((CDataObject *)io)->c_type->ct_flags & CT_IS_LONGDOUBLE)) {
@@ -3658,11 +3681,23 @@
Py_INCREF(io);
}
- value = PyComplex_AsCComplex(io);
+ int got_value_indicator;
+ int cannot_cast_indicator;
+ check_bytes_for_float_compatible(io, &(value.real),
+ &got_value_indicator, &cannot_cast_indicator);
+ if (cannot_cast_indicator) {
+ goto cannot_cast;
+ }
+ if (got_value_indicator) {
+ // got it from string
+ value.imag = 0.0;
+ } else {
+ value = PyComplex_AsCComplex(io);
+ }
Py_DECREF(io);
- if (value.real == -1.0 && value.imag == 0.0 && PyErr_Occurred())
+ if (value.real == -1.0 && value.imag == 0.0 && PyErr_Occurred()) {
return NULL;
-
+ }
cd = _new_casted_primitive(ct);
if (cd != NULL) {
write_raw_complex_data(cd->c_data, value, ct->ct_size);
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -206,8 +206,9 @@
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'
- assert complex(cast(p, '\x09')) == 9.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
py.test.raises(TypeError, cast, p, None)
#
More information about the pypy-commit
mailing list