Skip to content

Commit 6082bb5

Browse files
mdickinsoncorona10
andauthored
bpo-24234: implement complex.__complex__ (GH-27887)
Co-authored-by: Dong-hee Na <[email protected]>
1 parent eec340e commit 6082bb5

File tree

6 files changed

+57
-3
lines changed

6 files changed

+57
-3
lines changed

Lib/test/test_complex.py

+12
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,18 @@ def __complex__(self):
499499
self.assertEqual(complex(complex1(1j)), 2j)
500500
self.assertRaises(TypeError, complex, complex2(1j))
501501

502+
def test___complex__(self):
503+
z = 3 + 4j
504+
self.assertEqual(z.__complex__(), z)
505+
self.assertEqual(type(z.__complex__()), complex)
506+
507+
class complex_subclass(complex):
508+
pass
509+
510+
z = complex_subclass(3 + 4j)
511+
self.assertEqual(z.__complex__(), 3 + 4j)
512+
self.assertEqual(type(z.__complex__()), complex)
513+
502514
@support.requires_IEEE_754
503515
def test_constructor_special_numbers(self):
504516
class complex2(complex):

Lib/test/test_doctest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ def non_Python_modules(): r"""
668668
669669
>>> import builtins
670670
>>> tests = doctest.DocTestFinder().find(builtins)
671-
>>> 816 < len(tests) < 836 # approximate number of objects with docstrings
671+
>>> 820 < len(tests) < 840 # approximate number of objects with docstrings
672672
True
673673
>>> real_tests = [t for t in tests if len(t.examples) > 0]
674674
>>> len(real_tests) # objects that actually have doctests

Lib/test/test_typing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1533,11 +1533,11 @@ def test_supports_float(self):
15331533

15341534
def test_supports_complex(self):
15351535

1536-
# Note: complex itself doesn't have __complex__.
15371536
class C:
15381537
def __complex__(self):
15391538
return 0j
15401539

1540+
self.assertIsSubclass(complex, typing.SupportsComplex)
15411541
self.assertIsSubclass(C, typing.SupportsComplex)
15421542
self.assertNotIsSubclass(str, typing.SupportsComplex)
15431543

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Implement the :meth:`__complex__` special method on the :class:`complex` type,
2+
so a complex number ``z`` passes an ``isinstance(z, typing.SupportsComplex)``
3+
check.

Objects/clinic/complexobject.c.h

+19-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Objects/complexobject.c

+21
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,29 @@ complex___format___impl(PyComplexObject *self, PyObject *format_spec)
693693
return _PyUnicodeWriter_Finish(&writer);
694694
}
695695

696+
/*[clinic input]
697+
complex.__complex__
698+
699+
Convert this value to exact type complex.
700+
[clinic start generated code]*/
701+
702+
static PyObject *
703+
complex___complex___impl(PyComplexObject *self)
704+
/*[clinic end generated code: output=e6b35ba3d275dc9c input=3589ada9d27db854]*/
705+
{
706+
if (PyComplex_CheckExact(self)) {
707+
Py_INCREF(self);
708+
return (PyObject *)self;
709+
}
710+
else {
711+
return PyComplex_FromCComplex(self->cval);
712+
}
713+
}
714+
715+
696716
static PyMethodDef complex_methods[] = {
697717
COMPLEX_CONJUGATE_METHODDEF
718+
COMPLEX___COMPLEX___METHODDEF
698719
COMPLEX___GETNEWARGS___METHODDEF
699720
COMPLEX___FORMAT___METHODDEF
700721
{NULL, NULL} /* sentinel */

0 commit comments

Comments
 (0)