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
9 changes: 8 additions & 1 deletion vm/src/builtins/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ macro_rules! impl_into_pyobject_int {

impl_into_pyobject_int!(isize i8 i16 i32 i64 usize u8 u16 u32 u64 BigInt);

pub fn try_to_primitive<'a, I>(i: &'a BigInt, vm: &VirtualMachine) -> PyResult<I>
pub(crate) fn try_to_primitive<'a, I>(i: &'a BigInt, vm: &VirtualMachine) -> PyResult<I>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this used elsewhere in the crate after this change? If not, why not make it private?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vm/src/iterator.rs uses it

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like that place can easily be changed to accommodate this change, either the case, this lgtm, I'll leave the choice of if you want to change it up to you.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is still related to stdlib work. let me clean it up with it later.

where
I: PrimInt + TryFrom<&'a BigInt>,
{
Expand Down Expand Up @@ -302,6 +302,13 @@ impl PyInt {
})
}

pub fn try_to_primitive<'a, I>(&'a self, vm: &VirtualMachine) -> PyResult<I>
where
I: PrimInt + TryFrom<&'a BigInt>,
{
try_to_primitive(self.as_bigint(), vm)
}

#[inline]
fn int_op<F>(&self, other: PyObjectRef, op: F, vm: &VirtualMachine) -> PyArithmaticValue<BigInt>
where
Expand Down
6 changes: 3 additions & 3 deletions vm/src/stdlib/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl TryFromObject for Fildes {
.map_err(|_| vm.new_type_error("fileno() returned a non-integer".to_owned()))?
}
};
let fd = int::try_to_primitive(int.as_bigint(), vm)?;
let fd = int.try_to_primitive(vm)?;
if fd < 0 {
return Err(vm.new_value_error(format!(
"file descriptor cannot be a negative integer ({})",
Expand Down Expand Up @@ -2883,7 +2883,7 @@ mod _io {
))
})?;
let flags = flags.payload::<int::PyInt>().ok_or_else(state_err)?;
let flags = int::try_to_primitive(flags.as_bigint(), vm)?;
let flags = flags.try_to_primitive(vm)?;
Ok((buf, flags))
}
_ => Err(state_err()),
Expand Down Expand Up @@ -3846,7 +3846,7 @@ mod fileio {
}
fd
} else if let Some(i) = name.payload::<crate::builtins::PyInt>() {
crate::builtins::int::try_to_primitive(i.as_bigint(), vm)?
i.try_to_primitive(vm)?
} else {
let path = os::PyPathLike::try_from_object(vm, name.clone())?;
if !args.closefd {
Expand Down
4 changes: 2 additions & 2 deletions vm/src/stdlib/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub(crate) use _operator::make_module;
mod _operator {
use crate::common::cmp;
use crate::{
builtins::{int, PyInt, PyIntRef, PyStrRef, PyTypeRef},
builtins::{PyInt, PyIntRef, PyStrRef, PyTypeRef},
byteslike::ArgBytesLike,
function::{FuncArgs, KwArgs, OptionalArg},
iterator,
Expand Down Expand Up @@ -283,7 +283,7 @@ mod _operator {
v.class().name()
)));
}
int::try_to_primitive(v.payload::<PyInt>().unwrap().as_bigint(), vm)
v.payload::<PyInt>().unwrap().try_to_primitive(vm)
})
.unwrap_or(Ok(0))?;
iterator::length_hint(vm, obj).map(|v| vm.ctx.new_int(v.unwrap_or(default)))
Expand Down
16 changes: 8 additions & 8 deletions vm/src/stdlib/os.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::errno::errors;
use crate::crt_fd::Fd;
use crate::{
builtins::{int, PyBytes, PyBytesRef, PySet, PyStr, PyStrRef},
builtins::{PyBytes, PyBytesRef, PyInt, PySet, PyStr, PyStrRef},
exceptions::{IntoPyException, PyBaseExceptionRef},
function::{ArgumentError, FromArgs, FuncArgs},
protocol::PyBuffer,
Expand Down Expand Up @@ -208,8 +208,8 @@ pub(crate) enum PathOrFd {

impl TryFromObject for PathOrFd {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
match obj.downcast::<int::PyInt>() {
Ok(int) => int::try_to_primitive(int.as_bigint(), vm).map(Self::Fd),
match obj.downcast::<PyInt>() {
Ok(int) => int.try_to_primitive(vm).map(Self::Fd),
Err(obj) => PyPathLike::try_from_object(vm, obj).map(Self::Path),
}
}
Expand Down Expand Up @@ -334,7 +334,7 @@ impl<const AVAILABLE: usize> FromArgs for DirFd<AVAILABLE> {
o.class().name()
)))
})?;
let fd = int::try_to_primitive(fd.as_bigint(), vm)?;
let fd = fd.try_to_primitive(vm)?;
Fd(fd)
}
};
Expand Down Expand Up @@ -368,10 +368,10 @@ pub(super) mod _os {
errno_err, DirFd, FollowSymlinks, FsPath, OutputMode, PathOrFd, PyPathLike, SupportFunc,
};
use crate::common::lock::{OnceCell, PyRwLock};
use crate::crt_fd::{Fd, Offset};
use crate::{
builtins::{int, PyBytesRef, PyStrRef, PyTuple, PyTupleRef, PyTypeRef},
builtins::{PyBytesRef, PyStrRef, PyTuple, PyTupleRef, PyTypeRef},
byteslike::ArgBytesLike,
crt_fd::{Fd, Offset},
exceptions::IntoPyException,
function::{FuncArgs, OptionalArg},
slots::{IteratorIterable, PyIter},
Expand Down Expand Up @@ -1302,8 +1302,8 @@ pub(super) mod _os {
divmod.class().name()
))
})?;
let secs = int::try_to_primitive(vm.to_index(&div)?.as_bigint(), vm)?;
let ns = int::try_to_primitive(vm.to_index(&rem)?.as_bigint(), vm)?;
let secs = vm.to_index(&div)?.try_to_primitive(vm)?;
let ns = vm.to_index(&rem)?.try_to_primitive(vm)?;
Ok(Duration::new(secs, ns))
};
// TODO: do validation to make sure this doesn't.. underflow?
Expand Down
8 changes: 4 additions & 4 deletions vm/src/stdlib/posix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
#[pymodule(name = "posix")]
pub mod module {
use crate::{
builtins::{int, PyDictRef, PyInt, PyListRef, PyStrRef, PyTupleRef, PyTypeRef},
builtins::{PyDictRef, PyInt, PyListRef, PyStrRef, PyTupleRef, PyTypeRef},
exceptions::IntoPyException,
function::OptionalArg,
slots::SlotConstructor,
Expand Down Expand Up @@ -483,13 +483,13 @@ pub mod module {
use crate::TypeProtocol;
let priority = self.sched_priority.clone();
let priority_type = priority.class().name();
let value = priority.downcast::<int::PyInt>().map_err(|_| {
let value = priority.downcast::<PyInt>().map_err(|_| {
vm.new_type_error(format!(
"an integer is required (got type {})",
priority_type
))
})?;
let sched_priority = int::try_to_primitive(value.as_bigint(), vm)?;
let sched_priority = value.try_to_primitive(vm)?;
Ok(libc::sched_param { sched_priority })
}
}
Expand Down Expand Up @@ -1472,7 +1472,7 @@ pub mod module {
impl TryFromObject for ConfName {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
let i = match obj.downcast::<PyInt>() {
Ok(int) => int::try_to_primitive(int.as_bigint(), vm)?,
Ok(int) => int.try_to_primitive(vm)?,
Err(obj) => {
let s = PyStrRef::try_from_object(vm, obj)?;
s.as_str().parse::<PathconfVar>().map_err(|_| {
Expand Down
6 changes: 3 additions & 3 deletions vm/src/stdlib/pystruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
pub(crate) mod _struct {
use crate::{
builtins::{
bytes::PyBytesRef, float, int::try_to_primitive, pybool::IntoPyBool, pystr::PyStr,
pystr::PyStrRef, pytype::PyTypeRef, tuple::PyTupleRef,
bytes::PyBytesRef, float, pybool::IntoPyBool, pystr::PyStr, pystr::PyStrRef,
pytype::PyTypeRef, tuple::PyTupleRef,
},
byteslike::{ArgBytesLike, ArgMemoryBuffer},
common::str::wchar_t,
Expand Down Expand Up @@ -461,7 +461,7 @@ pub(crate) mod _struct {
T: PrimInt + for<'a> std::convert::TryFrom<&'a BigInt>,
{
match vm.to_index_opt(arg) {
Some(index) => try_to_primitive(index?.as_bigint(), vm),
Some(index) => index?.try_to_primitive(vm),
None => Err(new_struct_error(
vm,
"required argument is not an integer".to_owned(),
Expand Down
7 changes: 4 additions & 3 deletions vm/src/stdlib/socket.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::common::lock::{PyMappedRwLockReadGuard, PyRwLock, PyRwLockReadGuard};
use crate::{
builtins::{int, PyStrRef, PyTupleRef, PyTypeRef},
builtins::{PyStrRef, PyTupleRef, PyTypeRef},
byteslike::{ArgBytesLike, ArgMemoryBuffer},
exceptions::{IntoPyException, PyBaseExceptionRef},
function::{FuncArgs, OptionalArg, OptionalOption},
Expand Down Expand Up @@ -67,7 +67,8 @@ fn get_raw_sock(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<RawSocket> {
let int = vm
.to_index_opt(obj)
.unwrap_or_else(|| Err(vm.new_type_error("an integer is required".to_owned())))?;
int::try_to_primitive::<CastFrom>(int.as_bigint(), vm).map(|sock| sock as RawSocket)
int.try_to_primitive::<CastFrom>(vm)
.map(|sock| sock as RawSocket)
}

#[cfg(unix)]
Expand Down Expand Up @@ -707,7 +708,7 @@ impl PySocket {
let int = vm.to_index_opt(arg2).unwrap_or_else(|| {
Err(vm.new_type_error("an integer is required".to_owned()))
})?;
let flags = int::try_to_primitive::<i32>(int.as_bigint(), vm)?;
let flags = int.try_to_primitive::<i32>(vm)?;
(flags, arg3)
}
OptionalArg::Missing => (0, arg2),
Expand Down
4 changes: 2 additions & 2 deletions vm/src/stdlib/termios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub(crate) use self::termios::make_module;
#[pymodule]
mod termios {
use crate::{
builtins::{int, PyBytes, PyInt, PyListRef, PyTypeRef},
builtins::{PyBytes, PyInt, PyListRef, PyTypeRef},
exceptions::PyBaseExceptionRef,
IntoPyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine,
};
Expand Down Expand Up @@ -79,7 +79,7 @@ mod termios {
*cc = if let Some(c) = x.payload::<PyBytes>().filter(|b| b.as_bytes().len() == 1) {
c.as_bytes()[0] as _
} else if let Some(i) = x.payload::<PyInt>() {
int::try_to_primitive(i.as_bigint(), vm)?
i.try_to_primitive(vm)?
} else {
return Err(vm.new_type_error(
"tcsetattr: elements of attributes must be characters or integers"
Expand Down