Skip to content

Commit 86fcc97

Browse files
author
Shitong Wen
committed
int_from_bytes
1 parent e9fea28 commit 86fcc97

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

tests/snippets/ints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@
7272

7373
assert int.from_bytes(b'\x00\x10', 'big') == 16
7474
assert int.from_bytes(b'\x00\x10', 'little') == 4096
75-
assert int.from_bytes(b'\xfc\x00', 'big', True) == -1024
76-
assert int.from_bytes(b'\xfc\x00', 'big', False) == 64512
75+
assert int.from_bytes(b'\xfc\x00', 'big', signed=True) == -1024
76+
assert int.from_bytes(b'\xfc\x00', 'big', signed=False) == 64512
7777

7878
with assertRaises(TypeError):
7979
int(base=2)

vm/src/obj/objint.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ use num_integer::Integer;
66
use num_traits::{One, Pow, Signed, ToPrimitive, Zero};
77

88
use crate::format::FormatSpec;
9-
use crate::function::{OptionalArg, PyFuncArgs};
9+
use crate::function::{KwArgs, OptionalArg, PyFuncArgs};
1010
use crate::pyobject::{
1111
IntoPyObject, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
1212
TypeProtocol,
1313
};
1414
use crate::vm::VirtualMachine;
1515

16+
use super::objbyteinner::PyByteInner;
1617
use super::objstr::{PyString, PyStringRef};
1718
use super::objtype;
18-
use super::objbyteinner::{PyByteInner};
1919
use crate::obj::objtype::PyClassRef;
2020

2121
/// int(x=0) -> integer
@@ -491,24 +491,37 @@ impl PyInt {
491491
}
492492

493493
#[pymethod]
494-
fn from_bytes(bytes: PyByteInner, byteorder: PyStringRef, signed: OptionalArg<bool>, vm: &VirtualMachine) -> PyResult<BigInt>{
494+
fn from_bytes(
495+
bytes: PyByteInner,
496+
byteorder: PyStringRef,
497+
kwargs: KwArgs,
498+
vm: &VirtualMachine,
499+
) -> PyResult<BigInt> {
500+
let mut signed = false;
501+
for (key, value) in kwargs.into_iter() {
502+
if key == "signed" {
503+
signed = match_class!(value,
504+
505+
b @ PyInt => !b.as_bigint().is_zero(),
506+
_ => false,
507+
);
508+
}
509+
}
495510
let x;
496511
if byteorder.value == "big" {
497512
x = match signed {
498-
OptionalArg::Present(true) => BigInt::from_signed_bytes_be(&bytes.elements),
499-
_ => BigInt::from_bytes_be(Sign::Plus, &bytes.elements),
513+
true => BigInt::from_signed_bytes_be(&bytes.elements),
514+
false => BigInt::from_bytes_be(Sign::Plus, &bytes.elements),
500515
}
501-
}
502-
else if byteorder.value == "little" {
516+
} else if byteorder.value == "little" {
503517
x = match signed {
504-
OptionalArg::Present(true) => BigInt::from_signed_bytes_le(&bytes.elements),
505-
_ => BigInt::from_bytes_le(Sign::Plus, &bytes.elements),
518+
true => BigInt::from_signed_bytes_le(&bytes.elements),
519+
false => BigInt::from_bytes_le(Sign::Plus, &bytes.elements),
506520
}
507-
}
508-
else {
509-
return Err(vm.new_value_error(
510-
"byteorder must be either 'little' or 'big'".to_string(),
511-
));
521+
} else {
522+
return Err(
523+
vm.new_value_error("byteorder must be either 'little' or 'big'".to_string())
524+
);
512525
}
513526
Ok(x)
514527
}

0 commit comments

Comments
 (0)