Use usize directly

This commit is contained in:
Aviv Palivoda
2019-10-17 19:28:04 +03:00
parent ca20acf7a0
commit e89baf8d47
4 changed files with 20 additions and 38 deletions

View File

@@ -687,16 +687,10 @@ impl PyItertoolsTee {
fn new(
_cls: PyClassRef,
iterable: PyObjectRef,
n: OptionalArg<PyIntRef>,
n: OptionalArg<usize>,
vm: &VirtualMachine,
) -> PyResult<PyRef<PyTuple>> {
let n = match n {
OptionalArg::Present(x) => match x.as_bigint().to_usize() {
Some(y) => y,
None => return Err(vm.new_overflow_error(String::from("n is too big"))),
},
OptionalArg::Missing => 2,
};
let n = n.unwrap_or(2);
let copyable = if objtype::class_has_attr(&iterable.class(), "__copy__") {
vm.call_method(&iterable, "__copy__", PyFuncArgs::from(vec![]))?

View File

@@ -6,7 +6,7 @@
*/
use std::fmt;
use num_traits::{Signed, ToPrimitive};
use num_traits::Signed;
use regex::bytes::{Captures, Regex, RegexBuilder};
use crate::function::{Args, OptionalArg};
@@ -93,7 +93,7 @@ impl PyValue for PyMatch {
fn re_match(
pattern: PyStringRef,
string: PyStringRef,
flags: OptionalArg<PyIntRef>,
flags: OptionalArg<usize>,
vm: &VirtualMachine,
) -> PyResult {
let flags = extract_flags(flags);
@@ -104,7 +104,7 @@ fn re_match(
fn re_search(
pattern: PyStringRef,
string: PyStringRef,
flags: OptionalArg<PyIntRef>,
flags: OptionalArg<usize>,
vm: &VirtualMachine,
) -> PyResult {
let flags = extract_flags(flags);
@@ -117,7 +117,7 @@ fn re_sub(
repl: PyStringRef,
string: PyStringRef,
count: OptionalArg<usize>,
flags: OptionalArg<PyIntRef>,
flags: OptionalArg<usize>,
vm: &VirtualMachine,
) -> PyResult {
let flags = extract_flags(flags);
@@ -129,7 +129,7 @@ fn re_sub(
fn re_findall(
pattern: PyStringRef,
string: PyStringRef,
flags: OptionalArg<PyIntRef>,
flags: OptionalArg<usize>,
vm: &VirtualMachine,
) -> PyResult {
let flags = extract_flags(flags);
@@ -141,7 +141,7 @@ fn re_split(
pattern: PyStringRef,
string: PyStringRef,
maxsplit: OptionalArg<PyIntRef>,
flags: OptionalArg<PyIntRef>,
flags: OptionalArg<usize>,
vm: &VirtualMachine,
) -> PyResult {
let flags = extract_flags(flags);
@@ -296,18 +296,16 @@ fn create_match(vm: &VirtualMachine, haystack: PyStringRef, captures: Captures)
PyMatch { haystack, captures }.into_ref(vm).into_object()
}
fn extract_flags(flags: OptionalArg<PyIntRef>) -> PyRegexFlags {
fn extract_flags(flags: OptionalArg<usize>) -> PyRegexFlags {
match flags {
OptionalArg::Present(flags) => {
PyRegexFlags::from_int(flags.as_bigint().to_usize().unwrap())
}
OptionalArg::Present(flags) => PyRegexFlags::from_int(flags),
OptionalArg::Missing => Default::default(),
}
}
fn re_compile(
pattern: PyStringRef,
flags: OptionalArg<PyIntRef>,
flags: OptionalArg<usize>,
vm: &VirtualMachine,
) -> PyResult<PyPattern> {
let flags = extract_flags(flags);

View File

@@ -312,8 +312,8 @@ impl SocketRef {
Ok(vm.ctx.new_tuple(vec![socket.into_object(), addr_tuple]))
}
fn recv(self, bufsize: PyIntRef, vm: &VirtualMachine) -> PyResult {
let mut buffer = vec![0u8; bufsize.as_bigint().to_usize().unwrap()];
fn recv(self, bufsize: usize, vm: &VirtualMachine) -> PyResult {
let mut buffer = vec![0u8; bufsize];
match self.con.borrow_mut().as_mut() {
Some(v) => match v.read_exact(&mut buffer) {
Ok(_) => (),
@@ -328,8 +328,8 @@ impl SocketRef {
Ok(vm.ctx.new_bytes(buffer))
}
fn recvfrom(self, bufsize: PyIntRef, vm: &VirtualMachine) -> PyResult {
let mut buffer = vec![0u8; bufsize.as_bigint().to_usize().unwrap()];
fn recvfrom(self, bufsize: usize, vm: &VirtualMachine) -> PyResult {
let mut buffer = vec![0u8; bufsize];
let ret = match self.con.borrow().as_ref() {
Some(v) => v.recv_from(&mut buffer),
None => return Err(vm.new_type_error("".to_string())),

View File

@@ -1,5 +1,5 @@
use crate::function::OptionalArg;
use crate::obj::{objbytes::PyBytesRef, objint::PyIntRef};
use crate::obj::objbytes::PyBytesRef;
use crate::pyobject::{ItemProtocol, PyObjectRef, PyResult};
use crate::types::create_type;
use crate::vm::VirtualMachine;
@@ -8,7 +8,6 @@ use adler32::RollingAdler32 as Adler32;
use crc32fast::Hasher as Crc32;
use flate2::{write::ZlibEncoder, Compression, Decompress, FlushDecompress, Status};
use libz_sys as libz;
use num_traits::cast::ToPrimitive;
use std::io::Write;
@@ -92,23 +91,14 @@ fn zlib_compress(data: PyBytesRef, level: OptionalArg<i32>, vm: &VirtualMachine)
/// Returns a bytes object containing the uncompressed data.
fn zlib_decompress(
data: PyBytesRef,
wbits: OptionalArg<PyIntRef>,
bufsize: OptionalArg<PyIntRef>,
wbits: OptionalArg<u8>,
bufsize: OptionalArg<usize>,
vm: &VirtualMachine,
) -> PyResult {
let encoded_bytes = data.get_value();
let wbits = wbits
.into_option()
.as_ref()
.map(|wbits| wbits.as_bigint().to_u8().unwrap())
.unwrap_or(MAX_WBITS);
let bufsize = bufsize
.into_option()
.as_ref()
.map(|bufsize| bufsize.as_bigint().to_usize().unwrap())
.unwrap_or(DEF_BUF_SIZE);
let wbits = wbits.unwrap_or(MAX_WBITS);
let bufsize = bufsize.unwrap_or(DEF_BUF_SIZE);
let mut decompressor = Decompress::new_with_window_bits(true, wbits);
let mut decoded_bytes = Vec::with_capacity(bufsize);