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.
This commit is contained in:
Victor Stinner 2024-03-19 17:23:12 +01:00 committed by GitHub
parent 7f64ae30dd
commit 61c659e2dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 92 additions and 67 deletions

View File

@ -10,6 +10,7 @@ from test.support import import_helper
_testcapi = import_helper.import_module('_testcapi')
_testlimitedcapi = import_helper.import_module('_testlimitedcapi')
NULL = None
INF = float("inf")
@ -25,7 +26,7 @@ class BadComplex3:
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)))
@ -38,7 +39,7 @@ class CAPIComplexTest(unittest.TestCase):
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)))
@ -57,13 +58,13 @@ class CAPIComplexTest(unittest.TestCase):
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)
@ -98,7 +99,7 @@ class CAPIComplexTest(unittest.TestCase):
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)

View File

@ -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

View File

@ -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)
{
@ -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)
{
@ -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},

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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" />

View File

@ -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" />