Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions vm/src/builtins/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,6 @@ impl TryFromBorrowedObject for BigInt {
}
}

// _PyLong_AsUnsignedLongMask
pub fn bigint_unsigned_mask(v: &BigInt) -> u32 {
v.to_u32()
.or_else(|| v.to_i32().map(|i| i as u32))
.unwrap_or_else(|| {
let mut out = 0u32;
for digit in v.iter_u32_digits() {
out = out.wrapping_shl(32) | digit;
}
match v.sign() {
num_bigint::Sign::Minus => out * -1i32 as u32,
_ => out,
}
})
}

fn inner_pow(int1: &BigInt, int2: &BigInt, vm: &VirtualMachine) -> PyResult {
if int2.is_negative() {
let v1 = try_to_float(int1, vm)?;
Expand Down Expand Up @@ -279,7 +263,6 @@ impl SlotConstructor for PyInt {
}
}

#[pyimpl(flags(BASETYPE), with(Comparable, Hashable, SlotConstructor))]
impl PyInt {
fn with_value<T>(cls: PyTypeRef, value: T, vm: &VirtualMachine) -> PyResult<PyRef<Self>>
where
Expand All @@ -302,6 +285,23 @@ impl PyInt {
&self.value
}

// _PyLong_AsUnsignedLongMask
pub fn as_u32_mask(&self) -> u32 {
let v = self.as_bigint();
v.to_u32()
.or_else(|| v.to_i32().map(|i| i as u32))
.unwrap_or_else(|| {
let mut out = 0u32;
for digit in v.iter_u32_digits() {
out = out.wrapping_shl(32) | digit;
}
match v.sign() {
num_bigint::Sign::Minus => out * -1i32 as u32,
_ => out,
}
})
}

#[inline]
fn int_op<F>(&self, other: PyObjectRef, op: F, vm: &VirtualMachine) -> PyArithmaticValue<BigInt>
where
Expand All @@ -324,7 +324,10 @@ impl PyInt {
Ok(vm.ctx.not_implemented())
}
}
}

#[pyimpl(flags(BASETYPE), with(Comparable, Hashable, SlotConstructor))]
impl PyInt {
#[pymethod(name = "__radd__")]
#[pymethod(magic)]
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod fcntl {
}
return Ok(vm.ctx.new_bytes(buf[..arg_len].to_vec()));
}
OptionalArg::Present(Either::B(i)) => int::bigint_unsigned_mask(i.as_bigint()),
OptionalArg::Present(Either::B(i)) => i.as_u32_mask(),
OptionalArg::Missing => 0,
};
let ret = unsafe { libc::fcntl(fd, cmd, int as i32) };
Expand Down
6 changes: 3 additions & 3 deletions vm/src/stdlib/zlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub(crate) use decl::make_module;
mod decl {
use crate::common::lock::PyMutex;
use crate::{
builtins::{int, PyBytes, PyBytesRef, PyIntRef, PyTypeRef},
builtins::{PyBytes, PyBytesRef, PyIntRef, PyTypeRef},
byteslike::ArgBytesLike,
exceptions::PyBaseExceptionRef,
function::OptionalArg,
Expand Down Expand Up @@ -66,7 +66,7 @@ mod decl {
#[pyfunction]
fn adler32(data: ArgBytesLike, begin_state: OptionalArg<PyIntRef>) -> u32 {
data.with_ref(|data| {
let begin_state = begin_state.map_or(1, |i| int::bigint_unsigned_mask(i.as_bigint()));
let begin_state = begin_state.map_or(1, |i| i.as_u32_mask());

let mut hasher = Adler32::from_value(begin_state);
hasher.update_buffer(data);
Expand All @@ -78,7 +78,7 @@ mod decl {
#[pyfunction]
fn crc32(data: ArgBytesLike, begin_state: OptionalArg<PyIntRef>) -> u32 {
data.with_ref(|data| {
let begin_state = begin_state.map_or(0, |i| int::bigint_unsigned_mask(i.as_bigint()));
let begin_state = begin_state.map_or(0, |i| i.as_u32_mask());

let mut hasher = Crc32::new_with_initial(begin_state);
hasher.update(data);
Expand Down