Debian Codegraph @

Modules/_testcapi/complex.c

raw
1 #include "parts.h"
2 #include "util.h"
3
4
5 static PyObject *
6 complex_fromccomplex(PyObject *Py_UNUSED(module), PyObject *obj)
7 {
8 Py_complex complex;
9
10 if (!PyArg_Parse(obj, "D", &complex)) {
11 return NULL;
12 }
13
14 return PyComplex_FromCComplex(complex);
15 }
16
17 static PyObject *
18 complex_asccomplex(PyObject *Py_UNUSED(module), PyObject *obj)
19 {
20 Py_complex complex;
21
22 NULLABLE(obj);
23 complex = PyComplex_AsCComplex(obj);
24
25 if (complex.real == -1. && PyErr_Occurred()) {
26 return NULL;
27 }
28
29 return PyComplex_FromCComplex(complex);
30 }
31
32 static PyObject*
33 _py_c_neg(PyObject *Py_UNUSED(module), PyObject *num)
34 {
35 Py_complex complex;
36
37 complex = PyComplex_AsCComplex(num);
38 if (complex.real == -1. && PyErr_Occurred()) {
39 return NULL;
40 }
41
42 return PyComplex_FromCComplex(_Py_c_neg(complex));
43 }
44
45 #define _PY_C_FUNC2(suffix) \
46 static PyObject * \
47 _py_c_##suffix(PyObject *Py_UNUSED(module), PyObject *args) \
48 { \
49 Py_complex a, b, res; \
50 \
51 if (!PyArg_ParseTuple(args, "DD", &a, &b)) { \
52 return NULL; \
53 } \
54 \
55 errno = 0; \
56 res = _Py_c_##suffix(a, b); \
57 return Py_BuildValue("Di", &res, errno); \
58 };
59
60 _PY_C_FUNC2(sum)
61 _PY_C_FUNC2(diff)
62 _PY_C_FUNC2(prod)
63 _PY_C_FUNC2(quot)
64 _PY_C_FUNC2(pow)
65
66 static PyObject*
67 _py_c_abs(PyObject *Py_UNUSED(module), PyObject* obj)
68 {
69 Py_complex complex;
70 double res;
71
72 NULLABLE(obj);
73 complex = PyComplex_AsCComplex(obj);
74
75 if (complex.real == -1. && PyErr_Occurred()) {
76 return NULL;
77 }
78
79 errno = 0;
80 res = _Py_c_abs(complex);
81 return Py_BuildValue("di", res, errno);
82 }
83
84
85 static PyMethodDef test_methods[] = {
86 {"complex_fromccomplex", complex_fromccomplex, METH_O},
87 {"complex_asccomplex", complex_asccomplex, METH_O},
88 {"_py_c_sum", _py_c_sum, METH_VARARGS},
89 {"_py_c_diff", _py_c_diff, METH_VARARGS},
90 {"_py_c_neg", _py_c_neg, METH_O},
91 {"_py_c_prod", _py_c_prod, METH_VARARGS},
92 {"_py_c_quot", _py_c_quot, METH_VARARGS},
93 {"_py_c_pow", _py_c_pow, METH_VARARGS},
94 {"_py_c_abs", _py_c_abs, METH_O},
95 {NULL},
96 };
97
98 int
99 _PyTestCapi_Init_Complex(PyObject *mod)
100 {
101 if (PyModule_AddFunctions(mod, test_methods) < 0) {
102 return -1;
103 }
104
105 return 0;
106 }