Author: Amaury Forgeot d'Arc <[email protected]>
Branch:
Changeset: r54845:3617c6ce9ef2
Date: 2012-04-29 21:47 +0200
http://bitbucket.org/pypy/pypy/changeset/3617c6ce9ef2/
Log: cpyext: add PyComplex_FromCComplex
diff --git a/pypy/module/cpyext/complexobject.py
b/pypy/module/cpyext/complexobject.py
--- a/pypy/module/cpyext/complexobject.py
+++ b/pypy/module/cpyext/complexobject.py
@@ -33,6 +33,11 @@
# CPython also accepts anything
return 0.0
+@cpython_api([Py_complex_ptr], PyObject)
+def _PyComplex_FromCComplex(space, v):
+ """Create a new Python complex number object from a C Py_complex value."""
+ return space.newcomplex(v.c_real, v.c_imag)
+
# lltype does not handle functions returning a structure. This implements a
# helper function, which takes as argument a reference to the return value.
@cpython_api([PyObject, Py_complex_ptr], lltype.Void)
diff --git a/pypy/module/cpyext/include/complexobject.h
b/pypy/module/cpyext/include/complexobject.h
--- a/pypy/module/cpyext/include/complexobject.h
+++ b/pypy/module/cpyext/include/complexobject.h
@@ -21,6 +21,8 @@
return result;
}
+#define PyComplex_FromCComplex(c) _PyComplex_FromCComplex(&c)
+
#ifdef __cplusplus
}
#endif
diff --git a/pypy/module/cpyext/test/test_complexobject.py
b/pypy/module/cpyext/test/test_complexobject.py
--- a/pypy/module/cpyext/test/test_complexobject.py
+++ b/pypy/module/cpyext/test/test_complexobject.py
@@ -31,3 +31,12 @@
assert module.as_tuple(12-34j) == (12, -34)
assert module.as_tuple(-3.14) == (-3.14, 0.0)
raises(TypeError, module.as_tuple, "12")
+
+ def test_FromCComplex(self):
+ module = self.import_extension('foo', [
+ ("test", "METH_NOARGS",
+ """
+ Py_complex c = {1.2, 3.4};
+ return PyComplex_FromCComplex(c);
+ """)])
+ assert module.test() == 1.2 + 3.4j
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit