Skip to content

Commit 79f7c67

Browse files
authored
gh-128813: hide mixed-mode functions for complex arithmetic from C-API (#131703)
1 parent 5ffb894 commit 79f7c67

File tree

11 files changed

+91
-111
lines changed

11 files changed

+91
-111
lines changed

Diff for: Doc/c-api/complex.rst

-54
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,12 @@ pointers. This is consistent throughout the API.
4444
representation.
4545
4646
47-
.. c:function:: Py_complex _Py_cr_sum(Py_complex left, double right)
48-
49-
Return the sum of a complex number and a real number, using the C :c:type:`Py_complex`
50-
representation.
51-
52-
.. versionadded:: 3.14
53-
54-
5547
.. c:function:: Py_complex _Py_c_diff(Py_complex left, Py_complex right)
5648
5749
Return the difference between two complex numbers, using the C
5850
:c:type:`Py_complex` representation.
5951
6052
61-
.. c:function:: Py_complex _Py_cr_diff(Py_complex left, double right)
62-
63-
Return the difference between a complex number and a real number, using the C
64-
:c:type:`Py_complex` representation.
65-
66-
.. versionadded:: 3.14
67-
68-
69-
.. c:function:: Py_complex _Py_rc_diff(double left, Py_complex right)
70-
71-
Return the difference between a real number and a complex number, using the C
72-
:c:type:`Py_complex` representation.
73-
74-
.. versionadded:: 3.14
75-
76-
7753
.. c:function:: Py_complex _Py_c_neg(Py_complex num)
7854
7955
Return the negation of the complex number *num*, using the C
@@ -86,14 +62,6 @@ pointers. This is consistent throughout the API.
8662
representation.
8763
8864
89-
.. c:function:: Py_complex _Py_cr_prod(Py_complex left, double right)
90-
91-
Return the product of a complex number and a real number, using the C
92-
:c:type:`Py_complex` representation.
93-
94-
.. versionadded:: 3.14
95-
96-
9765
.. c:function:: Py_complex _Py_c_quot(Py_complex dividend, Py_complex divisor)
9866
9967
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.
10371
:c:data:`errno` to :c:macro:`!EDOM`.
10472
10573
106-
.. c:function:: Py_complex _Py_cr_quot(Py_complex dividend, double divisor)
107-
108-
Return the quotient of a complex number and a real number, using the C
109-
:c:type:`Py_complex` representation.
110-
111-
If *divisor* is zero, this method returns zero and sets
112-
:c:data:`errno` to :c:macro:`!EDOM`.
113-
114-
.. versionadded:: 3.14
115-
116-
117-
.. c:function:: Py_complex _Py_rc_quot(double dividend, Py_complex divisor)
118-
119-
Return the quotient of a real number and a complex number, using the C
120-
:c:type:`Py_complex` representation.
121-
122-
If *divisor* is zero, this method returns zero and sets
123-
:c:data:`errno` to :c:macro:`!EDOM`.
124-
125-
.. versionadded:: 3.14
126-
127-
12874
.. c:function:: Py_complex _Py_c_pow(Py_complex num, Py_complex exp)
12975
13076
Return the exponentiation of *num* by *exp*, using the C :c:type:`Py_complex`

Diff for: Include/cpython/complexobject.h

-6
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,10 @@ typedef struct {
99

1010
// Operations on complex numbers.
1111
PyAPI_FUNC(Py_complex) _Py_c_sum(Py_complex, Py_complex);
12-
PyAPI_FUNC(Py_complex) _Py_cr_sum(Py_complex, double);
1312
PyAPI_FUNC(Py_complex) _Py_c_diff(Py_complex, Py_complex);
14-
PyAPI_FUNC(Py_complex) _Py_cr_diff(Py_complex, double);
15-
PyAPI_FUNC(Py_complex) _Py_rc_diff(double, Py_complex);
1613
PyAPI_FUNC(Py_complex) _Py_c_neg(Py_complex);
1714
PyAPI_FUNC(Py_complex) _Py_c_prod(Py_complex, Py_complex);
18-
PyAPI_FUNC(Py_complex) _Py_cr_prod(Py_complex, double);
1915
PyAPI_FUNC(Py_complex) _Py_c_quot(Py_complex, Py_complex);
20-
PyAPI_FUNC(Py_complex) _Py_cr_quot(Py_complex, double);
21-
PyAPI_FUNC(Py_complex) _Py_rc_quot(double, Py_complex);
2216
PyAPI_FUNC(Py_complex) _Py_c_pow(Py_complex, Py_complex);
2317
PyAPI_FUNC(double) _Py_c_abs(Py_complex);
2418

Diff for: Include/internal/pycore_complexobject.h

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ extern int _PyComplex_FormatAdvancedWriter(
1919
Py_ssize_t start,
2020
Py_ssize_t end);
2121

22+
// Operations on complex numbers.
23+
PyAPI_FUNC(Py_complex) _Py_cr_sum(Py_complex, double);
24+
PyAPI_FUNC(Py_complex) _Py_cr_diff(Py_complex, double);
25+
PyAPI_FUNC(Py_complex) _Py_rc_diff(double, Py_complex);
26+
PyAPI_FUNC(Py_complex) _Py_cr_prod(Py_complex, double);
27+
PyAPI_FUNC(Py_complex) _Py_cr_quot(Py_complex, double);
28+
PyAPI_FUNC(Py_complex) _Py_rc_quot(double, Py_complex);
29+
30+
2231
#ifdef __cplusplus
2332
}
2433
#endif

Diff for: Lib/test/test_capi/test_complex.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
_testcapi = import_helper.import_module('_testcapi')
1414
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
15+
_testinternalcapi = import_helper.import_module('_testinternalcapi')
1516

1617
NULL = None
1718
INF = float("inf")
@@ -174,7 +175,7 @@ def test_py_c_sum(self):
174175

175176
def test_py_cr_sum(self):
176177
# Test _Py_cr_sum()
177-
_py_cr_sum = _testcapi._py_cr_sum
178+
_py_cr_sum = _testinternalcapi._py_cr_sum
178179

179180
self.assertComplexesAreIdentical(_py_cr_sum(-0j, -0.0)[0],
180181
complex(-0.0, -0.0))
@@ -187,14 +188,14 @@ def test_py_c_diff(self):
187188

188189
def test_py_cr_diff(self):
189190
# Test _Py_cr_diff()
190-
_py_cr_diff = _testcapi._py_cr_diff
191+
_py_cr_diff = _testinternalcapi._py_cr_diff
191192

192193
self.assertComplexesAreIdentical(_py_cr_diff(-0j, 0.0)[0],
193194
complex(-0.0, -0.0))
194195

195196
def test_py_rc_diff(self):
196197
# Test _Py_rc_diff()
197-
_py_rc_diff = _testcapi._py_rc_diff
198+
_py_rc_diff = _testinternalcapi._py_rc_diff
198199

199200
self.assertComplexesAreIdentical(_py_rc_diff(-0.0, 0j)[0],
200201
complex(-0.0, -0.0))
@@ -213,7 +214,7 @@ def test_py_c_prod(self):
213214

214215
def test_py_cr_prod(self):
215216
# Test _Py_cr_prod()
216-
_py_cr_prod = _testcapi._py_cr_prod
217+
_py_cr_prod = _testinternalcapi._py_cr_prod
217218

218219
self.assertComplexesAreIdentical(_py_cr_prod(complex('inf+1j'), INF)[0],
219220
complex('inf+infj'))
@@ -242,14 +243,14 @@ def test_py_c_quot(self):
242243

243244
def test_py_cr_quot(self):
244245
# Test _Py_cr_quot()
245-
_py_cr_quot = _testcapi._py_cr_quot
246+
_py_cr_quot = _testinternalcapi._py_cr_quot
246247

247248
self.assertComplexesAreIdentical(_py_cr_quot(complex('inf+1j'), 2**1000)[0],
248249
INF + 2**-1000*1j)
249250

250251
def test_py_rc_quot(self):
251252
# Test _Py_rc_quot()
252-
_py_rc_quot = _testcapi._py_rc_quot
253+
_py_rc_quot = _testinternalcapi._py_rc_quot
253254

254255
self.assertComplexesAreIdentical(_py_rc_quot(1.0, complex('nan-infj'))[0],
255256
0j)

Diff for: Modules/Setup.stdlib.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
@MODULE_XXSUBTYPE_TRUE@xxsubtype xxsubtype.c
176176
@MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c
177177
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
178-
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c
178+
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c _testinternalcapi/complex.c
179179
@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
180180
@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
181181
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c

Diff for: Modules/_testcapi/complex.c

-44
Original file line numberDiff line numberDiff line change
@@ -57,48 +57,10 @@ _py_c_neg(PyObject *Py_UNUSED(module), PyObject *num)
5757
return Py_BuildValue("Di", &res, errno); \
5858
};
5959

60-
#define _PY_CR_FUNC2(suffix) \
61-
static PyObject * \
62-
_py_cr_##suffix(PyObject *Py_UNUSED(module), PyObject *args) \
63-
{ \
64-
Py_complex a, res; \
65-
double b; \
66-
\
67-
if (!PyArg_ParseTuple(args, "Dd", &a, &b)) { \
68-
return NULL; \
69-
} \
70-
\
71-
errno = 0; \
72-
res = _Py_cr_##suffix(a, b); \
73-
return Py_BuildValue("Di", &res, errno); \
74-
};
75-
76-
#define _PY_RC_FUNC2(suffix) \
77-
static PyObject * \
78-
_py_rc_##suffix(PyObject *Py_UNUSED(module), PyObject *args) \
79-
{ \
80-
Py_complex b, res; \
81-
double a; \
82-
\
83-
if (!PyArg_ParseTuple(args, "dD", &a, &b)) { \
84-
return NULL; \
85-
} \
86-
\
87-
errno = 0; \
88-
res = _Py_rc_##suffix(a, b); \
89-
return Py_BuildValue("Di", &res, errno); \
90-
};
91-
9260
_PY_C_FUNC2(sum)
93-
_PY_CR_FUNC2(sum)
9461
_PY_C_FUNC2(diff)
95-
_PY_CR_FUNC2(diff)
96-
_PY_RC_FUNC2(diff)
9762
_PY_C_FUNC2(prod)
98-
_PY_CR_FUNC2(prod)
9963
_PY_C_FUNC2(quot)
100-
_PY_CR_FUNC2(quot)
101-
_PY_RC_FUNC2(quot)
10264
_PY_C_FUNC2(pow)
10365

10466
static PyObject*
@@ -124,16 +86,10 @@ static PyMethodDef test_methods[] = {
12486
{"complex_fromccomplex", complex_fromccomplex, METH_O},
12587
{"complex_asccomplex", complex_asccomplex, METH_O},
12688
{"_py_c_sum", _py_c_sum, METH_VARARGS},
127-
{"_py_cr_sum", _py_cr_sum, METH_VARARGS},
12889
{"_py_c_diff", _py_c_diff, METH_VARARGS},
129-
{"_py_cr_diff", _py_cr_diff, METH_VARARGS},
130-
{"_py_rc_diff", _py_rc_diff, METH_VARARGS},
13190
{"_py_c_neg", _py_c_neg, METH_O},
13291
{"_py_c_prod", _py_c_prod, METH_VARARGS},
133-
{"_py_cr_prod", _py_cr_prod, METH_VARARGS},
13492
{"_py_c_quot", _py_c_quot, METH_VARARGS},
135-
{"_py_cr_quot", _py_cr_quot, METH_VARARGS},
136-
{"_py_rc_quot", _py_rc_quot, METH_VARARGS},
13793
{"_py_c_pow", _py_c_pow, METH_VARARGS},
13894
{"_py_c_abs", _py_c_abs, METH_O},
13995
{NULL},

Diff for: Modules/_testinternalcapi.c

+3
Original file line numberDiff line numberDiff line change
@@ -2118,6 +2118,9 @@ module_exec(PyObject *module)
21182118
if (_PyTestInternalCapi_Init_Set(module) < 0) {
21192119
return 1;
21202120
}
2121+
if (_PyTestInternalCapi_Init_Complex(module) < 0) {
2122+
return 1;
2123+
}
21212124
if (_PyTestInternalCapi_Init_CriticalSection(module) < 0) {
21222125
return 1;
21232126
}

Diff for: Modules/_testinternalcapi/complex.c

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "parts.h"
2+
#include "../_testcapi/util.h"
3+
4+
#define Py_BUILD_CORE
5+
#include "pycore_complexobject.h"
6+
7+
8+
#define _PY_CR_FUNC2(suffix) \
9+
static PyObject * \
10+
_py_cr_##suffix(PyObject *Py_UNUSED(module), PyObject *args) \
11+
{ \
12+
Py_complex a, res; \
13+
double b; \
14+
\
15+
if (!PyArg_ParseTuple(args, "Dd", &a, &b)) { \
16+
return NULL; \
17+
} \
18+
\
19+
errno = 0; \
20+
res = _Py_cr_##suffix(a, b); \
21+
return Py_BuildValue("Di", &res, errno); \
22+
};
23+
24+
#define _PY_RC_FUNC2(suffix) \
25+
static PyObject * \
26+
_py_rc_##suffix(PyObject *Py_UNUSED(module), PyObject *args) \
27+
{ \
28+
Py_complex b, res; \
29+
double a; \
30+
\
31+
if (!PyArg_ParseTuple(args, "dD", &a, &b)) { \
32+
return NULL; \
33+
} \
34+
\
35+
errno = 0; \
36+
res = _Py_rc_##suffix(a, b); \
37+
return Py_BuildValue("Di", &res, errno); \
38+
};
39+
40+
_PY_CR_FUNC2(sum)
41+
_PY_CR_FUNC2(diff)
42+
_PY_RC_FUNC2(diff)
43+
_PY_CR_FUNC2(prod)
44+
_PY_CR_FUNC2(quot)
45+
_PY_RC_FUNC2(quot)
46+
47+
48+
static PyMethodDef test_methods[] = {
49+
{"_py_cr_sum", _py_cr_sum, METH_VARARGS},
50+
{"_py_cr_diff", _py_cr_diff, METH_VARARGS},
51+
{"_py_rc_diff", _py_rc_diff, METH_VARARGS},
52+
{"_py_cr_prod", _py_cr_prod, METH_VARARGS},
53+
{"_py_cr_quot", _py_cr_quot, METH_VARARGS},
54+
{"_py_rc_quot", _py_rc_quot, METH_VARARGS},
55+
{NULL},
56+
};
57+
58+
int
59+
_PyTestInternalCapi_Init_Complex(PyObject *mod)
60+
{
61+
if (PyModule_AddFunctions(mod, test_methods) < 0) {
62+
return -1;
63+
}
64+
65+
return 0;
66+
}

Diff for: Modules/_testinternalcapi/parts.h

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
int _PyTestInternalCapi_Init_Lock(PyObject *module);
1414
int _PyTestInternalCapi_Init_PyTime(PyObject *module);
1515
int _PyTestInternalCapi_Init_Set(PyObject *module);
16+
int _PyTestInternalCapi_Init_Complex(PyObject *module);
1617
int _PyTestInternalCapi_Init_CriticalSection(PyObject *module);
1718

1819
#endif // Py_TESTINTERNALCAPI_PARTS_H

Diff for: PCbuild/_testinternalcapi.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
<ClCompile Include="..\Modules\_testinternalcapi\test_critical_sections.c" />
9999
<ClCompile Include="..\Modules\_testinternalcapi\test_lock.c" />
100100
<ClCompile Include="..\Modules\_testinternalcapi\set.c" />
101+
<ClCompile Include="..\Modules\_testinternalcapi\complex.c" />
101102
</ItemGroup>
102103
<ItemGroup>
103104
<ResourceCompile Include="..\PC\python_nt.rc" />

Diff for: PCbuild/_testinternalcapi.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<ClCompile Include="..\Modules\_testinternalcapi\set.c">
2525
<Filter>Source Files</Filter>
2626
</ClCompile>
27+
<ClCompile Include="..\Modules\_testinternalcapi\complex.c">
28+
<Filter>Source Files</Filter>
29+
</ClCompile>
2730
</ItemGroup>
2831
<ItemGroup>
2932
<ResourceCompile Include="..\PC\python_nt.rc">

0 commit comments

Comments
 (0)