forked from Rust-related/RustPython
Merge pull request #3142 from youknowone/int-primitive
int::try_to_primitive -> PyInt::try_to_primitive
This commit is contained in:
@@ -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>
|
||||
where
|
||||
I: PrimInt + TryFrom<&'a BigInt>,
|
||||
{
|
||||
@@ -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
|
||||
|
||||
@@ -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 ({})",
|
||||
@@ -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()),
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
@@ -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)))
|
||||
|
||||
@@ -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,
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
};
|
||||
@@ -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},
|
||||
@@ -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?
|
||||
|
||||
@@ -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,
|
||||
@@ -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 })
|
||||
}
|
||||
}
|
||||
@@ -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(|_| {
|
||||
|
||||
@@ -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,
|
||||
@@ -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(),
|
||||
|
||||
@@ -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},
|
||||
@@ -70,7 +70,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)]
|
||||
@@ -726,7 +727,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),
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user