Skip to content

Commit f09f158

Browse files
fermianyouknowone
authored andcommitted
Fix sliceable error messages to show its type names
1 parent 8bfff80 commit f09f158

File tree

7 files changed

+21
-20
lines changed

7 files changed

+21
-20
lines changed

vm/src/builtins/bytearray.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use crate::slots::{
2626
use crate::utils::Either;
2727
use crate::vm::VirtualMachine;
2828
use crate::{
29-
IdProtocol, IntoPyObject, PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef,
30-
PyRef, PyResult, PyValue, TypeProtocol,
29+
IdProtocol, IntoPyObject, PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyIterable,
30+
PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
3131
};
3232
use bstr::ByteSlice;
3333
use crossbeam_utils::atomic::AtomicCell;
@@ -151,11 +151,11 @@ impl PyByteArray {
151151
#[pymethod(magic)]
152152
fn setitem(
153153
zelf: PyRef<Self>,
154-
needle: SequenceIndex,
154+
needle: PyObjectRef,
155155
value: PyObjectRef,
156156
vm: &VirtualMachine,
157157
) -> PyResult<()> {
158-
match needle {
158+
match SequenceIndex::try_from_object_for(vm, needle, Self::NAME)? {
159159
SequenceIndex::Int(i) => {
160160
let value = value_from_object(vm, &value)?;
161161
let mut elements = zelf.borrow_buf_mut();
@@ -192,7 +192,7 @@ impl PyByteArray {
192192

193193
#[pymethod(magic)]
194194
fn getitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
195-
self.inner().getitem("bytearray", needle, vm)
195+
self.inner().getitem(Self::NAME, needle, vm)
196196
}
197197

198198
#[pymethod(magic)]

vm/src/builtins/bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl PyBytes {
151151

152152
#[pymethod(name = "__getitem__")]
153153
fn getitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
154-
self.inner.getitem("byte", needle, vm)
154+
self.inner.getitem("byte", needle, vm) // byte != Self::NAME
155155
}
156156

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

vm/src/builtins/list.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter, Unhas
2323
use crate::utils::Either;
2424
use crate::vm::{ReprGuard, VirtualMachine};
2525
use crate::{
26-
PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, PyRef, PyResult, PyValue,
27-
TryFromObject, TypeProtocol,
26+
PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, PyRef,
27+
PyResult, PyValue, TryFromObject, TypeProtocol,
2828
};
2929

3030
/// Built-in mutable sequence.
@@ -172,7 +172,7 @@ impl PyList {
172172

173173
#[pymethod(name = "__getitem__")]
174174
fn getitem(zelf: PyRef<Self>, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
175-
let result = match zelf.borrow_vec().get_item(vm, needle, "list")? {
175+
let result = match zelf.borrow_vec().get_item(vm, needle, Self::NAME)? {
176176
Either::A(obj) => obj,
177177
Either::B(vec) => vm.ctx.new_list(vec),
178178
};
@@ -182,11 +182,11 @@ impl PyList {
182182
#[pymethod(name = "__setitem__")]
183183
fn setitem(
184184
&self,
185-
subscript: SequenceIndex,
185+
needle: PyObjectRef,
186186
value: PyObjectRef,
187187
vm: &VirtualMachine,
188188
) -> PyResult<()> {
189-
match subscript {
189+
match SequenceIndex::try_from_object_for(vm, needle, Self::NAME)? {
190190
SequenceIndex::Int(index) => self.setindex(index, value, vm),
191191
SequenceIndex::Slice(slice) => {
192192
if let Ok(sec) = PyIterable::try_from_object(vm, value) {

vm/src/builtins/pystr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter};
2323
use crate::utils::Either;
2424
use crate::VirtualMachine;
2525
use crate::{
26-
IdProtocol, IntoPyObject, ItemProtocol, PyClassImpl, PyComparisonValue, PyContext, PyIterable,
27-
PyObjectRef, PyRef, PyResult, PyValue, TryIntoRef, TypeProtocol,
26+
IdProtocol, IntoPyObject, ItemProtocol, PyClassDef, PyClassImpl, PyComparisonValue, PyContext,
27+
PyIterable, PyObjectRef, PyRef, PyResult, PyValue, TryIntoRef, TypeProtocol,
2828
};
2929
use rustpython_common::atomic::{self, PyAtomic, Radium};
3030
use rustpython_common::hash;
@@ -254,7 +254,7 @@ impl PyStr {
254254

255255
#[pymethod(name = "__getitem__")]
256256
fn getitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
257-
let s = match self.get_item(vm, needle, "string")? {
257+
let s = match self.get_item(vm, needle, Self::NAME)? {
258258
Either::A(ch) => ch.to_string(),
259259
Either::B(s) => s,
260260
};

vm/src/builtins/tuple.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter};
1919
use crate::utils::Either;
2020
use crate::vm::{ReprGuard, VirtualMachine};
2121
use crate::{
22-
IdProtocol, IntoPyObject, PyArithmaticValue, PyClassImpl, PyComparisonValue, PyContext,
23-
PyObjectRef, PyRef, PyResult, PyValue, TransmuteFromObject, TryFromObject, TypeProtocol,
22+
IdProtocol, IntoPyObject, PyArithmaticValue, PyClassDef, PyClassImpl, PyComparisonValue,
23+
PyContext, PyObjectRef, PyRef, PyResult, PyValue, TransmuteFromObject, TryFromObject,
24+
TypeProtocol,
2425
};
2526

2627
/// tuple() -> empty tuple
@@ -182,7 +183,7 @@ impl PyTuple {
182183

183184
#[pymethod(name = "__getitem__")]
184185
fn getitem(zelf: PyRef<Self>, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
185-
let result = match zelf.elements.as_ref().get_item(vm, needle, "tuple")? {
186+
let result = match zelf.elements.as_ref().get_item(vm, needle, Self::NAME)? {
186187
Either::A(obj) => obj,
187188
Either::B(vec) => vm.ctx.new_tuple(vec),
188189
};

vm/src/bytesinner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,11 @@ impl PyBytesInner {
335335

336336
pub fn getitem(
337337
&self,
338-
name: &'static str,
338+
owner_type: &'static str,
339339
needle: PyObjectRef,
340340
vm: &VirtualMachine,
341341
) -> PyResult {
342-
let obj = match self.elements.get_item(vm, needle, name)? {
342+
let obj = match self.elements.get_item(vm, needle, owner_type)? {
343343
Either::A(byte) => vm.new_pyobj(byte),
344344
Either::B(bytes) => vm.ctx.new_bytes(bytes),
345345
};

vm/src/sliceable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ pub enum SequenceIndex {
336336
}
337337

338338
impl SequenceIndex {
339-
fn try_from_object_for(
339+
pub fn try_from_object_for(
340340
vm: &VirtualMachine,
341341
obj: PyObjectRef,
342342
owner_type: &'static str,

0 commit comments

Comments
 (0)