Skip to content

Commit 61c659e

Browse files
authored
gh-116417: Move limited C API complex.c tests to _testlimitedcapi (#117014)
Split complex.c tests of _testcapi into two parts: limited C API tests in _testlimitedcapi and non-limited C API tests in _testcapi.
1 parent 7f64ae3 commit 61c659e

File tree

8 files changed

+92
-67
lines changed

8 files changed

+92
-67
lines changed

Lib/test/test_capi/test_complex.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
_testcapi = import_helper.import_module('_testcapi')
13+
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
1314

1415
NULL = None
1516
INF = float("inf")
@@ -25,7 +26,7 @@ def __complex__(self):
2526
class CAPIComplexTest(unittest.TestCase):
2627
def test_check(self):
2728
# Test PyComplex_Check()
28-
check = _testcapi.complex_check
29+
check = _testlimitedcapi.complex_check
2930

3031
self.assertTrue(check(1+2j))
3132
self.assertTrue(check(ComplexSubclass(1+2j)))
@@ -38,7 +39,7 @@ def test_check(self):
3839

3940
def test_checkexact(self):
4041
# PyComplex_CheckExact()
41-
checkexact = _testcapi.complex_checkexact
42+
checkexact = _testlimitedcapi.complex_checkexact
4243

4344
self.assertTrue(checkexact(1+2j))
4445
self.assertFalse(checkexact(ComplexSubclass(1+2j)))
@@ -57,13 +58,13 @@ def test_fromccomplex(self):
5758

5859
def test_fromdoubles(self):
5960
# Test PyComplex_FromDoubles()
60-
fromdoubles = _testcapi.complex_fromdoubles
61+
fromdoubles = _testlimitedcapi.complex_fromdoubles
6162

6263
self.assertEqual(fromdoubles(1.0, 2.0), 1.0+2.0j)
6364

6465
def test_realasdouble(self):
6566
# Test PyComplex_RealAsDouble()
66-
realasdouble = _testcapi.complex_realasdouble
67+
realasdouble = _testlimitedcapi.complex_realasdouble
6768

6869
self.assertEqual(realasdouble(1+2j), 1.0)
6970
self.assertEqual(realasdouble(-1+0j), -1.0)
@@ -98,7 +99,7 @@ def test_realasdouble(self):
9899

99100
def test_imagasdouble(self):
100101
# Test PyComplex_ImagAsDouble()
101-
imagasdouble = _testcapi.complex_imagasdouble
102+
imagasdouble = _testlimitedcapi.complex_imagasdouble
102103

103104
self.assertEqual(imagasdouble(1+2j), 2.0)
104105
self.assertEqual(imagasdouble(1-1j), -1.0)

Modules/Setup.stdlib.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@
163163
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
164164
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c
165165
@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/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c
166-
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/dict.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/sys.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c
166+
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/complex.c _testlimitedcapi/dict.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/sys.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c
167167
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
168168
@MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c
169169

Modules/_testcapi/complex.c

-61
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,6 @@
22
#include "util.h"
33

44

5-
static PyObject *
6-
complex_check(PyObject *Py_UNUSED(module), PyObject *obj)
7-
{
8-
NULLABLE(obj);
9-
return PyLong_FromLong(PyComplex_Check(obj));
10-
}
11-
12-
static PyObject *
13-
complex_checkexact(PyObject *Py_UNUSED(module), PyObject *obj)
14-
{
15-
NULLABLE(obj);
16-
return PyLong_FromLong(PyComplex_CheckExact(obj));
17-
}
18-
195
static PyObject *
206
complex_fromccomplex(PyObject *Py_UNUSED(module), PyObject *obj)
217
{
@@ -28,48 +14,6 @@ complex_fromccomplex(PyObject *Py_UNUSED(module), PyObject *obj)
2814
return PyComplex_FromCComplex(complex);
2915
}
3016

31-
static PyObject *
32-
complex_fromdoubles(PyObject *Py_UNUSED(module), PyObject *args)
33-
{
34-
double real, imag;
35-
36-
if (!PyArg_ParseTuple(args, "dd", &real, &imag)) {
37-
return NULL;
38-
}
39-
40-
return PyComplex_FromDoubles(real, imag);
41-
}
42-
43-
static PyObject *
44-
complex_realasdouble(PyObject *Py_UNUSED(module), PyObject *obj)
45-
{
46-
double real;
47-
48-
NULLABLE(obj);
49-
real = PyComplex_RealAsDouble(obj);
50-
51-
if (real == -1. && PyErr_Occurred()) {
52-
return NULL;
53-
}
54-
55-
return PyFloat_FromDouble(real);
56-
}
57-
58-
static PyObject *
59-
complex_imagasdouble(PyObject *Py_UNUSED(module), PyObject *obj)
60-
{
61-
double imag;
62-
63-
NULLABLE(obj);
64-
imag = PyComplex_ImagAsDouble(obj);
65-
66-
if (imag == -1. && PyErr_Occurred()) {
67-
return NULL;
68-
}
69-
70-
return PyFloat_FromDouble(imag);
71-
}
72-
7317
static PyObject *
7418
complex_asccomplex(PyObject *Py_UNUSED(module), PyObject *obj)
7519
{
@@ -139,12 +83,7 @@ _py_c_abs(PyObject *Py_UNUSED(module), PyObject* obj)
13983

14084

14185
static PyMethodDef test_methods[] = {
142-
{"complex_check", complex_check, METH_O},
143-
{"complex_checkexact", complex_checkexact, METH_O},
14486
{"complex_fromccomplex", complex_fromccomplex, METH_O},
145-
{"complex_fromdoubles", complex_fromdoubles, METH_VARARGS},
146-
{"complex_realasdouble", complex_realasdouble, METH_O},
147-
{"complex_imagasdouble", complex_imagasdouble, METH_O},
14887
{"complex_asccomplex", complex_asccomplex, METH_O},
14988
{"_py_c_sum", _py_c_sum, METH_VARARGS},
15089
{"_py_c_diff", _py_c_diff, METH_VARARGS},

Modules/_testlimitedcapi.c

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ PyInit__testlimitedcapi(void)
3535
if (_PyTestLimitedCAPI_Init_Bytes(mod) < 0) {
3636
return NULL;
3737
}
38+
if (_PyTestLimitedCAPI_Init_Complex(mod) < 0) {
39+
return NULL;
40+
}
3841
if (_PyTestLimitedCAPI_Init_Dict(mod) < 0) {
3942
return NULL;
4043
}

Modules/_testlimitedcapi/complex.c

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include "parts.h"
2+
#include "util.h"
3+
4+
5+
static PyObject *
6+
complex_check(PyObject *Py_UNUSED(module), PyObject *obj)
7+
{
8+
NULLABLE(obj);
9+
return PyLong_FromLong(PyComplex_Check(obj));
10+
}
11+
12+
static PyObject *
13+
complex_checkexact(PyObject *Py_UNUSED(module), PyObject *obj)
14+
{
15+
NULLABLE(obj);
16+
return PyLong_FromLong(PyComplex_CheckExact(obj));
17+
}
18+
19+
static PyObject *
20+
complex_fromdoubles(PyObject *Py_UNUSED(module), PyObject *args)
21+
{
22+
double real, imag;
23+
24+
if (!PyArg_ParseTuple(args, "dd", &real, &imag)) {
25+
return NULL;
26+
}
27+
28+
return PyComplex_FromDoubles(real, imag);
29+
}
30+
31+
static PyObject *
32+
complex_realasdouble(PyObject *Py_UNUSED(module), PyObject *obj)
33+
{
34+
double real;
35+
36+
NULLABLE(obj);
37+
real = PyComplex_RealAsDouble(obj);
38+
39+
if (real == -1. && PyErr_Occurred()) {
40+
return NULL;
41+
}
42+
43+
return PyFloat_FromDouble(real);
44+
}
45+
46+
static PyObject *
47+
complex_imagasdouble(PyObject *Py_UNUSED(module), PyObject *obj)
48+
{
49+
double imag;
50+
51+
NULLABLE(obj);
52+
imag = PyComplex_ImagAsDouble(obj);
53+
54+
if (imag == -1. && PyErr_Occurred()) {
55+
return NULL;
56+
}
57+
58+
return PyFloat_FromDouble(imag);
59+
}
60+
61+
62+
static PyMethodDef test_methods[] = {
63+
{"complex_check", complex_check, METH_O},
64+
{"complex_checkexact", complex_checkexact, METH_O},
65+
{"complex_fromdoubles", complex_fromdoubles, METH_VARARGS},
66+
{"complex_realasdouble", complex_realasdouble, METH_O},
67+
{"complex_imagasdouble", complex_imagasdouble, METH_O},
68+
{NULL},
69+
};
70+
71+
int
72+
_PyTestLimitedCAPI_Init_Complex(PyObject *mod)
73+
{
74+
if (PyModule_AddFunctions(mod, test_methods) < 0) {
75+
return -1;
76+
}
77+
78+
return 0;
79+
}

Modules/_testlimitedcapi/parts.h

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
int _PyTestLimitedCAPI_Init_Abstract(PyObject *module);
2626
int _PyTestLimitedCAPI_Init_ByteArray(PyObject *module);
2727
int _PyTestLimitedCAPI_Init_Bytes(PyObject *module);
28+
int _PyTestLimitedCAPI_Init_Complex(PyObject *module);
2829
int _PyTestLimitedCAPI_Init_Dict(PyObject *module);
2930
int _PyTestLimitedCAPI_Init_Float(PyObject *module);
3031
int _PyTestLimitedCAPI_Init_HeaptypeRelative(PyObject *module);

PCbuild/_testlimitedcapi.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<ClCompile Include="..\Modules\_testlimitedcapi\abstract.c" />
9898
<ClCompile Include="..\Modules\_testlimitedcapi\bytearray.c" />
9999
<ClCompile Include="..\Modules\_testlimitedcapi\bytes.c" />
100+
<ClCompile Include="..\Modules\_testlimitedcapi\complex.c" />
100101
<ClCompile Include="..\Modules\_testlimitedcapi\dict.c" />
101102
<ClCompile Include="..\Modules\_testlimitedcapi\float.c" />
102103
<ClCompile Include="..\Modules\_testlimitedcapi\heaptype_relative.c" />

PCbuild/_testlimitedcapi.vcxproj.filters

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<ClCompile Include="..\Modules\_testlimitedcapi\abstract.c" />
1313
<ClCompile Include="..\Modules\_testlimitedcapi\bytearray.c" />
1414
<ClCompile Include="..\Modules\_testlimitedcapi\bytes.c" />
15+
<ClCompile Include="..\Modules\_testlimitedcapi\complex.c" />
1516
<ClCompile Include="..\Modules\_testlimitedcapi\dict.c" />
1617
<ClCompile Include="..\Modules\_testlimitedcapi\float.c" />
1718
<ClCompile Include="..\Modules\_testlimitedcapi\heaptype_relative.c" />

0 commit comments

Comments
 (0)