Skip to content

Update pystruct + array #2297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 27, 2020
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
4 changes: 0 additions & 4 deletions Lib/test/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,6 @@ def __bool__(self):
for c in [b'\x01', b'\x7f', b'\xff', b'\x0f', b'\xf0']:
self.assertTrue(struct.unpack('>?', c)[0])

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_count_overflow(self):
hugecount = '{}b'.format(sys.maxsize+1)
self.assertRaises(struct.error, struct.calcsize, hugecount)
Expand Down Expand Up @@ -621,8 +619,6 @@ def test_boundary_error_message_with_negative_offset(self):
"offset -11 out of range for 10-byte buffer"):
struct.unpack_from('<B', byte_list, -11)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_boundary_error_message_with_large_offset(self):
# Test overflows cause by large offset and value size (issue 30245)
regex1 = (
Expand Down
37 changes: 17 additions & 20 deletions vm/src/stdlib/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::VirtualMachine;
use crossbeam_utils::atomic::AtomicCell;
use itertools::Itertools;
use std::cmp::Ordering;
use std::fmt;
use std::{fmt, os::raw};

struct ArrayTypeSpecifierError {
_priv: (),
Expand All @@ -37,7 +37,7 @@ impl fmt::Display for ArrayTypeSpecifierError {
}

macro_rules! def_array_enum {
($(($n:ident, $t:ident, $c:literal)),*$(,)?) => {
($(($n:ident, $t:ty, $c:literal)),*$(,)?) => {
#[derive(Debug, Clone)]
pub(crate) enum ArrayContentType {
$($n(Vec<$t>),)*
Expand Down Expand Up @@ -79,7 +79,7 @@ macro_rules! def_array_enum {
fn push(&mut self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
match self {
$(ArrayContentType::$n(v) => {
let val = $t::try_into_from_object(vm, obj)?;
let val = <$t>::try_into_from_object(vm, obj)?;
v.push(val);
})*
}
Expand All @@ -97,7 +97,7 @@ macro_rules! def_array_enum {
fn insert(&mut self, i: usize, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
match self {
$(ArrayContentType::$n(v) => {
let val = $t::try_into_from_object(vm, obj)?;
let val = <$t>::try_into_from_object(vm, obj)?;
v.insert(i, val);
})*
}
Expand All @@ -107,7 +107,7 @@ macro_rules! def_array_enum {
fn count(&self, obj: PyObjectRef, vm: &VirtualMachine) -> usize {
match self {
$(ArrayContentType::$n(v) => {
if let Ok(val) = $t::try_into_from_object(vm, obj) {
if let Ok(val) = <$t>::try_into_from_object(vm, obj) {
v.iter().filter(|&&a| a == val).count()
} else {
0
Expand All @@ -119,7 +119,7 @@ macro_rules! def_array_enum {
fn remove(&mut self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()>{
match self {
$(ArrayContentType::$n(v) => {
if let Ok(val) = $t::try_into_from_object(vm, obj) {
if let Ok(val) = <$t>::try_into_from_object(vm, obj) {
if let Some(pos) = v.iter().position(|&a| a == val) {
v.remove(pos);
return Ok(());
Expand Down Expand Up @@ -151,7 +151,7 @@ macro_rules! def_array_enum {
.borrow_value()
.iter()
.cloned()
.map(|value| $t::try_into_from_object(vm, value))
.map(|value| <$t>::try_into_from_object(vm, value))
.try_collect()?;
v.append(&mut list);
Ok(())
Expand Down Expand Up @@ -184,7 +184,7 @@ macro_rules! def_array_enum {
fn index(&self, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
match self {
$(ArrayContentType::$n(v) => {
if let Ok(val) = $t::try_into_from_object(vm, obj) {
if let Ok(val) = <$t>::try_into_from_object(vm, obj) {
if let Some(pos) = v.iter().position(|&a| a == val) {
return Ok(pos);
}
Expand Down Expand Up @@ -267,7 +267,7 @@ macro_rules! def_array_enum {
fn setitem_by_idx(&mut self, i: isize, value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
let i = self.idx(i, "array assignment", vm)?;
match self {
$(ArrayContentType::$n(v) => { v[i] = $t::try_into_from_object(vm, value)? },)*
$(ArrayContentType::$n(v) => { v[i] = <$t>::try_into_from_object(vm, value)? },)*
}
Ok(())
}
Expand Down Expand Up @@ -397,17 +397,14 @@ def_array_enum!(
(SignedByte, i8, 'b'),
(UnsignedByte, u8, 'B'),
// TODO: support unicode char
(SignedShort, i16, 'h'),
(UnsignedShort, u16, 'H'),
(SignedInt, i32, 'i'),
(UnsignedInt, u32, 'I'),
(SignedLong, i32, 'l'),
(UnsignedLong, u32, 'L'),
// FIXME: architecture depended size
// (SignedLong, i64, 'l'),
// (UnsignedLong, u64, 'L'),
(SignedLongLong, i64, 'q'),
(UnsignedLongLong, u64, 'Q'),
(SignedShort, raw::c_short, 'h'),
(UnsignedShort, raw::c_ushort, 'H'),
(SignedInt, raw::c_int, 'i'),
(UnsignedInt, raw::c_uint, 'I'),
(SignedLong, raw::c_long, 'l'),
(UnsignedLong, raw::c_ulong, 'L'),
(SignedLongLong, raw::c_longlong, 'q'),
(UnsignedLongLong, raw::c_ulonglong, 'Q'),
(Float, f32, 'f'),
(Double, f64, 'd'),
);
Expand Down
Loading