Skip to content

Commit dc4e778

Browse files
committed
gh-131704: add PyComplex_FromString() function
1 parent e329f74 commit dc4e778

File tree

9 files changed

+40
-0
lines changed

9 files changed

+40
-0
lines changed

Doc/c-api/complex.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ Complex Numbers as Python Objects
207207
.. versionchanged:: 3.13
208208
Use :meth:`~object.__complex__` if available.
209209
210+
.. c:function:: PyObject* PyComplex_FromString(PyObject *str)
211+
212+
Create a Python complex number object from the string value in *str* or
213+
return ``NULL`` with an exception set on error.
214+
215+
.. versionadded:: next
216+
210217
.. c:function:: Py_complex PyComplex_AsCComplex(PyObject *op)
211218
212219
Return the :c:type:`Py_complex` value of the complex number *op*.

Doc/data/stable_abi.dat

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/complexobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ PyAPI_DATA(PyTypeObject) PyComplex_Type;
1414
#define PyComplex_CheckExact(op) Py_IS_TYPE((op), &PyComplex_Type)
1515

1616
PyAPI_FUNC(PyObject *) PyComplex_FromDoubles(double real, double imag);
17+
PyAPI_FUNC(PyObject *) PyComplex_FromString(PyObject *op);
1718

1819
PyAPI_FUNC(double) PyComplex_RealAsDouble(PyObject *op);
1920
PyAPI_FUNC(double) PyComplex_ImagAsDouble(PyObject *op);

Lib/test/test_capi/test_complex.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ def test_imagasdouble(self):
131131

132132
# CRASHES imagasdouble(NULL)
133133

134+
def test_fromstring(self):
135+
# Test PyComplex_FromString()
136+
fromstring = _testlimitedcapi.complex_fromstring
137+
138+
self.assertEqual(fromstring("1+2j"), 1+2j)
139+
self.assertEqual(fromstring("(1+2j)"), 1+2j)
140+
self.assertEqual(fromstring("2j"), 2j)
141+
142+
self.assertRaises(ValueError, fromstring, "2j\0")
143+
self.assertRaises(TypeError, fromstring, 2j)
144+
145+
# CRASHES fromstring(NULL)
146+
134147
def test_asccomplex(self):
135148
# Test PyComplex_AsCComplex()
136149
asccomplex = _testcapi.complex_asccomplex

Lib/test/test_stable_abi_ctypes.py

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Misc/stable_abi.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,3 +2544,5 @@
25442544
added = '3.14'
25452545
[function.Py_PACK_VERSION]
25462546
added = '3.14'
2547+
[function.PyComplex_FromString]
2548+
added = '3.14'

Modules/_testlimitedcapi/complex.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,21 @@ complex_imagasdouble(PyObject *Py_UNUSED(module), PyObject *obj)
5858
return PyFloat_FromDouble(imag);
5959
}
6060

61+
static PyObject *
62+
complex_fromstring(PyObject *Py_UNUSED(module), PyObject *obj)
63+
{
64+
NULLABLE(obj);
65+
return PyComplex_FromString(obj);
66+
}
67+
6168

6269
static PyMethodDef test_methods[] = {
6370
{"complex_check", complex_check, METH_O},
6471
{"complex_checkexact", complex_checkexact, METH_O},
6572
{"complex_fromdoubles", complex_fromdoubles, METH_VARARGS},
6673
{"complex_realasdouble", complex_realasdouble, METH_O},
6774
{"complex_imagasdouble", complex_imagasdouble, METH_O},
75+
{"complex_fromstring", complex_fromstring, METH_O},
6876
{NULL},
6977
};
7078

Objects/complexobject.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,12 @@ complex_subtype_from_string(PyTypeObject *type, PyObject *v)
10801080
return result;
10811081
}
10821082

1083+
PyObject *
1084+
PyComplex_FromString(PyObject *op)
1085+
{
1086+
return complex_subtype_from_string(&PyComplex_Type, op);
1087+
}
1088+
10831089
/* The constructor should only accept a string as a positional argument,
10841090
* not as by the 'real' keyword. But Argument Clinic does not allow
10851091
* to distinguish between argument passed positionally and by keyword.

PC/python3dll.c

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)