gh-128813: hide mixed-mode functions for complex arithmetic from C-API (#131703)

This commit is contained in:
Sergey B Kirpichev 2025-04-22 15:18:18 +03:00 committed by GitHub
parent 5ffb89420c
commit 79f7c67bf6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 91 additions and 111 deletions

View File

@ -44,36 +44,12 @@ pointers. This is consistent throughout the API.
representation.
.. c:function:: Py_complex _Py_cr_sum(Py_complex left, double right)
Return the sum of a complex number and a real number, using the C :c:type:`Py_complex`
representation.
.. versionadded:: 3.14
.. c:function:: Py_complex _Py_c_diff(Py_complex left, Py_complex right)
Return the difference between two complex numbers, using the C
:c:type:`Py_complex` representation.
.. c:function:: Py_complex _Py_cr_diff(Py_complex left, double right)
Return the difference between a complex number and a real number, using the C
:c:type:`Py_complex` representation.
.. versionadded:: 3.14
.. c:function:: Py_complex _Py_rc_diff(double left, Py_complex right)
Return the difference between a real number and a complex number, using the C
:c:type:`Py_complex` representation.
.. versionadded:: 3.14
.. c:function:: Py_complex _Py_c_neg(Py_complex num)
Return the negation of the complex number *num*, using the C
@ -86,14 +62,6 @@ pointers. This is consistent throughout the API.
representation.
.. c:function:: Py_complex _Py_cr_prod(Py_complex left, double right)
Return the product of a complex number and a real number, using the C
:c:type:`Py_complex` representation.
.. versionadded:: 3.14
.. c:function:: Py_complex _Py_c_quot(Py_complex dividend, Py_complex divisor)
Return the quotient of two complex numbers, using the C :c:type:`Py_complex`
@ -103,28 +71,6 @@ pointers. This is consistent throughout the API.
:c:data:`errno` to :c:macro:`!EDOM`.
.. c:function:: Py_complex _Py_cr_quot(Py_complex dividend, double divisor)
Return the quotient of a complex number and a real number, using the C
:c:type:`Py_complex` representation.
If *divisor* is zero, this method returns zero and sets
:c:data:`errno` to :c:macro:`!EDOM`.
.. versionadded:: 3.14
.. c:function:: Py_complex _Py_rc_quot(double dividend, Py_complex divisor)
Return the quotient of a real number and a complex number, using the C
:c:type:`Py_complex` representation.
If *divisor* is zero, this method returns zero and sets
:c:data:`errno` to :c:macro:`!EDOM`.
.. versionadded:: 3.14
.. c:function:: Py_complex _Py_c_pow(Py_complex num, Py_complex exp)
Return the exponentiation of *num* by *exp*, using the C :c:type:`Py_complex`

View File

@ -9,16 +9,10 @@ typedef struct {
// Operations on complex numbers.
PyAPI_FUNC(Py_complex) _Py_c_sum(Py_complex, Py_complex);
PyAPI_FUNC(Py_complex) _Py_cr_sum(Py_complex, double);
PyAPI_FUNC(Py_complex) _Py_c_diff(Py_complex, Py_complex);
PyAPI_FUNC(Py_complex) _Py_cr_diff(Py_complex, double);
PyAPI_FUNC(Py_complex) _Py_rc_diff(double, Py_complex);
PyAPI_FUNC(Py_complex) _Py_c_neg(Py_complex);
PyAPI_FUNC(Py_complex) _Py_c_prod(Py_complex, Py_complex);
PyAPI_FUNC(Py_complex) _Py_cr_prod(Py_complex, double);
PyAPI_FUNC(Py_complex) _Py_c_quot(Py_complex, Py_complex);
PyAPI_FUNC(Py_complex) _Py_cr_quot(Py_complex, double);
PyAPI_FUNC(Py_complex) _Py_rc_quot(double, Py_complex);
PyAPI_FUNC(Py_complex) _Py_c_pow(Py_complex, Py_complex);
PyAPI_FUNC(double) _Py_c_abs(Py_complex);

View File

@ -19,6 +19,15 @@ extern int _PyComplex_FormatAdvancedWriter(
Py_ssize_t start,
Py_ssize_t end);
// Operations on complex numbers.
PyAPI_FUNC(Py_complex) _Py_cr_sum(Py_complex, double);
PyAPI_FUNC(Py_complex) _Py_cr_diff(Py_complex, double);
PyAPI_FUNC(Py_complex) _Py_rc_diff(double, Py_complex);
PyAPI_FUNC(Py_complex) _Py_cr_prod(Py_complex, double);
PyAPI_FUNC(Py_complex) _Py_cr_quot(Py_complex, double);
PyAPI_FUNC(Py_complex) _Py_rc_quot(double, Py_complex);
#ifdef __cplusplus
}
#endif

View File

@ -12,6 +12,7 @@
_testcapi = import_helper.import_module('_testcapi')
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
_testinternalcapi = import_helper.import_module('_testinternalcapi')
NULL = None
INF = float("inf")
@ -174,7 +175,7 @@ def test_py_c_sum(self):
def test_py_cr_sum(self):
# Test _Py_cr_sum()
_py_cr_sum = _testcapi._py_cr_sum
_py_cr_sum = _testinternalcapi._py_cr_sum
self.assertComplexesAreIdentical(_py_cr_sum(-0j, -0.0)[0],
complex(-0.0, -0.0))
@ -187,14 +188,14 @@ def test_py_c_diff(self):
def test_py_cr_diff(self):
# Test _Py_cr_diff()
_py_cr_diff = _testcapi._py_cr_diff
_py_cr_diff = _testinternalcapi._py_cr_diff
self.assertComplexesAreIdentical(_py_cr_diff(-0j, 0.0)[0],
complex(-0.0, -0.0))
def test_py_rc_diff(self):
# Test _Py_rc_diff()
_py_rc_diff = _testcapi._py_rc_diff
_py_rc_diff = _testinternalcapi._py_rc_diff
self.assertComplexesAreIdentical(_py_rc_diff(-0.0, 0j)[0],
complex(-0.0, -0.0))
@ -213,7 +214,7 @@ def test_py_c_prod(self):
def test_py_cr_prod(self):
# Test _Py_cr_prod()
_py_cr_prod = _testcapi._py_cr_prod
_py_cr_prod = _testinternalcapi._py_cr_prod
self.assertComplexesAreIdentical(_py_cr_prod(complex('inf+1j'), INF)[0],
complex('inf+infj'))
@ -242,14 +243,14 @@ def test_py_c_quot(self):
def test_py_cr_quot(self):
# Test _Py_cr_quot()
_py_cr_quot = _testcapi._py_cr_quot
_py_cr_quot = _testinternalcapi._py_cr_quot
self.assertComplexesAreIdentical(_py_cr_quot(complex('inf+1j'), 2**1000)[0],
INF + 2**-1000*1j)
def test_py_rc_quot(self):
# Test _Py_rc_quot()
_py_rc_quot = _testcapi._py_rc_quot
_py_rc_quot = _testinternalcapi._py_rc_quot
self.assertComplexesAreIdentical(_py_rc_quot(1.0, complex('nan-infj'))[0],
0j)

View File

@ -175,7 +175,7 @@
@MODULE_XXSUBTYPE_TRUE@xxsubtype xxsubtype.c
@MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c _testinternalcapi/complex.c
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c _testcapi/bytes.c _testcapi/object.c _testcapi/monitoring.c _testcapi/config.c _testcapi/import.c _testcapi/frame.c _testcapi/type.c _testcapi/function.c
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/codec.c _testlimitedcapi/complex.c _testlimitedcapi/dict.c _testlimitedcapi/eval.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/import.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/object.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/sys.c _testlimitedcapi/tuple.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c _testlimitedcapi/version.c _testlimitedcapi/file.c
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c

View File

@ -57,48 +57,10 @@ _py_c_neg(PyObject *Py_UNUSED(module), PyObject *num)
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); \
return Py_BuildValue("Di", &res, errno); \
};
_PY_C_FUNC2(sum)
_PY_CR_FUNC2(sum)
_PY_C_FUNC2(diff)
_PY_CR_FUNC2(diff)
_PY_RC_FUNC2(diff)
_PY_C_FUNC2(prod)
_PY_CR_FUNC2(prod)
_PY_C_FUNC2(quot)
_PY_CR_FUNC2(quot)
_PY_RC_FUNC2(quot)
_PY_C_FUNC2(pow)
static PyObject*
@ -124,16 +86,10 @@ static PyMethodDef test_methods[] = {
{"complex_fromccomplex", complex_fromccomplex, METH_O},
{"complex_asccomplex", complex_asccomplex, METH_O},
{"_py_c_sum", _py_c_sum, METH_VARARGS},
{"_py_cr_sum", _py_cr_sum, METH_VARARGS},
{"_py_c_diff", _py_c_diff, METH_VARARGS},
{"_py_cr_diff", _py_cr_diff, METH_VARARGS},
{"_py_rc_diff", _py_rc_diff, METH_VARARGS},
{"_py_c_neg", _py_c_neg, METH_O},
{"_py_c_prod", _py_c_prod, METH_VARARGS},
{"_py_cr_prod", _py_cr_prod, METH_VARARGS},
{"_py_c_quot", _py_c_quot, METH_VARARGS},
{"_py_cr_quot", _py_cr_quot, METH_VARARGS},
{"_py_rc_quot", _py_rc_quot, METH_VARARGS},
{"_py_c_pow", _py_c_pow, METH_VARARGS},
{"_py_c_abs", _py_c_abs, METH_O},
{NULL},

View File

@ -2118,6 +2118,9 @@ module_exec(PyObject *module)
if (_PyTestInternalCapi_Init_Set(module) < 0) {
return 1;
}
if (_PyTestInternalCapi_Init_Complex(module) < 0) {
return 1;
}
if (_PyTestInternalCapi_Init_CriticalSection(module) < 0) {
return 1;
}

View File

@ -0,0 +1,66 @@
#include "parts.h"
#include "../_testcapi/util.h"
#define Py_BUILD_CORE
#include "pycore_complexobject.h"
#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); \
return Py_BuildValue("Di", &res, errno); \
};
_PY_CR_FUNC2(sum)
_PY_CR_FUNC2(diff)
_PY_RC_FUNC2(diff)
_PY_CR_FUNC2(prod)
_PY_CR_FUNC2(quot)
_PY_RC_FUNC2(quot)
static PyMethodDef test_methods[] = {
{"_py_cr_sum", _py_cr_sum, METH_VARARGS},
{"_py_cr_diff", _py_cr_diff, METH_VARARGS},
{"_py_rc_diff", _py_rc_diff, METH_VARARGS},
{"_py_cr_prod", _py_cr_prod, METH_VARARGS},
{"_py_cr_quot", _py_cr_quot, METH_VARARGS},
{"_py_rc_quot", _py_rc_quot, METH_VARARGS},
{NULL},
};
int
_PyTestInternalCapi_Init_Complex(PyObject *mod)
{
if (PyModule_AddFunctions(mod, test_methods) < 0) {
return -1;
}
return 0;
}

View File

@ -13,6 +13,7 @@
int _PyTestInternalCapi_Init_Lock(PyObject *module);
int _PyTestInternalCapi_Init_PyTime(PyObject *module);
int _PyTestInternalCapi_Init_Set(PyObject *module);
int _PyTestInternalCapi_Init_Complex(PyObject *module);
int _PyTestInternalCapi_Init_CriticalSection(PyObject *module);
#endif // Py_TESTINTERNALCAPI_PARTS_H

View File

@ -98,6 +98,7 @@
<ClCompile Include="..\Modules\_testinternalcapi\test_critical_sections.c" />
<ClCompile Include="..\Modules\_testinternalcapi\test_lock.c" />
<ClCompile Include="..\Modules\_testinternalcapi\set.c" />
<ClCompile Include="..\Modules\_testinternalcapi\complex.c" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\PC\python_nt.rc" />

View File

@ -24,6 +24,9 @@
<ClCompile Include="..\Modules\_testinternalcapi\set.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Modules\_testinternalcapi\complex.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\PC\python_nt.rc">