diff options
author | Tom Krauss <thomas.p.krauss@gmail.com> | 2017-01-22 19:36:02 -0600 |
---|---|---|
committer | Tom Krauss <thomas.p.krauss@gmail.com> | 2017-01-22 19:36:02 -0600 |
commit | eb745c2a2e27ffa2debbf4b2b2ed7d60075429e1 (patch) | |
tree | 1b516a161565b38fe9c0ddf8639f1b6acfd9a262 | |
parent | 79c196ade5ba7820b063ae9f0eb98ec62b9a3aaa (diff) | |
download | cffi-eb745c2a2e27ffa2debbf4b2b2ed7d60075429e1.tar.gz |
unskip the complex test in c/test_c.py. It still doesn't pass but gets past a couple points
-rw-r--r-- | c/_cffi_backend.c | 53 | ||||
-rw-r--r-- | c/test_c.py | 5 |
2 files changed, 55 insertions, 3 deletions
diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c index 3970e7e..dcb489d 100644 --- a/c/_cffi_backend.c +++ b/c/_cffi_backend.c @@ -899,6 +899,25 @@ write_raw_longdouble_data(char *target, long double source) _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 @@ static PyObject *do_cast(CTypeDescrObject *ct, PyObject *ob) } 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 index 0ef0327..fa9356d 100644 --- a/c/test_c.py +++ b/c/test_c.py @@ -183,10 +183,9 @@ def test_float_types(): 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)) |