implement complex.__complex__

add `complex.__complex__` which will be added in cpython 3.11 for typing
module.

documents:
* https://docs.python.org/3.11/whatsnew/3.11.html#other-cpython-implementation-changes
* https://bugs.python.org/issue24234

Signed-off-by: snowapril <sinjihng@gmail.com>
This commit is contained in:
Snowapril
2021-08-29 11:33:56 +09:00
committed by GitHub
parent 30faf19987
commit 2aa58cc551
2 changed files with 24 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
import testutils
# __complex__
def test__complex__():
z = 3 + 4j
assert z.__complex__() == z
assert type(z.__complex__()) == complex
class complex_subclass(complex):
pass
z = complex_subclass(3 + 4j)
assert z.__complex__() == 3 + 4j
assert type(z.__complex__()) == complex
testutils.skip_if_unsupported(3,11,test__complex__)

View File

@@ -89,6 +89,15 @@ impl PyComplex {
self.value
}
#[pymethod(magic)]
fn complex(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRef<PyComplex> {
if zelf.is(&vm.ctx.types.complex_type) {
zelf
} else {
PyComplex::from(zelf.value).into_ref(vm)
}
}
#[pyproperty]
fn real(&self) -> f64 {
self.value.re