Skip to content

Commit 235aa86

Browse files
Merge pull request RustPython#296 from agude/int_conj
Add .conjugate() method to int type
2 parents 371f8f8 + 33523d4 commit 235aa86

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

tests/snippets/basic_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
assert type(a) is complex
4646
assert type(a + a) is complex
4747

48+
a = 1
49+
assert a.conjugate() == a
4850

4951
a = 12345
5052

tests/snippets/bools.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@ def __bool__(self):
4646
assert False * 7 == 0
4747
assert True > 0
4848
assert int(True) == 1
49+
assert True.conjugate() == 1
50+
assert isinstance(True.conjugate(), int)

vm/src/obj/objint.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,12 @@ fn int_bit_length(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
489489
Ok(vm.ctx.new_int(bits.to_bigint().unwrap()))
490490
}
491491

492+
fn int_conjugate(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
493+
arg_check!(vm, args, required = [(i, Some(vm.ctx.int_type()))]);
494+
let v = get_value(i);
495+
Ok(vm.ctx.new_int(v))
496+
}
497+
492498
pub fn init(context: &PyContext) {
493499
let ref int_type = context.int_type;
494500
context.set_attr(&int_type, "__eq__", context.new_rustfunc(int_eq));
@@ -526,4 +532,5 @@ pub fn init(context: &PyContext) {
526532
"bit_length",
527533
context.new_rustfunc(int_bit_length),
528534
);
535+
context.set_attr(&int_type, "conjugate", context.new_rustfunc(int_conjugate));
529536
}

0 commit comments

Comments
 (0)