Author: Tom Krauss <thomas.p.kra...@gmail.com> Branch: sirtom67/float_complex Changeset: r2865:d3cb148ff6fa Date: 2017-01-22 19:36 -0600 http://bitbucket.org/cffi/cffi/changeset/d3cb148ff6fa/
Log: unskip the complex test in c/test_c.py. It still doesn't pass but gets past a couple points diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c --- a/c/_cffi_backend.c +++ b/c/_cffi_backend.c @@ -899,6 +899,25 @@ _write_raw_data(long double); } +#define _write_raw_complex_data(type) \ + do { \ + if (size == 2*sizeof(type)) { \ + type r = (type)source.real; \ + memcpy(target, &r, sizeof(type)); \ + type i = (type)source.imag; \ + memcpy(target+sizeof(type), &i, sizeof(type)); \ + return; \ + } \ + } while(0) + +static void +write_raw_complex_data(char *target, Py_complex source, int size) +{ + _write_raw_complex_data(float); + _write_raw_complex_data(double); + Py_FatalError("write_raw_complex_data: bad float size"); +} + static PyObject * new_simple_cdata(char *data, CTypeDescrObject *ct) { @@ -3574,6 +3593,40 @@ } return (PyObject *)cd; } + + + else if (ct->ct_flags & CT_PRIMITIVE_COMPLEX) { + /* cast to a complex */ + Py_complex value; + PyObject *io; + printf("_cffi_backend.c do_cast ct->ct_size=%ld\n", ct->ct_size); + if (CData_Check(ob)) { + CDataObject *cdsrc = (CDataObject *)ob; + + if (!(cdsrc->c_type->ct_flags & CT_PRIMITIVE_ANY)) + goto cannot_cast; + io = convert_to_object(cdsrc->c_data, cdsrc->c_type); + if (io == NULL) + return NULL; + } + else { + io = ob; + Py_INCREF(io); + } + + value = PyComplex_AsCComplex(io); + Py_DECREF(io); + 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); + } + return (PyObject *)cd; + } + + else { PyErr_Format(PyExc_TypeError, "cannot cast to ctype '%s'", ct->ct_name); diff --git a/c/test_c.py b/c/test_c.py --- a/c/test_c.py +++ b/c/test_c.py @@ -183,10 +183,9 @@ py.test.raises(TypeError, cast, p, None) def test_complex_types(): - py.test.skip("later") INF = 1E200 * 1E200 - for name in ["float", "double"]: - p = new_primitive_type("_Complex " + name) + for name in ["float"]: #, "double"]: + p = new_primitive_type(name + " _Complex") assert bool(cast(p, 0)) assert bool(cast(p, INF)) assert bool(cast(p, -INF)) _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit