Skip to content

Commit

Permalink
pythongh-116417: Move limited C API complex.c tests to _testlimitedca…
Browse files Browse the repository at this point in the history
…pi (python#117014)

Split complex.c tests of _testcapi into two parts: limited C API
tests in _testlimitedcapi and non-limited C API tests in _testcapi.
  • Loading branch information
vstinner authored and diegorusso committed Apr 17, 2024
1 parent b30f7e6 commit 34fac11
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 67 deletions.
11 changes: 6 additions & 5 deletions Lib/test/test_capi/test_complex.py
Expand Up @@ -10,6 +10,7 @@


_testcapi = import_helper.import_module('_testcapi')
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')

NULL = None
INF = float("inf")
Expand All @@ -25,7 +26,7 @@ def __complex__(self):
class CAPIComplexTest(unittest.TestCase):
def test_check(self):
# Test PyComplex_Check()
check = _testcapi.complex_check
check = _testlimitedcapi.complex_check

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

def test_checkexact(self):
# PyComplex_CheckExact()
checkexact = _testcapi.complex_checkexact
checkexact = _testlimitedcapi.complex_checkexact

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

def test_fromdoubles(self):
# Test PyComplex_FromDoubles()
fromdoubles = _testcapi.complex_fromdoubles
fromdoubles = _testlimitedcapi.complex_fromdoubles

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

def test_realasdouble(self):
# Test PyComplex_RealAsDouble()
realasdouble = _testcapi.complex_realasdouble
realasdouble = _testlimitedcapi.complex_realasdouble

self.assertEqual(realasdouble(1+2j), 1.0)
self.assertEqual(realasdouble(-1+0j), -1.0)
Expand Down Expand Up @@ -98,7 +99,7 @@ def test_realasdouble(self):

def test_imagasdouble(self):
# Test PyComplex_ImagAsDouble()
imagasdouble = _testcapi.complex_imagasdouble
imagasdouble = _testlimitedcapi.complex_imagasdouble

self.assertEqual(imagasdouble(1+2j), 2.0)
self.assertEqual(imagasdouble(1-1j), -1.0)
Expand Down
2 changes: 1 addition & 1 deletion Modules/Setup.stdlib.in
Expand Up @@ -163,7 +163,7 @@
@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__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
@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
@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
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
@MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c

Expand Down
61 changes: 0 additions & 61 deletions Modules/_testcapi/complex.c
Expand Up @@ -2,20 +2,6 @@
#include "util.h"


static PyObject *
complex_check(PyObject *Py_UNUSED(module), PyObject *obj)
{
NULLABLE(obj);
return PyLong_FromLong(PyComplex_Check(obj));
}

static PyObject *
complex_checkexact(PyObject *Py_UNUSED(module), PyObject *obj)
{
NULLABLE(obj);
return PyLong_FromLong(PyComplex_CheckExact(obj));
}

static PyObject *
complex_fromccomplex(PyObject *Py_UNUSED(module), PyObject *obj)
{
Expand All @@ -28,48 +14,6 @@ complex_fromccomplex(PyObject *Py_UNUSED(module), PyObject *obj)
return PyComplex_FromCComplex(complex);
}

static PyObject *
complex_fromdoubles(PyObject *Py_UNUSED(module), PyObject *args)
{
double real, imag;

if (!PyArg_ParseTuple(args, "dd", &real, &imag)) {
return NULL;
}

return PyComplex_FromDoubles(real, imag);
}

static PyObject *
complex_realasdouble(PyObject *Py_UNUSED(module), PyObject *obj)
{
double real;

NULLABLE(obj);
real = PyComplex_RealAsDouble(obj);

if (real == -1. && PyErr_Occurred()) {
return NULL;
}

return PyFloat_FromDouble(real);
}

static PyObject *
complex_imagasdouble(PyObject *Py_UNUSED(module), PyObject *obj)
{
double imag;

NULLABLE(obj);
imag = PyComplex_ImagAsDouble(obj);

if (imag == -1. && PyErr_Occurred()) {
return NULL;
}

return PyFloat_FromDouble(imag);
}

static PyObject *
complex_asccomplex(PyObject *Py_UNUSED(module), PyObject *obj)
{
Expand Down Expand Up @@ -139,12 +83,7 @@ _py_c_abs(PyObject *Py_UNUSED(module), PyObject* obj)


static PyMethodDef test_methods[] = {
{"complex_check", complex_check, METH_O},
{"complex_checkexact", complex_checkexact, METH_O},
{"complex_fromccomplex", complex_fromccomplex, METH_O},
{"complex_fromdoubles", complex_fromdoubles, METH_VARARGS},
{"complex_realasdouble", complex_realasdouble, METH_O},
{"complex_imagasdouble", complex_imagasdouble, METH_O},
{"complex_asccomplex", complex_asccomplex, METH_O},
{"_py_c_sum", _py_c_sum, METH_VARARGS},
{"_py_c_diff", _py_c_diff, METH_VARARGS},
Expand Down
3 changes: 3 additions & 0 deletions Modules/_testlimitedcapi.c
Expand Up @@ -35,6 +35,9 @@ PyInit__testlimitedcapi(void)
if (_PyTestLimitedCAPI_Init_Bytes(mod) < 0) {
return NULL;
}
if (_PyTestLimitedCAPI_Init_Complex(mod) < 0) {
return NULL;
}
if (_PyTestLimitedCAPI_Init_Dict(mod) < 0) {
return NULL;
}
Expand Down
79 changes: 79 additions & 0 deletions Modules/_testlimitedcapi/complex.c
@@ -0,0 +1,79 @@
#include "parts.h"
#include "util.h"


static PyObject *
complex_check(PyObject *Py_UNUSED(module), PyObject *obj)
{
NULLABLE(obj);
return PyLong_FromLong(PyComplex_Check(obj));
}

static PyObject *
complex_checkexact(PyObject *Py_UNUSED(module), PyObject *obj)
{
NULLABLE(obj);
return PyLong_FromLong(PyComplex_CheckExact(obj));
}

static PyObject *
complex_fromdoubles(PyObject *Py_UNUSED(module), PyObject *args)
{
double real, imag;

if (!PyArg_ParseTuple(args, "dd", &real, &imag)) {
return NULL;
}

return PyComplex_FromDoubles(real, imag);
}

static PyObject *
complex_realasdouble(PyObject *Py_UNUSED(module), PyObject *obj)
{
double real;

NULLABLE(obj);
real = PyComplex_RealAsDouble(obj);

if (real == -1. && PyErr_Occurred()) {
return NULL;
}

return PyFloat_FromDouble(real);
}

static PyObject *
complex_imagasdouble(PyObject *Py_UNUSED(module), PyObject *obj)
{
double imag;

NULLABLE(obj);
imag = PyComplex_ImagAsDouble(obj);

if (imag == -1. && PyErr_Occurred()) {
return NULL;
}

return PyFloat_FromDouble(imag);
}


static PyMethodDef test_methods[] = {
{"complex_check", complex_check, METH_O},
{"complex_checkexact", complex_checkexact, METH_O},
{"complex_fromdoubles", complex_fromdoubles, METH_VARARGS},
{"complex_realasdouble", complex_realasdouble, METH_O},
{"complex_imagasdouble", complex_imagasdouble, METH_O},
{NULL},
};

int
_PyTestLimitedCAPI_Init_Complex(PyObject *mod)
{
if (PyModule_AddFunctions(mod, test_methods) < 0) {
return -1;
}

return 0;
}
1 change: 1 addition & 0 deletions Modules/_testlimitedcapi/parts.h
Expand Up @@ -25,6 +25,7 @@
int _PyTestLimitedCAPI_Init_Abstract(PyObject *module);
int _PyTestLimitedCAPI_Init_ByteArray(PyObject *module);
int _PyTestLimitedCAPI_Init_Bytes(PyObject *module);
int _PyTestLimitedCAPI_Init_Complex(PyObject *module);
int _PyTestLimitedCAPI_Init_Dict(PyObject *module);
int _PyTestLimitedCAPI_Init_Float(PyObject *module);
int _PyTestLimitedCAPI_Init_HeaptypeRelative(PyObject *module);
Expand Down
1 change: 1 addition & 0 deletions PCbuild/_testlimitedcapi.vcxproj
Expand Up @@ -97,6 +97,7 @@
<ClCompile Include="..\Modules\_testlimitedcapi\abstract.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\bytearray.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\bytes.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\complex.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\dict.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\float.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\heaptype_relative.c" />
Expand Down
1 change: 1 addition & 0 deletions PCbuild/_testlimitedcapi.vcxproj.filters
Expand Up @@ -12,6 +12,7 @@
<ClCompile Include="..\Modules\_testlimitedcapi\abstract.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\bytearray.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\bytes.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\complex.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\dict.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\float.c" />
<ClCompile Include="..\Modules\_testlimitedcapi\heaptype_relative.c" />
Expand Down

0 comments on commit 34fac11

Please sign in to comment.