mirror of
https://github.com/python/cpython.git
synced 2025-11-25 04:34:37 +00:00
bpo-24234: implement complex.__complex__ (GH-27887)
Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
This commit is contained in:
parent
eec340ea3a
commit
6082bb5add
6 changed files with 57 additions and 3 deletions
|
|
@ -499,6 +499,18 @@ class ComplexTest(unittest.TestCase):
|
||||||
self.assertEqual(complex(complex1(1j)), 2j)
|
self.assertEqual(complex(complex1(1j)), 2j)
|
||||||
self.assertRaises(TypeError, complex, complex2(1j))
|
self.assertRaises(TypeError, complex, complex2(1j))
|
||||||
|
|
||||||
|
def test___complex__(self):
|
||||||
|
z = 3 + 4j
|
||||||
|
self.assertEqual(z.__complex__(), z)
|
||||||
|
self.assertEqual(type(z.__complex__()), complex)
|
||||||
|
|
||||||
|
class complex_subclass(complex):
|
||||||
|
pass
|
||||||
|
|
||||||
|
z = complex_subclass(3 + 4j)
|
||||||
|
self.assertEqual(z.__complex__(), 3 + 4j)
|
||||||
|
self.assertEqual(type(z.__complex__()), complex)
|
||||||
|
|
||||||
@support.requires_IEEE_754
|
@support.requires_IEEE_754
|
||||||
def test_constructor_special_numbers(self):
|
def test_constructor_special_numbers(self):
|
||||||
class complex2(complex):
|
class complex2(complex):
|
||||||
|
|
|
||||||
|
|
@ -668,7 +668,7 @@ plain ol' Python and is guaranteed to be available.
|
||||||
|
|
||||||
>>> import builtins
|
>>> import builtins
|
||||||
>>> tests = doctest.DocTestFinder().find(builtins)
|
>>> tests = doctest.DocTestFinder().find(builtins)
|
||||||
>>> 816 < len(tests) < 836 # approximate number of objects with docstrings
|
>>> 820 < len(tests) < 840 # approximate number of objects with docstrings
|
||||||
True
|
True
|
||||||
>>> real_tests = [t for t in tests if len(t.examples) > 0]
|
>>> real_tests = [t for t in tests if len(t.examples) > 0]
|
||||||
>>> len(real_tests) # objects that actually have doctests
|
>>> len(real_tests) # objects that actually have doctests
|
||||||
|
|
|
||||||
|
|
@ -1533,11 +1533,11 @@ class ProtocolTests(BaseTestCase):
|
||||||
|
|
||||||
def test_supports_complex(self):
|
def test_supports_complex(self):
|
||||||
|
|
||||||
# Note: complex itself doesn't have __complex__.
|
|
||||||
class C:
|
class C:
|
||||||
def __complex__(self):
|
def __complex__(self):
|
||||||
return 0j
|
return 0j
|
||||||
|
|
||||||
|
self.assertIsSubclass(complex, typing.SupportsComplex)
|
||||||
self.assertIsSubclass(C, typing.SupportsComplex)
|
self.assertIsSubclass(C, typing.SupportsComplex)
|
||||||
self.assertNotIsSubclass(str, typing.SupportsComplex)
|
self.assertNotIsSubclass(str, typing.SupportsComplex)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
Implement the :meth:`__complex__` special method on the :class:`complex` type,
|
||||||
|
so a complex number ``z`` passes an ``isinstance(z, typing.SupportsComplex)``
|
||||||
|
check.
|
||||||
20
Objects/clinic/complexobject.c.h
generated
20
Objects/clinic/complexobject.c.h
generated
|
|
@ -69,6 +69,24 @@ exit:
|
||||||
return return_value;
|
return return_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PyDoc_STRVAR(complex___complex____doc__,
|
||||||
|
"__complex__($self, /)\n"
|
||||||
|
"--\n"
|
||||||
|
"\n"
|
||||||
|
"Convert this value to exact type complex.");
|
||||||
|
|
||||||
|
#define COMPLEX___COMPLEX___METHODDEF \
|
||||||
|
{"__complex__", (PyCFunction)complex___complex__, METH_NOARGS, complex___complex____doc__},
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
complex___complex___impl(PyComplexObject *self);
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
complex___complex__(PyComplexObject *self, PyObject *Py_UNUSED(ignored))
|
||||||
|
{
|
||||||
|
return complex___complex___impl(self);
|
||||||
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(complex_new__doc__,
|
PyDoc_STRVAR(complex_new__doc__,
|
||||||
"complex(real=0, imag=0)\n"
|
"complex(real=0, imag=0)\n"
|
||||||
"--\n"
|
"--\n"
|
||||||
|
|
@ -113,4 +131,4 @@ skip_optional_pos:
|
||||||
exit:
|
exit:
|
||||||
return return_value;
|
return return_value;
|
||||||
}
|
}
|
||||||
/*[clinic end generated code: output=056cac3226d94967 input=a9049054013a1b77]*/
|
/*[clinic end generated code: output=6d85094ace15677e input=a9049054013a1b77]*/
|
||||||
|
|
|
||||||
|
|
@ -693,8 +693,29 @@ complex___format___impl(PyComplexObject *self, PyObject *format_spec)
|
||||||
return _PyUnicodeWriter_Finish(&writer);
|
return _PyUnicodeWriter_Finish(&writer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*[clinic input]
|
||||||
|
complex.__complex__
|
||||||
|
|
||||||
|
Convert this value to exact type complex.
|
||||||
|
[clinic start generated code]*/
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
complex___complex___impl(PyComplexObject *self)
|
||||||
|
/*[clinic end generated code: output=e6b35ba3d275dc9c input=3589ada9d27db854]*/
|
||||||
|
{
|
||||||
|
if (PyComplex_CheckExact(self)) {
|
||||||
|
Py_INCREF(self);
|
||||||
|
return (PyObject *)self;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return PyComplex_FromCComplex(self->cval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static PyMethodDef complex_methods[] = {
|
static PyMethodDef complex_methods[] = {
|
||||||
COMPLEX_CONJUGATE_METHODDEF
|
COMPLEX_CONJUGATE_METHODDEF
|
||||||
|
COMPLEX___COMPLEX___METHODDEF
|
||||||
COMPLEX___GETNEWARGS___METHODDEF
|
COMPLEX___GETNEWARGS___METHODDEF
|
||||||
COMPLEX___FORMAT___METHODDEF
|
COMPLEX___FORMAT___METHODDEF
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue