Skip to content

Commit babaeba

Browse files
authored
Merge pull request RustPython#1952 from RustPython/coolreader18/32bit-support
32 bit support
2 parents 5f34c61 + 2fc90e1 commit babaeba

15 files changed

+171
-234
lines changed

Lib/test/seq_tests.py

+1
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ def test_repeat(self):
321321
self.assertEqual(self.type2test(s)*(-4), self.type2test([]))
322322
self.assertEqual(id(s), id(s*1))
323323

324+
@unittest.skip("TODO: RUSTPYTHON")
324325
def test_bigrepeat(self):
325326
if sys.maxsize <= 2147483647:
326327
x = self.type2test([0])

Lib/test/string_tests.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@ def test_expandtabs(self):
352352

353353
self.checkraises(TypeError, 'hello', 'expandtabs', 42, 42)
354354
# This test is only valid when sizeof(int) == sizeof(void*) == 4.
355-
if sys.maxsize < (1 << 32) and struct.calcsize('P') == 4:
355+
# XXX RUSTPYTHON TODO: expandtabs overflow checks
356+
if sys.maxsize < (1 << 32) and struct.calcsize('P') == 4 and False:
356357
self.checkraises(OverflowError,
357358
'\ta\n\tb', 'expandtabs', sys.maxsize)
358359

@@ -672,6 +673,7 @@ def test_replace(self):
672673
self.checkraises(TypeError, 'hello', 'replace', 42, 'h')
673674
self.checkraises(TypeError, 'hello', 'replace', 'h', 42)
674675

676+
@unittest.skip("TODO: RUSTPYTHON")
675677
@unittest.skipIf(sys.maxsize > (1 << 32) or struct.calcsize('P') != 4,
676678
'only applies to 32-bit platforms')
677679
def test_replace_overflow(self):

Lib/test/test_bytes.py

-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ def test_copy(self):
5454
self.assertEqual(a, b)
5555
self.assertEqual(type(a), type(b))
5656

57-
# TODO: RUSTPYTHON
58-
@unittest.expectedFailure
5957
def test_empty_sequence(self):
6058
b = self.type2test()
6159
self.assertEqual(len(b), 0)

Lib/test/test_list.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def test_basic(self):
2020
self.assertEqual(list(x for x in range(10) if x % 2),
2121
[1, 3, 5, 7, 9])
2222

23-
if sys.maxsize == 0x7fffffff:
23+
# XXX RUSTPYTHON TODO: catch ooms
24+
if sys.maxsize == 0x7fffffff and False:
2425
# This test can currently only work on 32-bit machines.
2526
# XXX If/when PySequence_Length() returns a ssize_t, it should be
2627
# XXX re-enabled.

Lib/test/test_memoryview.py

-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ def check_getitem_with_type(self, tp):
4747
m = None
4848
self.assertEqual(sys.getrefcount(b), oldrefcount)
4949

50-
# TODO: RUSTPYTHON
51-
@unittest.expectedFailure
5250
def test_getitem(self):
5351
for tp in self._types:
5452
self.check_getitem_with_type(tp)

Lib/test/test_unicode.py

+2
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ def __str__(self): return self.sval
471471
self.checkraises(TypeError, ' ', 'join', [1, 2, 3])
472472
self.checkraises(TypeError, ' ', 'join', ['1', '2', 3])
473473

474+
@unittest.skip("TODO: RUSTPYTHON, oom handling")
474475
@unittest.skipIf(sys.maxsize > 2**32,
475476
'needs too much memory on a 64-bit platform')
476477
def test_join_overflow(self):
@@ -2357,6 +2358,7 @@ def test_printable_repr(self):
23572358
# This test only affects 32-bit platforms because expandtabs can only take
23582359
# an int as the max value, not a 64-bit C long. If expandtabs is changed
23592360
# to take a 64-bit long, this test should apply to all platforms.
2361+
@unittest.skip("TODO: RUSTPYTHON, oom handling")
23602362
@unittest.skipIf(sys.maxsize > (1 << 32) or struct.calcsize('P') != 4,
23612363
'only applies to 32-bit platforms')
23622364
def test_expandtabs_overflows_gracefully(self):

tests/snippets/index_overflow.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44
def expect_cannot_fit_index_error(s, index):
55
try:
66
s[index]
7-
except IndexError:
8-
pass
9-
# TODO: Replace current except block with commented
10-
# after solving https://github.com/RustPython/RustPython/issues/322
11-
# except IndexError as error:
12-
# assert str(error) == "cannot fit 'int' into an index-sized integer"
7+
except IndexError as error:
8+
assert str(error) == "cannot fit 'int' into an index-sized integer"
139
else:
1410
assert False
1511

vm/src/obj/objbytearray.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use super::objbyteinner::{
1212
};
1313
use super::objint::PyIntRef;
1414
use super::objiter;
15-
use super::objslice::PySliceRef;
15+
use super::objsequence::SequenceIndex;
1616
use super::objstr::{PyString, PyStringRef};
1717
use super::objtype::PyClassRef;
1818
use super::pystr::{self, PyCommonString};
@@ -172,22 +172,17 @@ impl PyByteArray {
172172
}
173173

174174
#[pymethod(name = "__getitem__")]
175-
fn getitem(&self, needle: Either<i32, PySliceRef>, vm: &VirtualMachine) -> PyResult {
175+
fn getitem(&self, needle: SequenceIndex, vm: &VirtualMachine) -> PyResult {
176176
self.borrow_value().getitem(needle, vm)
177177
}
178178

179179
#[pymethod(name = "__setitem__")]
180-
fn setitem(
181-
&self,
182-
needle: Either<i32, PySliceRef>,
183-
value: PyObjectRef,
184-
vm: &VirtualMachine,
185-
) -> PyResult {
180+
fn setitem(&self, needle: SequenceIndex, value: PyObjectRef, vm: &VirtualMachine) -> PyResult {
186181
self.borrow_value_mut().setitem(needle, value, vm)
187182
}
188183

189184
#[pymethod(name = "__delitem__")]
190-
fn delitem(&self, needle: Either<i32, PySliceRef>, vm: &VirtualMachine) -> PyResult<()> {
185+
fn delitem(&self, needle: SequenceIndex, vm: &VirtualMachine) -> PyResult<()> {
191186
self.borrow_value_mut().delitem(needle, vm)
192187
}
193188

vm/src/obj/objbyteinner.rs

+14-20
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::objint::{self, PyInt, PyIntRef};
1010
use super::objlist::PyList;
1111
use super::objmemory::PyMemoryView;
1212
use super::objnone::PyNoneRef;
13-
use super::objsequence::PySliceableSequence;
13+
use super::objsequence::{PySliceableSequence, SequenceIndex};
1414
use super::objslice::PySliceRef;
1515
use super::objstr::{self, PyString, PyStringRef};
1616
use super::pystr::{self, PyCommonString, PyCommonStringWrapper};
@@ -327,22 +327,22 @@ impl PyByteInner {
327327
})
328328
}
329329

330-
pub fn getitem(&self, needle: Either<i32, PySliceRef>, vm: &VirtualMachine) -> PyResult {
330+
pub fn getitem(&self, needle: SequenceIndex, vm: &VirtualMachine) -> PyResult {
331331
match needle {
332-
Either::A(int) => {
332+
SequenceIndex::Int(int) => {
333333
if let Some(idx) = self.elements.get_pos(int) {
334334
Ok(vm.new_int(self.elements[idx]))
335335
} else {
336336
Err(vm.new_index_error("index out of range".to_owned()))
337337
}
338338
}
339-
Either::B(slice) => Ok(vm
340-
.ctx
341-
.new_bytes(self.elements.get_slice_items(vm, slice.as_object())?)),
339+
SequenceIndex::Slice(slice) => {
340+
Ok(vm.ctx.new_bytes(self.elements.get_slice_items(vm, &slice)?))
341+
}
342342
}
343343
}
344344

345-
fn setindex(&mut self, int: i32, object: PyObjectRef, vm: &VirtualMachine) -> PyResult {
345+
fn setindex(&mut self, int: isize, object: PyObjectRef, vm: &VirtualMachine) -> PyResult {
346346
if let Some(idx) = self.elements.get_pos(int) {
347347
let result = match_class!(match object {
348348
i @ PyInt => {
@@ -392,38 +392,32 @@ impl PyByteInner {
392392
range.end = range.start;
393393
}
394394
self.elements.splice(range, items);
395-
Ok(vm
396-
.ctx
397-
.new_bytes(self.elements.get_slice_items(vm, slice.as_object())?))
395+
Ok(vm.ctx.new_bytes(self.elements.get_slice_items(vm, &slice)?))
398396
}
399397

400398
pub fn setitem(
401399
&mut self,
402-
needle: Either<i32, PySliceRef>,
400+
needle: SequenceIndex,
403401
object: PyObjectRef,
404402
vm: &VirtualMachine,
405403
) -> PyResult {
406404
match needle {
407-
Either::A(int) => self.setindex(int, object, vm),
408-
Either::B(slice) => self.setslice(slice, object, vm),
405+
SequenceIndex::Int(int) => self.setindex(int, object, vm),
406+
SequenceIndex::Slice(slice) => self.setslice(slice, object, vm),
409407
}
410408
}
411409

412-
pub fn delitem(
413-
&mut self,
414-
needle: Either<i32, PySliceRef>,
415-
vm: &VirtualMachine,
416-
) -> PyResult<()> {
410+
pub fn delitem(&mut self, needle: SequenceIndex, vm: &VirtualMachine) -> PyResult<()> {
417411
match needle {
418-
Either::A(int) => {
412+
SequenceIndex::Int(int) => {
419413
if let Some(idx) = self.elements.get_pos(int) {
420414
self.elements.remove(idx);
421415
Ok(())
422416
} else {
423417
Err(vm.new_index_error("index out of range".to_owned()))
424418
}
425419
}
426-
Either::B(slice) => self.delslice(slice, vm),
420+
SequenceIndex::Slice(slice) => self.delslice(slice, vm),
427421
}
428422
}
429423

vm/src/obj/objbytes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::objbyteinner::{
1010
};
1111
use super::objint::PyIntRef;
1212
use super::objiter;
13-
use super::objslice::PySliceRef;
13+
use super::objsequence::SequenceIndex;
1414
use super::objstr::{PyString, PyStringRef};
1515
use super::objtype::PyClassRef;
1616
use super::pystr::{self, PyCommonString};
@@ -172,7 +172,7 @@ impl PyBytes {
172172
}
173173

174174
#[pymethod(name = "__getitem__")]
175-
fn getitem(&self, needle: Either<i32, PySliceRef>, vm: &VirtualMachine) -> PyResult {
175+
fn getitem(&self, needle: SequenceIndex, vm: &VirtualMachine) -> PyResult {
176176
self.inner.getitem(needle, vm)
177177
}
178178

vm/src/obj/objlist.rs

+15-60
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use super::objbool;
1111
use super::objbyteinner;
1212
use super::objint::PyIntRef;
1313
use super::objiter;
14-
use super::objsequence::{get_item, SequenceIndex};
14+
use super::objsequence::{get_item, get_pos, get_slice_range, SequenceIndex};
1515
use super::objslice::PySliceRef;
1616
use super::objtype::PyClassRef;
1717
use crate::function::OptionalArg;
@@ -62,51 +62,6 @@ impl PyList {
6262
self.elements.write().unwrap()
6363
}
6464

65-
fn get_pos(p: i32, len: usize) -> Option<usize> {
66-
// convert a (potentially negative) positon into a real index
67-
if p < 0 {
68-
if -p as usize > len {
69-
None
70-
} else {
71-
Some(len - ((-p) as usize))
72-
}
73-
} else if p as usize >= len {
74-
None
75-
} else {
76-
Some(p as usize)
77-
}
78-
}
79-
80-
fn get_slice_pos(slice_pos: &BigInt, len: usize) -> usize {
81-
if let Some(pos) = slice_pos.to_i32() {
82-
if let Some(index) = PyList::get_pos(pos, len) {
83-
// within bounds
84-
return index;
85-
}
86-
}
87-
88-
if slice_pos.is_negative() {
89-
// slice past start bound, round to start
90-
0
91-
} else {
92-
// slice past end bound, round to end
93-
len
94-
}
95-
}
96-
97-
fn get_slice_range(start: &Option<BigInt>, stop: &Option<BigInt>, len: usize) -> Range<usize> {
98-
let start = start
99-
.as_ref()
100-
.map(|x| PyList::get_slice_pos(x, len))
101-
.unwrap_or(0);
102-
let stop = stop
103-
.as_ref()
104-
.map(|x| PyList::get_slice_pos(x, len))
105-
.unwrap_or_else(|| len);
106-
107-
start..stop
108-
}
109-
11065
pub(crate) fn get_byte_inner(
11166
&self,
11267
vm: &VirtualMachine,
@@ -273,9 +228,9 @@ impl PyList {
273228
}
274229
}
275230

276-
fn setindex(&self, index: i32, value: PyObjectRef, vm: &VirtualMachine) -> PyResult {
231+
fn setindex(&self, index: isize, value: PyObjectRef, vm: &VirtualMachine) -> PyResult {
277232
let mut elements = self.borrow_elements_mut();
278-
if let Some(pos_index) = PyList::get_pos(index, elements.len()) {
233+
if let Some(pos_index) = get_pos(index, elements.len()) {
279234
elements[pos_index] = value;
280235
Ok(vm.get_none())
281236
} else {
@@ -297,16 +252,16 @@ impl PyList {
297252
if step.is_zero() {
298253
Err(vm.new_value_error("slice step cannot be zero".to_owned()))
299254
} else if step.is_positive() {
300-
let range = PyList::get_slice_range(&start, &stop, elements.len());
255+
let range = get_slice_range(&start, &stop, elements.len());
301256
if range.start < range.end {
302-
match step.to_i32() {
257+
match step.to_isize() {
303258
Some(1) => PyList::_set_slice(elements, range, items, vm),
304259
Some(num) => {
305260
// assign to extended slice
306261
PyList::_set_stepped_slice(elements, range, num as usize, items, vm)
307262
}
308263
None => {
309-
// not sure how this is reached, step too big for i32?
264+
// not sure how this is reached, step too big for isize?
310265
// then step is bigger than the than len of the list, no question
311266
#[allow(clippy::range_plus_one)]
312267
PyList::_set_stepped_slice(
@@ -339,13 +294,13 @@ impl PyList {
339294
x + 1
340295
}
341296
});
342-
let range = PyList::get_slice_range(&stop, &start, elements.len());
343-
match (-step).to_i32() {
297+
let range = get_slice_range(&stop, &start, elements.len());
298+
match (-step).to_isize() {
344299
Some(num) => {
345300
PyList::_set_stepped_slice_reverse(elements, range, num as usize, items, vm)
346301
}
347302
None => {
348-
// not sure how this is reached, step too big for i32?
303+
// not sure how this is reached, step too big for isize?
349304
// then step is bigger than the than len of the list no question
350305
PyList::_set_stepped_slice_reverse(
351306
elements,
@@ -638,9 +593,9 @@ impl PyList {
638593
}
639594
}
640595

641-
fn delindex(&self, index: i32, vm: &VirtualMachine) -> PyResult<()> {
596+
fn delindex(&self, index: isize, vm: &VirtualMachine) -> PyResult<()> {
642597
let mut elements = self.borrow_elements_mut();
643-
if let Some(pos_index) = PyList::get_pos(index, elements.len()) {
598+
if let Some(pos_index) = get_pos(index, elements.len()) {
644599
elements.remove(pos_index);
645600
Ok(())
646601
} else {
@@ -657,10 +612,10 @@ impl PyList {
657612
if step.is_zero() {
658613
Err(vm.new_value_error("slice step cannot be zero".to_owned()))
659614
} else if step.is_positive() {
660-
let range = PyList::get_slice_range(&start, &stop, elements.len());
615+
let range = get_slice_range(&start, &stop, elements.len());
661616
if range.start < range.end {
662617
#[allow(clippy::range_plus_one)]
663-
match step.to_i32() {
618+
match step.to_isize() {
664619
Some(1) => {
665620
PyList::_del_slice(elements, range);
666621
Ok(())
@@ -695,9 +650,9 @@ impl PyList {
695650
x + 1
696651
}
697652
});
698-
let range = PyList::get_slice_range(&stop, &start, elements.len());
653+
let range = get_slice_range(&stop, &start, elements.len());
699654
if range.start < range.end {
700-
match (-step).to_i32() {
655+
match (-step).to_isize() {
701656
Some(1) => {
702657
PyList::_del_slice(elements, range);
703658
Ok(())

0 commit comments

Comments
 (0)