Skip to content

Commit 39058e2

Browse files
committed
getitem for bytes/bytearray
1 parent dd5f9e3 commit 39058e2

File tree

4 files changed

+15
-22
lines changed

4 files changed

+15
-22
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/bytesinner.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -295,19 +295,17 @@ impl PyBytesInner {
295295
})
296296
}
297297

298-
pub fn getitem(&self, needle: SequenceIndex, vm: &VirtualMachine) -> PyResult {
299-
match needle {
300-
SequenceIndex::Int(int) => {
301-
if let Some(idx) = self.elements.wrap_index(int) {
302-
Ok(vm.ctx.new_int(self.elements[idx]))
303-
} else {
304-
Err(vm.new_index_error("index out of range".to_owned()))
305-
}
306-
}
307-
SequenceIndex::Slice(slice) => {
308-
Ok(vm.ctx.new_bytes(self.elements.get_slice_items(vm, &slice)?))
309-
}
310-
}
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)
311309
}
312310

313311
pub fn setindex(

vm/src/obj/objbytearray.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -19,7 +19,6 @@ use crate::pyobject::{
1919
BorrowValue, Either, IntoPyObject, PyClassImpl, PyComparisonValue, PyContext, PyIterable,
2020
PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
2121
};
22-
use crate::sliceable::SequenceIndex;
2322
use crate::slots::{Comparable, Hashable, PyComparisonOp};
2423
use crate::vm::VirtualMachine;
2524
use rustpython_common::hash::PyHash;
@@ -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")]

0 commit comments

Comments
 (0)