Skip to content

Commit 209d6be

Browse files
authored
Merge pull request RustPython#2247 from youknowone/vm-len
vm._len and objsequence.rs -> sliceable.rs
2 parents ab3aa89 + 39058e2 commit 209d6be

20 files changed

+229
-240
lines changed

Lib/test/test_bytes.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -934,8 +934,6 @@ def test_translate(self):
934934
class BytesTest(BaseBytesTest, unittest.TestCase):
935935
type2test = bytes
936936

937-
# TODO: RUSTPYTHON
938-
@unittest.expectedFailure
939937
def test_getitem_error(self):
940938
b = b'python'
941939
msg = "byte indices must be integers or slices"
@@ -1133,8 +1131,6 @@ class BufferBlocked(bytearray):
11331131
class ByteArrayTest(BaseBytesTest, unittest.TestCase):
11341132
type2test = bytearray
11351133

1136-
# TODO: RUSTPYTHON
1137-
@unittest.expectedFailure
11381134
def test_getitem_error(self):
11391135
b = bytearray(b'python')
11401136
msg = "bytearray indices must be integers or slices"

vm/src/builtins.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ mod decl {
2828
use crate::obj::objint::{self, PyIntRef};
2929
use crate::obj::objiter;
3030
use crate::obj::objlist::{PyList, SortOptions};
31-
use crate::obj::objsequence;
3231
use crate::obj::objstr::{PyStr, PyStrRef};
3332
use crate::obj::objtype::{self, PyTypeRef};
3433
use crate::pyobject::{
@@ -37,6 +36,7 @@ mod decl {
3736
};
3837
use crate::readline::{Readline, ReadlineResult};
3938
use crate::scope::Scope;
39+
use crate::sliceable;
4040
use crate::slots::PyComparisonOp;
4141
#[cfg(feature = "rustpython-parser")]
4242
use crate::stdlib::ast;
@@ -444,7 +444,12 @@ mod decl {
444444

445445
#[pyfunction]
446446
fn len(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
447-
objsequence::len(&obj, vm)
447+
vm._len(&obj).unwrap_or_else(|| {
448+
Err(vm.new_type_error(format!(
449+
"object of type '{}' has no len()",
450+
obj.lease_class().name
451+
)))
452+
})
448453
}
449454

450455
#[pyfunction]

vm/src/bytesinner.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ use crate::obj::objbytes::PyBytes;
1111
use crate::obj::objint::{self, PyInt, PyIntRef};
1212
use crate::obj::objlist::PyList;
1313
use crate::obj::objmemory::PyMemoryView;
14-
use crate::obj::objsequence::{
15-
get_saturated_pos, PySliceableSequence, PySliceableSequenceMut, SequenceIndex,
16-
};
1714
use crate::obj::objsingletons::PyNoneRef;
1815
use crate::obj::objslice::PySliceRef;
1916
use crate::obj::objstr::{self, PyStr, PyStrRef};
2017
use crate::pyobject::{
2118
BorrowValue, Either, PyComparisonValue, PyIterable, PyIterator, PyObjectRef, PyResult,
2219
TryFromObject, TypeProtocol,
2320
};
21+
use crate::sliceable::{PySliceableSequence, PySliceableSequenceMut, SequenceIndex};
2422
use crate::slots::PyComparisonOp;
2523
use crate::vm::VirtualMachine;
2624
use rustpython_common::hash;
@@ -297,19 +295,17 @@ impl PyBytesInner {
297295
})
298296
}
299297

300-
pub fn getitem(&self, needle: SequenceIndex, vm: &VirtualMachine) -> PyResult {
301-
match needle {
302-
SequenceIndex::Int(int) => {
303-
if let Some(idx) = self.elements.get_pos(int) {
304-
Ok(vm.ctx.new_int(self.elements[idx]))
305-
} else {
306-
Err(vm.new_index_error("index out of range".to_owned()))
307-
}
308-
}
309-
SequenceIndex::Slice(slice) => {
310-
Ok(vm.ctx.new_bytes(self.elements.get_slice_items(vm, &slice)?))
311-
}
312-
}
298+
pub fn getitem(
299+
&self,
300+
name: &'static str,
301+
needle: PyObjectRef,
302+
vm: &VirtualMachine,
303+
) -> PyResult {
304+
let obj = match self.elements.get_item(vm, needle, name)? {
305+
Either::A(byte) => vm.new_pyobj(byte),
306+
Either::B(bytes) => vm.ctx.new_bytes(bytes),
307+
};
308+
Ok(obj)
313309
}
314310

315311
pub fn setindex(
@@ -318,7 +314,7 @@ impl PyBytesInner {
318314
object: PyObjectRef,
319315
vm: &VirtualMachine,
320316
) -> PyResult<()> {
321-
if let Some(idx) = self.elements.get_pos(int) {
317+
if let Some(idx) = self.elements.wrap_index(int) {
322318
let value = Self::value_try_from_object(vm, object)?;
323319
self.elements[idx] = value;
324320
Ok(())
@@ -374,7 +370,7 @@ impl PyBytesInner {
374370
pub fn delitem(&mut self, needle: SequenceIndex, vm: &VirtualMachine) -> PyResult<()> {
375371
match needle {
376372
SequenceIndex::Int(int) => {
377-
if let Some(idx) = self.elements.get_pos(int) {
373+
if let Some(idx) = self.elements.wrap_index(int) {
378374
self.elements.remove(idx);
379375
Ok(())
380376
} else {
@@ -390,7 +386,7 @@ impl PyBytesInner {
390386
}
391387

392388
pub fn pop(&mut self, index: isize, vm: &VirtualMachine) -> PyResult<u8> {
393-
if let Some(index) = self.elements.get_pos(index) {
389+
if let Some(index) = self.elements.wrap_index(index) {
394390
Ok(self.elements.remove(index))
395391
} else {
396392
Err(vm.new_index_error("index out of range".to_owned()))
@@ -404,7 +400,7 @@ impl PyBytesInner {
404400
vm: &VirtualMachine,
405401
) -> PyResult<()> {
406402
let value = Self::value_try_from_object(vm, object)?;
407-
let index = get_saturated_pos(index, self.len());
403+
let index = self.elements.saturate_index(index);
408404
self.elements.insert(index, value);
409405
Ok(())
410406
}

vm/src/obj/objcoroinner.rs renamed to vm/src/coroutine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use super::objtype::{self, PyTypeRef};
21
use crate::exceptions::{self, PyBaseExceptionRef};
32
use crate::frame::{ExecutionResult, FrameRef};
3+
use crate::obj::objtype::{self, PyTypeRef};
44
use crate::pyobject::{PyObjectRef, PyResult};
55
use crate::vm::VirtualMachine;
66

vm/src/frame.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use itertools::Itertools;
77
use crate::builtins::builtin_isinstance;
88
use crate::bytecode;
99
use crate::common::lock::PyMutex;
10+
use crate::coroutine::Coro;
1011
use crate::exceptions::{self, ExceptionCtor, PyBaseExceptionRef};
1112
use crate::function::PyFuncArgs;
1213
use crate::obj::objasyncgenerator::PyAsyncGenWrappedValue;
1314
use crate::obj::objcode::PyCodeRef;
14-
use crate::obj::objcoroinner::Coro;
1515
use crate::obj::objcoroutine::PyCoroutine;
1616
use crate::obj::objdict::{PyDict, PyDictRef};
1717
use crate::obj::objgenerator::PyGenerator;

vm/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ mod builtins;
4848
mod bytesinner;
4949
pub mod byteslike;
5050
pub mod cformat;
51+
mod coroutine;
5152
mod dictdatatype;
5253
#[cfg(feature = "rustpython-compiler")]
5354
pub mod eval;
@@ -65,6 +66,7 @@ mod pyobjectrc;
6566
pub mod readline;
6667
pub mod scope;
6768
mod sequence;
69+
mod sliceable;
6870
pub mod slots;
6971
pub mod stdlib;
7072
mod sysmodule;

vm/src/obj/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub mod objbytes;
88
pub mod objclassmethod;
99
pub mod objcode;
1010
pub mod objcomplex;
11-
pub mod objcoroinner;
1211
pub mod objcoroutine;
1312
pub mod objdict;
1413
pub mod objenumerate;
@@ -29,7 +28,6 @@ pub mod objnamespace;
2928
pub mod objobject;
3029
pub mod objproperty;
3130
pub mod objrange;
32-
pub mod objsequence;
3331
pub mod objset;
3432
pub mod objsingletons;
3533
pub mod objslice;

vm/src/obj/objasyncgenerator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::objcode::PyCodeRef;
2-
use super::objcoroinner::{Coro, Variant};
32
use super::objtype::{self, PyTypeRef};
3+
use crate::coroutine::{Coro, Variant};
44
use crate::exceptions::PyBaseExceptionRef;
55
use crate::frame::FrameRef;
66
use crate::function::OptionalArg;

vm/src/obj/objbytearray.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::mem::size_of;
55

66
use super::objint::PyIntRef;
77
use super::objiter;
8-
use super::objsequence::SequenceIndex;
98
use super::objstr::PyStrRef;
109
use super::objtype::PyTypeRef;
1110
use crate::anystr::{self, AnyStr};
@@ -22,6 +21,7 @@ use crate::pyobject::{
2221
BorrowValue, Either, IdProtocol, IntoPyObject, PyClassImpl, PyComparisonValue, PyContext,
2322
PyIterable, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
2423
};
24+
use crate::sliceable::SequenceIndex;
2525
use crate::slots::{Comparable, Hashable, PyComparisonOp, Unhashable};
2626
use crate::vm::VirtualMachine;
2727

@@ -152,8 +152,8 @@ impl PyByteArray {
152152
}
153153

154154
#[pymethod(name = "__getitem__")]
155-
fn getitem(&self, needle: SequenceIndex, vm: &VirtualMachine) -> PyResult {
156-
self.borrow_value().getitem(needle, vm)
155+
fn getitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
156+
self.borrow_value().getitem("bytearray", needle, vm)
157157
}
158158

159159
#[pymethod(name = "__setitem__")]

vm/src/obj/objbytes.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::ops::Deref;
55

66
use super::objint::PyIntRef;
77
use super::objiter;
8-
use super::objsequence::SequenceIndex;
98
use super::objstr::PyStrRef;
109
use super::objtype::PyTypeRef;
1110
use crate::anystr::{self, AnyStr};
@@ -139,8 +138,8 @@ impl PyBytes {
139138
}
140139

141140
#[pymethod(name = "__getitem__")]
142-
fn getitem(&self, needle: SequenceIndex, vm: &VirtualMachine) -> PyResult {
143-
self.inner.getitem(needle, vm)
141+
fn getitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
142+
self.inner.getitem("byte", needle, vm)
144143
}
145144

146145
#[pymethod(name = "isalnum")]

vm/src/obj/objcoroutine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::objcode::PyCodeRef;
2-
use super::objcoroinner::{Coro, Variant};
32
use super::objstr::PyStrRef;
43
use super::objtype::PyTypeRef;
4+
use crate::coroutine::{Coro, Variant};
55
use crate::frame::FrameRef;
66
use crate::function::OptionalArg;
77
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};

vm/src/obj/objgenerator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*/
44

55
use super::objcode::PyCodeRef;
6-
use super::objcoroinner::{Coro, Variant};
76
use super::objtype::PyTypeRef;
7+
use crate::coroutine::{Coro, Variant};
88
use crate::frame::FrameRef;
99
use crate::function::OptionalArg;
1010
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};

vm/src/obj/objiter.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crossbeam_utils::atomic::AtomicCell;
66
use num_traits::Signed;
77

88
use super::objint::{self, PyInt};
9-
use super::objsequence;
109
use super::objtype::{self, PyTypeRef};
1110
use crate::exceptions::PyBaseExceptionRef;
1211
use crate::pyobject::{
@@ -103,7 +102,7 @@ pub fn stop_iter_value(vm: &VirtualMachine, exc: &PyBaseExceptionRef) -> PyResul
103102
}
104103

105104
pub fn length_hint(vm: &VirtualMachine, iter: PyObjectRef) -> PyResult<Option<usize>> {
106-
if let Some(len) = objsequence::opt_len(&iter, vm) {
105+
if let Some(len) = vm._len(&iter) {
107106
match len {
108107
Ok(len) => return Ok(Some(len)),
109108
Err(e) => {
@@ -203,7 +202,7 @@ impl PySequenceIterator {
203202
let hint = if self.reversed {
204203
pos + 1
205204
} else {
206-
let len = objsequence::opt_len(&self.obj, vm).unwrap_or_else(|| {
205+
let len = vm._len(&self.obj).unwrap_or_else(|| {
207206
Err(vm.new_type_error("sequence has no __len__ method".to_owned()))
208207
})?;
209208
len as isize - pos

vm/src/obj/objlist.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ use num_traits::ToPrimitive;
88

99
use super::objint::PyIntRef;
1010
use super::objiter;
11-
use super::objsequence::{
12-
get_item, get_pos, get_saturated_pos, PySliceableSequenceMut, SequenceIndex,
13-
};
1411
use super::objslice::PySliceRef;
1512
use super::objtype::PyTypeRef;
1613
use crate::bytesinner;
@@ -21,6 +18,7 @@ use crate::pyobject::{
2118
PyResult, PyValue, TryFromObject, TypeProtocol,
2219
};
2320
use crate::sequence::{self, SimpleSeq};
21+
use crate::sliceable::{PySliceableSequence, PySliceableSequenceMut, SequenceIndex};
2422
use crate::slots::{Comparable, Hashable, PyComparisonOp, Unhashable};
2523
use crate::vm::{ReprGuard, VirtualMachine};
2624

@@ -120,7 +118,7 @@ impl PyList {
120118
#[pymethod]
121119
pub(crate) fn insert(&self, position: isize, element: PyObjectRef) {
122120
let mut elements = self.borrow_value_mut();
123-
let position = get_saturated_pos(position, elements.len());
121+
let position = elements.saturate_index(position);
124122
elements.insert(position, element);
125123
}
126124

@@ -192,12 +190,11 @@ impl PyList {
192190

193191
#[pymethod(name = "__getitem__")]
194192
fn getitem(zelf: PyRef<Self>, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
195-
Ok(
196-
match get_item(vm, zelf.as_object(), &zelf.borrow_value(), needle)? {
197-
Either::A(obj) => obj,
198-
Either::B(vec) => vm.ctx.new_list(vec),
199-
},
200-
)
193+
let result = match zelf.borrow_value().get_item(vm, needle, "list")? {
194+
Either::A(obj) => obj,
195+
Either::B(vec) => vm.ctx.new_list(vec),
196+
};
197+
Ok(result)
201198
}
202199

203200
#[pymethod(name = "__iter__")]
@@ -228,7 +225,7 @@ impl PyList {
228225

229226
fn setindex(&self, index: isize, mut value: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
230227
let mut elements = self.borrow_value_mut();
231-
if let Some(pos_index) = get_pos(index, elements.len()) {
228+
if let Some(pos_index) = elements.wrap_index(index) {
232229
std::mem::swap(&mut elements[pos_index], &mut value);
233230
Ok(())
234231
} else {
@@ -361,7 +358,7 @@ impl PyList {
361358
fn delindex(&self, index: isize, vm: &VirtualMachine) -> PyResult<()> {
362359
let removed = {
363360
let mut elements = self.borrow_value_mut();
364-
if let Some(pos_index) = get_pos(index, elements.len()) {
361+
if let Some(pos_index) = elements.wrap_index(index) {
365362
// defer delete out of borrow
366363
Ok(elements.remove(pos_index))
367364
} else {

0 commit comments

Comments
 (0)