forked from Rust-related/RustPython
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:
15
extra_tests/snippets/complex.py
Normal file
15
extra_tests/snippets/complex.py
Normal 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__)
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user