Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10496,7 +10496,6 @@ class CustomerModel(ModelBase, init=False):


class NoDefaultTests(BaseTestCase):
@unittest.expectedFailure # TODO: RUSTPYTHON
def test_pickling(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
s = pickle.dumps(NoDefault, proto)
Expand Down
4 changes: 2 additions & 2 deletions crates/stdlib/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ mod _sqlite {
let text2 = vm.ctx.new_str(text2);

let val = callable.call((text1, text2), vm)?;
let Some(val) = val.to_number().index(vm) else {
let Some(val) = val.number().index(vm) else {
return Ok(0);
};

Expand Down Expand Up @@ -2980,7 +2980,7 @@ mod _sqlite {
fn bind_parameters(self, parameters: &PyObject, vm: &VirtualMachine) -> PyResult<()> {
if let Some(dict) = parameters.downcast_ref::<PyDict>() {
self.bind_parameters_name(dict, vm)
} else if let Ok(seq) = PySequence::try_protocol(parameters, vm) {
} else if let Ok(seq) = parameters.try_sequence(vm) {
self.bind_parameters_sequence(seq, vm)
} else {
Err(new_programming_error(
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl PyObjectRef {
return Ok(false);
}
let rs_bool = if let Some(nb_bool) = self.class().slots.as_number.boolean.load() {
nb_bool(self.as_object().to_number(), vm)?
nb_bool(self.as_object().number(), vm)?
} else {
// TODO: Fully implement AsNumber and remove this block
match vm.get_method(self.clone(), identifier!(vm, __bool__)) {
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/classmethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl PyClassMethod {
}

#[pyclass(
with(GetDescriptor, Constructor, Representable),
with(GetDescriptor, Constructor, Initializer, Representable),
flags(BASETYPE, HAS_DICT)
)]
impl PyClassMethod {
Expand Down
4 changes: 2 additions & 2 deletions crates/vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ impl ViewSetOps for PyDictKeys {}
impl PyDictKeys {
#[pymethod]
fn __contains__(zelf: PyObjectRef, key: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
zelf.to_sequence().contains(&key, vm)
zelf.sequence_unchecked().contains(&key, vm)
}

#[pygetset]
Expand Down Expand Up @@ -1210,7 +1210,7 @@ impl ViewSetOps for PyDictItems {}
impl PyDictItems {
#[pymethod]
fn __contains__(zelf: PyObjectRef, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
zelf.to_sequence().contains(&needle, vm)
zelf.sequence_unchecked().contains(&needle, vm)
}
#[pygetset]
fn mapping(zelf: PyRef<Self>) -> PyMappingProxy {
Expand Down
18 changes: 4 additions & 14 deletions crates/vm/src/builtins/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
class::PyClassImpl,
function::ArgCallable,
object::{Traverse, TraverseFn},
protocol::{PyIterReturn, PySequence, PySequenceMethods},
protocol::PyIterReturn,
types::{IterNext, Iterable, SelfIter},
};
use rustpython_common::{
Expand Down Expand Up @@ -177,9 +177,6 @@ pub fn builtins_reversed(vm: &VirtualMachine) -> &PyObject {
#[pyclass(module = false, name = "iterator", traverse)]
#[derive(Debug)]
pub struct PySequenceIterator {
// cached sequence methods
#[pytraverse(skip)]
seq_methods: &'static PySequenceMethods,
internal: PyMutex<PositionIterInternal<PyObjectRef>>,
}

Expand All @@ -193,9 +190,8 @@ impl PyPayload for PySequenceIterator {
#[pyclass(with(IterNext, Iterable))]
impl PySequenceIterator {
pub fn new(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
let seq = PySequence::try_protocol(&obj, vm)?;
let _seq = obj.try_sequence(vm)?;
Ok(Self {
seq_methods: seq.methods,
internal: PyMutex::new(PositionIterInternal::new(obj, 0)),
})
}
Expand All @@ -204,10 +200,7 @@ impl PySequenceIterator {
fn __length_hint__(&self, vm: &VirtualMachine) -> PyObjectRef {
let internal = self.internal.lock();
if let IterStatus::Active(obj) = &internal.status {
let seq = PySequence {
obj,
methods: self.seq_methods,
};
let seq = obj.sequence_unchecked();
seq.length(vm)
.map(|x| PyInt::from(x).into_pyobject(vm))
.unwrap_or_else(|_| vm.ctx.not_implemented())
Expand All @@ -231,10 +224,7 @@ impl SelfIter for PySequenceIterator {}
impl IterNext for PySequenceIterator {
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
zelf.internal.lock().next(|obj, pos| {
let seq = PySequence {
obj,
methods: zelf.seq_methods,
};
let seq = obj.sequence_unchecked();
PyIterReturn::from_getitem_result(seq.get_item(pos as isize, vm), vm)
})
}
Expand Down
6 changes: 5 additions & 1 deletion crates/vm/src/builtins/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,11 @@ where
} else {
let iter = obj.to_owned().get_iter(vm)?;
let iter = iter.iter::<PyObjectRef>(vm)?;
let len = obj.to_sequence().length_opt(vm).transpose()?.unwrap_or(0);
let len = obj
.sequence_unchecked()
.length_opt(vm)
.transpose()?
.unwrap_or(0);
let mut v = Vec::with_capacity(len);
for x in iter {
v.push(f(x?)?);
Expand Down
10 changes: 4 additions & 6 deletions crates/vm/src/builtins/mappingproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
convert::ToPyObject,
function::{ArgMapping, OptionalArg, PyComparisonValue},
object::{Traverse, TraverseFn},
protocol::{PyMapping, PyMappingMethods, PyNumberMethods, PySequenceMethods},
protocol::{PyMappingMethods, PyNumberMethods, PySequenceMethods},
types::{
AsMapping, AsNumber, AsSequence, Comparable, Constructor, Iterable, PyComparisonOp,
Representable,
Expand Down Expand Up @@ -62,14 +62,12 @@ impl Constructor for PyMappingProxy {
type Args = PyObjectRef;

fn py_new(_cls: &Py<PyType>, mapping: Self::Args, vm: &VirtualMachine) -> PyResult<Self> {
if let Some(methods) = PyMapping::find_methods(&mapping)
if mapping.mapping_unchecked().check()
&& !mapping.downcastable::<PyList>()
&& !mapping.downcastable::<PyTuple>()
{
return Ok(Self {
mapping: MappingProxyInner::Mapping(ArgMapping::with_methods(mapping, unsafe {
methods.borrow_static()
})),
mapping: MappingProxyInner::Mapping(ArgMapping::new(mapping)),
});
}
Err(vm.new_type_error(format!(
Expand Down Expand Up @@ -124,7 +122,7 @@ impl PyMappingProxy {
MappingProxyInner::Class(class) => Ok(key
.as_interned_str(vm)
.is_some_and(|key| class.attributes.read().contains_key(key))),
MappingProxyInner::Mapping(mapping) => mapping.to_sequence().contains(key, vm),
MappingProxyInner::Mapping(mapping) => mapping.sequence_unchecked().contains(key, vm),
}
}

Expand Down
5 changes: 1 addition & 4 deletions crates/vm/src/builtins/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,7 @@ impl PyBaseObject {
}
}
PyComparisonOp::Ne => {
let cmp = zelf
.class()
.mro_find_map(|cls| cls.slots.richcompare.load())
.unwrap();
let cmp = zelf.class().slots.richcompare.load().unwrap();
let value = match cmp(zelf, other, PyComparisonOp::Eq, vm)? {
Either::A(obj) => PyArithmeticValue::from_object(vm, obj)
.map(|obj| obj.try_to_bool(vm))
Expand Down
Loading
Loading