Skip to content

Commit 1575b9d

Browse files
Merge pull request RustPython#1266 from corona10/complex__getnewargs__
complex: Implement __getnewargs__
2 parents 5f285a5 + 4ea960c commit 1575b9d

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

tests/snippets/builtin_complex.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,7 @@
145145
assert '(1-1j)' == str(1-1j)
146146
assert '(1+1j)' == repr(1+1j)
147147
assert '(1-1j)' == repr(1-1j)
148+
149+
# __getnewargs__
150+
assert (3 + 5j).__getnewargs__() == (3.0, 5.0)
151+
assert (5j).__getnewargs__() == (0.0, 5.0)

vm/src/obj/objcomplex.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,12 @@ impl PyComplex {
258258
let ret = Wrapping(re_hash) + Wrapping(im_hash) * Wrapping(pyhash::IMAG);
259259
ret.0
260260
}
261+
262+
#[pymethod(name = "__getnewargs__")]
263+
fn complex_getnewargs(&self, vm: &VirtualMachine) -> PyResult {
264+
let Complex64 { re, im } = self.value;
265+
Ok(vm
266+
.ctx
267+
.new_tuple(vec![vm.ctx.new_float(re), vm.ctx.new_float(im)]))
268+
}
261269
}

0 commit comments

Comments
 (0)