| 
									
										
										
										
											2023-11-01 08:44:54 +03:00
										 |  |  | #include "parts.h"
 | 
					
						
							|  |  |  | #include "util.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-05 10:42:42 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | complex_fromccomplex(PyObject *Py_UNUSED(module), PyObject *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_complex complex; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!PyArg_Parse(obj, "D", &complex)) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return PyComplex_FromCComplex(complex); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | complex_asccomplex(PyObject *Py_UNUSED(module), PyObject *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_complex complex; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     NULLABLE(obj); | 
					
						
							|  |  |  |     complex = PyComplex_AsCComplex(obj); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (complex.real == -1. && PyErr_Occurred()) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return PyComplex_FromCComplex(complex); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-28 11:18:33 +03:00
										 |  |  | static PyObject* | 
					
						
							|  |  |  | _py_c_neg(PyObject *Py_UNUSED(module), PyObject *num) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_complex complex; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     complex = PyComplex_AsCComplex(num); | 
					
						
							|  |  |  |     if (complex.real == -1. && PyErr_Occurred()) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return PyComplex_FromCComplex(_Py_c_neg(complex)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define _PY_C_FUNC2(suffix)                                      \
 | 
					
						
							|  |  |  |     static PyObject *                                            \ | 
					
						
							|  |  |  |     _py_c_##suffix(PyObject *Py_UNUSED(module), PyObject *args)  \ | 
					
						
							|  |  |  |     {                                                            \ | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  |         Py_complex a, b, res;                                    \ | 
					
						
							| 
									
										
										
										
											2023-11-28 11:18:33 +03:00
										 |  |  |                                                                  \ | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  |         if (!PyArg_ParseTuple(args, "DD", &a, &b)) {             \ | 
					
						
							| 
									
										
										
										
											2023-11-28 11:18:33 +03:00
										 |  |  |             return NULL;                                         \ | 
					
						
							|  |  |  |         }                                                        \ | 
					
						
							|  |  |  |                                                                  \ | 
					
						
							|  |  |  |         errno = 0;                                               \ | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  |         res = _Py_c_##suffix(a, b);                              \ | 
					
						
							|  |  |  |         return Py_BuildValue("Di", &res, errno);                 \ | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define _PY_CR_FUNC2(suffix)                                     \
 | 
					
						
							|  |  |  |     static PyObject *                                            \ | 
					
						
							|  |  |  |     _py_cr_##suffix(PyObject *Py_UNUSED(module), PyObject *args) \ | 
					
						
							|  |  |  |     {                                                            \ | 
					
						
							|  |  |  |         Py_complex a, res;                                       \ | 
					
						
							|  |  |  |         double b;                                                \ | 
					
						
							|  |  |  |                                                                  \ | 
					
						
							|  |  |  |         if (!PyArg_ParseTuple(args, "Dd", &a, &b)) {             \ | 
					
						
							|  |  |  |             return NULL;                                         \ | 
					
						
							|  |  |  |         }                                                        \ | 
					
						
							|  |  |  |                                                                  \ | 
					
						
							|  |  |  |         errno = 0;                                               \ | 
					
						
							|  |  |  |         res = _Py_cr_##suffix(a, b);                             \ | 
					
						
							|  |  |  |         return Py_BuildValue("Di", &res, errno);                 \ | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define _PY_RC_FUNC2(suffix)                                     \
 | 
					
						
							|  |  |  |     static PyObject *                                            \ | 
					
						
							|  |  |  |     _py_rc_##suffix(PyObject *Py_UNUSED(module), PyObject *args) \ | 
					
						
							|  |  |  |     {                                                            \ | 
					
						
							|  |  |  |         Py_complex b, res;                                       \ | 
					
						
							|  |  |  |         double a;                                                \ | 
					
						
							|  |  |  |                                                                  \ | 
					
						
							|  |  |  |         if (!PyArg_ParseTuple(args, "dD", &a, &b)) {             \ | 
					
						
							|  |  |  |             return NULL;                                         \ | 
					
						
							|  |  |  |         }                                                        \ | 
					
						
							|  |  |  |                                                                  \ | 
					
						
							|  |  |  |         errno = 0;                                               \ | 
					
						
							|  |  |  |         res = _Py_rc_##suffix(a, b);                             \ | 
					
						
							| 
									
										
										
										
											2023-11-28 11:18:33 +03:00
										 |  |  |         return Py_BuildValue("Di", &res, errno);                 \ | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | _PY_C_FUNC2(sum) | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  | _PY_CR_FUNC2(sum) | 
					
						
							| 
									
										
										
										
											2023-11-28 11:18:33 +03:00
										 |  |  | _PY_C_FUNC2(diff) | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  | _PY_CR_FUNC2(diff) | 
					
						
							|  |  |  | _PY_RC_FUNC2(diff) | 
					
						
							| 
									
										
										
										
											2023-11-28 11:18:33 +03:00
										 |  |  | _PY_C_FUNC2(prod) | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  | _PY_CR_FUNC2(prod) | 
					
						
							| 
									
										
										
										
											2023-11-28 11:18:33 +03:00
										 |  |  | _PY_C_FUNC2(quot) | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  | _PY_CR_FUNC2(quot) | 
					
						
							|  |  |  | _PY_RC_FUNC2(quot) | 
					
						
							| 
									
										
										
										
											2023-11-28 11:18:33 +03:00
										 |  |  | _PY_C_FUNC2(pow) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject* | 
					
						
							|  |  |  | _py_c_abs(PyObject *Py_UNUSED(module), PyObject* obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_complex complex; | 
					
						
							|  |  |  |     double res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     NULLABLE(obj); | 
					
						
							|  |  |  |     complex = PyComplex_AsCComplex(obj); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (complex.real == -1. && PyErr_Occurred()) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     errno = 0; | 
					
						
							|  |  |  |     res = _Py_c_abs(complex); | 
					
						
							|  |  |  |     return Py_BuildValue("di", res, errno); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-05 10:42:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-01 08:44:54 +03:00
										 |  |  | static PyMethodDef test_methods[] = { | 
					
						
							| 
									
										
										
										
											2023-11-05 10:42:42 +03:00
										 |  |  |     {"complex_fromccomplex", complex_fromccomplex, METH_O}, | 
					
						
							|  |  |  |     {"complex_asccomplex", complex_asccomplex, METH_O}, | 
					
						
							| 
									
										
										
										
											2023-11-28 11:18:33 +03:00
										 |  |  |     {"_py_c_sum", _py_c_sum, METH_VARARGS}, | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  |     {"_py_cr_sum", _py_cr_sum, METH_VARARGS}, | 
					
						
							| 
									
										
										
										
											2023-11-28 11:18:33 +03:00
										 |  |  |     {"_py_c_diff", _py_c_diff, METH_VARARGS}, | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  |     {"_py_cr_diff", _py_cr_diff, METH_VARARGS}, | 
					
						
							|  |  |  |     {"_py_rc_diff", _py_rc_diff, METH_VARARGS}, | 
					
						
							| 
									
										
										
										
											2023-11-28 11:18:33 +03:00
										 |  |  |     {"_py_c_neg", _py_c_neg, METH_O}, | 
					
						
							|  |  |  |     {"_py_c_prod", _py_c_prod, METH_VARARGS}, | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  |     {"_py_cr_prod", _py_cr_prod, METH_VARARGS}, | 
					
						
							| 
									
										
										
										
											2023-11-28 11:18:33 +03:00
										 |  |  |     {"_py_c_quot", _py_c_quot, METH_VARARGS}, | 
					
						
							| 
									
										
										
										
											2024-11-26 18:57:39 +03:00
										 |  |  |     {"_py_cr_quot", _py_cr_quot, METH_VARARGS}, | 
					
						
							|  |  |  |     {"_py_rc_quot", _py_rc_quot, METH_VARARGS}, | 
					
						
							| 
									
										
										
										
											2023-11-28 11:18:33 +03:00
										 |  |  |     {"_py_c_pow", _py_c_pow, METH_VARARGS}, | 
					
						
							|  |  |  |     {"_py_c_abs", _py_c_abs, METH_O}, | 
					
						
							| 
									
										
										
										
											2023-11-01 08:44:54 +03:00
										 |  |  |     {NULL}, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | _PyTestCapi_Init_Complex(PyObject *mod) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (PyModule_AddFunctions(mod, test_methods) < 0) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } |