]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-128813: hide mixed-mode functions for complex arithmetic from C-API (#131703)
authorSergey B Kirpichev <skirpichev@gmail.com>
Tue, 22 Apr 2025 12:18:18 +0000 (15:18 +0300)
committerGitHub <noreply@github.com>
Tue, 22 Apr 2025 12:18:18 +0000 (14:18 +0200)
Doc/c-api/complex.rst
Include/cpython/complexobject.h
Include/internal/pycore_complexobject.h
Lib/test/test_capi/test_complex.py
Modules/Setup.stdlib.in
Modules/_testcapi/complex.c
Modules/_testinternalcapi.c
Modules/_testinternalcapi/complex.c [new file with mode: 0644]
Modules/_testinternalcapi/parts.h
PCbuild/_testinternalcapi.vcxproj
PCbuild/_testinternalcapi.vcxproj.filters

index d1f5d8eda676ef52da1a789ae849edf705b13d2c..16bd79475dc1e66ea6fffd3ef3844dd3f1ce1f43 100644 (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`
index 28576afad0b6b5b3f01c22bafa6db8ea839446e8..fbdc6a91fe895c0f7af0bff6a3836a1991709c20 100644 (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);
 
index 54713536eedc462ce82400fd0fa0767dd9c0751d..f595f6ab7a674d18533a716cbb4e12d342dfe70d 100644 (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
index 97e0eb3f0430809e2017ad71d6939d3ab149960b..c3189a67cc7e2d3058731c6400087d0ffe86963d 100644 (file)
@@ -12,6 +12,7 @@ from test.support.testcase import ComplexesAreIdenticalMixin
 
 _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 @@ class CAPIComplexTest(ComplexesAreIdenticalMixin, unittest.TestCase):
 
     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 @@ class CAPIComplexTest(ComplexesAreIdenticalMixin, unittest.TestCase):
 
     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 @@ class CAPIComplexTest(ComplexesAreIdenticalMixin, unittest.TestCase):
 
     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 @@ class CAPIComplexTest(ComplexesAreIdenticalMixin, unittest.TestCase):
 
     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)
index 0774e7ddab844cd3eb7ebb8ec3cad020d7d978bc..33e60f37d19922bb1233ab92ffcc63f611d32478 100644 (file)
 @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
index b726cd3236f1792f7510e88b0481c71795e84239..fb5234d03cf067627d45e2edd8e5aaf8207ad86f 100644 (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},
index 1858ce2b00c8243f66e6dc7c7812ec0019da0751..f91470837edb1ca1746b537dbeb744c6dd7c3b34 100644 (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;
     }
diff --git a/Modules/_testinternalcapi/complex.c b/Modules/_testinternalcapi/complex.c
new file mode 100644 (file)
index 0000000..50080d5
--- /dev/null
@@ -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;
+}
index 49a1395f499fc3fb0db184b32cb19193c8387691..03557d5bf5957fca32d860d5e2217c38cc063ab4 100644 (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
index 87db569423de2a2a070259e354cd9a6bd26af9d7..f7e050b7c7418098129f06d09f70c5048a7e2acc 100644 (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" />
index eb0a83ba17f8158df8a8743142de3aabd14090a4..012d709bd1ce5ddc606b611a04e0bf1e2bf57145 100644 (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">