mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Fix sliceable error messages to show its type names
This commit is contained in:
@@ -26,8 +26,8 @@ use crate::slots::{
|
||||
use crate::utils::Either;
|
||||
use crate::vm::VirtualMachine;
|
||||
use crate::{
|
||||
IdProtocol, IntoPyObject, PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef,
|
||||
PyRef, PyResult, PyValue, TypeProtocol,
|
||||
IdProtocol, IntoPyObject, PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyIterable,
|
||||
PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
|
||||
};
|
||||
use bstr::ByteSlice;
|
||||
use crossbeam_utils::atomic::AtomicCell;
|
||||
@@ -151,11 +151,11 @@ impl PyByteArray {
|
||||
#[pymethod(magic)]
|
||||
fn setitem(
|
||||
zelf: PyRef<Self>,
|
||||
needle: SequenceIndex,
|
||||
needle: PyObjectRef,
|
||||
value: PyObjectRef,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<()> {
|
||||
match needle {
|
||||
match SequenceIndex::try_from_object_for(vm, needle, Self::NAME)? {
|
||||
SequenceIndex::Int(i) => {
|
||||
let value = value_from_object(vm, &value)?;
|
||||
let mut elements = zelf.borrow_buf_mut();
|
||||
@@ -192,7 +192,7 @@ impl PyByteArray {
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn getitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.inner().getitem("bytearray", needle, vm)
|
||||
self.inner().getitem(Self::NAME, needle, vm)
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
|
||||
@@ -151,7 +151,7 @@ impl PyBytes {
|
||||
|
||||
#[pymethod(name = "__getitem__")]
|
||||
fn getitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.inner.getitem("byte", needle, vm)
|
||||
self.inner.getitem("byte", needle, vm) // byte != Self::NAME
|
||||
}
|
||||
|
||||
#[pymethod(name = "isalnum")]
|
||||
|
||||
@@ -23,8 +23,8 @@ use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter, Unhas
|
||||
use crate::utils::Either;
|
||||
use crate::vm::{ReprGuard, VirtualMachine};
|
||||
use crate::{
|
||||
PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, PyRef, PyResult, PyValue,
|
||||
TryFromObject, TypeProtocol,
|
||||
PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyIterable, PyObjectRef, PyRef,
|
||||
PyResult, PyValue, TryFromObject, TypeProtocol,
|
||||
};
|
||||
|
||||
/// Built-in mutable sequence.
|
||||
@@ -172,7 +172,7 @@ impl PyList {
|
||||
|
||||
#[pymethod(name = "__getitem__")]
|
||||
fn getitem(zelf: PyRef<Self>, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
let result = match zelf.borrow_vec().get_item(vm, needle, "list")? {
|
||||
let result = match zelf.borrow_vec().get_item(vm, needle, Self::NAME)? {
|
||||
Either::A(obj) => obj,
|
||||
Either::B(vec) => vm.ctx.new_list(vec),
|
||||
};
|
||||
@@ -182,11 +182,11 @@ impl PyList {
|
||||
#[pymethod(name = "__setitem__")]
|
||||
fn setitem(
|
||||
&self,
|
||||
subscript: SequenceIndex,
|
||||
needle: PyObjectRef,
|
||||
value: PyObjectRef,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<()> {
|
||||
match subscript {
|
||||
match SequenceIndex::try_from_object_for(vm, needle, Self::NAME)? {
|
||||
SequenceIndex::Int(index) => self.setindex(index, value, vm),
|
||||
SequenceIndex::Slice(slice) => {
|
||||
if let Ok(sec) = PyIterable::try_from_object(vm, value) {
|
||||
|
||||
@@ -23,8 +23,8 @@ use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter};
|
||||
use crate::utils::Either;
|
||||
use crate::VirtualMachine;
|
||||
use crate::{
|
||||
IdProtocol, IntoPyObject, ItemProtocol, PyClassImpl, PyComparisonValue, PyContext, PyIterable,
|
||||
PyObjectRef, PyRef, PyResult, PyValue, TryIntoRef, TypeProtocol,
|
||||
IdProtocol, IntoPyObject, ItemProtocol, PyClassDef, PyClassImpl, PyComparisonValue, PyContext,
|
||||
PyIterable, PyObjectRef, PyRef, PyResult, PyValue, TryIntoRef, TypeProtocol,
|
||||
};
|
||||
use rustpython_common::atomic::{self, PyAtomic, Radium};
|
||||
use rustpython_common::hash;
|
||||
@@ -254,7 +254,7 @@ impl PyStr {
|
||||
|
||||
#[pymethod(name = "__getitem__")]
|
||||
fn getitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
let s = match self.get_item(vm, needle, "string")? {
|
||||
let s = match self.get_item(vm, needle, Self::NAME)? {
|
||||
Either::A(ch) => ch.to_string(),
|
||||
Either::B(s) => s,
|
||||
};
|
||||
|
||||
@@ -19,8 +19,9 @@ use crate::slots::{Comparable, Hashable, Iterable, PyComparisonOp, PyIter};
|
||||
use crate::utils::Either;
|
||||
use crate::vm::{ReprGuard, VirtualMachine};
|
||||
use crate::{
|
||||
IdProtocol, IntoPyObject, PyArithmaticValue, PyClassImpl, PyComparisonValue, PyContext,
|
||||
PyObjectRef, PyRef, PyResult, PyValue, TransmuteFromObject, TryFromObject, TypeProtocol,
|
||||
IdProtocol, IntoPyObject, PyArithmaticValue, PyClassDef, PyClassImpl, PyComparisonValue,
|
||||
PyContext, PyObjectRef, PyRef, PyResult, PyValue, TransmuteFromObject, TryFromObject,
|
||||
TypeProtocol,
|
||||
};
|
||||
|
||||
/// tuple() -> empty tuple
|
||||
@@ -182,7 +183,7 @@ impl PyTuple {
|
||||
|
||||
#[pymethod(name = "__getitem__")]
|
||||
fn getitem(zelf: PyRef<Self>, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
let result = match zelf.elements.as_ref().get_item(vm, needle, "tuple")? {
|
||||
let result = match zelf.elements.as_ref().get_item(vm, needle, Self::NAME)? {
|
||||
Either::A(obj) => obj,
|
||||
Either::B(vec) => vm.ctx.new_tuple(vec),
|
||||
};
|
||||
|
||||
@@ -335,11 +335,11 @@ impl PyBytesInner {
|
||||
|
||||
pub fn getitem(
|
||||
&self,
|
||||
name: &'static str,
|
||||
owner_type: &'static str,
|
||||
needle: PyObjectRef,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult {
|
||||
let obj = match self.elements.get_item(vm, needle, name)? {
|
||||
let obj = match self.elements.get_item(vm, needle, owner_type)? {
|
||||
Either::A(byte) => vm.new_pyobj(byte),
|
||||
Either::B(bytes) => vm.ctx.new_bytes(bytes),
|
||||
};
|
||||
|
||||
@@ -336,7 +336,7 @@ pub enum SequenceIndex {
|
||||
}
|
||||
|
||||
impl SequenceIndex {
|
||||
fn try_from_object_for(
|
||||
pub fn try_from_object_for(
|
||||
vm: &VirtualMachine,
|
||||
obj: PyObjectRef,
|
||||
owner_type: &'static str,
|
||||
|
||||
Reference in New Issue
Block a user