Skip to content

Commit 020ff30

Browse files
authored
update_sub_slot, __delitem__, no __bool__, missing rops (#6618)
* fix update_sub_slot * Fix __setitem__ __delitem__ handling * contains * no __bool__ anymore * Add rops to wrapper * Remove pymethod from set * remove len
1 parent e10dc94 commit 020ff30

File tree

14 files changed

+154
-97
lines changed

14 files changed

+154
-97
lines changed

crates/vm/src/builtins/bool.rs

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::{
99
types::{AsNumber, Constructor, Representable},
1010
};
1111
use core::fmt::{Debug, Formatter};
12-
use malachite_bigint::Sign;
1312
use num_traits::Zero;
1413

1514
impl ToPyObject for bool {
@@ -42,46 +41,28 @@ impl PyObjectRef {
4241
if self.is(&vm.ctx.false_value) {
4342
return Ok(false);
4443
}
45-
let rs_bool = if let Some(nb_bool) = self.class().slots.as_number.boolean.load() {
46-
nb_bool(self.as_object().number(), vm)?
47-
} else {
48-
// TODO: Fully implement AsNumber and remove this block
49-
match vm.get_method(self.clone(), identifier!(vm, __bool__)) {
50-
Some(method_or_err) => {
51-
// If descriptor returns Error, propagate it further
52-
let method = method_or_err?;
53-
let bool_obj = method.call((), vm)?;
54-
if !bool_obj.fast_isinstance(vm.ctx.types.bool_type) {
55-
return Err(vm.new_type_error(format!(
56-
"__bool__ should return bool, returned type {}",
57-
bool_obj.class().name()
58-
)));
59-
}
60-
61-
get_value(&bool_obj)
62-
}
63-
None => match vm.get_method(self, identifier!(vm, __len__)) {
64-
Some(method_or_err) => {
65-
let method = method_or_err?;
66-
let bool_obj = method.call((), vm)?;
67-
let int_obj = bool_obj.downcast_ref::<PyInt>().ok_or_else(|| {
68-
vm.new_type_error(format!(
69-
"'{}' object cannot be interpreted as an integer",
70-
bool_obj.class().name()
71-
))
72-
})?;
73-
74-
let len_val = int_obj.as_bigint();
75-
if len_val.sign() == Sign::Minus {
76-
return Err(vm.new_value_error("__len__() should return >= 0"));
77-
}
78-
!len_val.is_zero()
79-
}
80-
None => true,
81-
},
82-
}
83-
};
84-
Ok(rs_bool)
44+
45+
let slots = &self.class().slots;
46+
47+
// 1. Try nb_bool slot first
48+
if let Some(nb_bool) = slots.as_number.boolean.load() {
49+
return nb_bool(self.as_object().number(), vm);
50+
}
51+
52+
// 2. Try mp_length slot (mapping protocol)
53+
if let Some(mp_length) = slots.as_mapping.length.load() {
54+
let len = mp_length(self.as_object().mapping_unchecked(), vm)?;
55+
return Ok(len != 0);
56+
}
57+
58+
// 3. Try sq_length slot (sequence protocol)
59+
if let Some(sq_length) = slots.as_sequence.length.load() {
60+
let len = sq_length(self.as_object().sequence_unchecked(), vm)?;
61+
return Ok(len != 0);
62+
}
63+
64+
// 4. Default: objects without __bool__ or __len__ are truthy
65+
Ok(true)
8566
}
8667
}
8768

crates/vm/src/builtins/bytearray.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ impl PyByteArray {
206206
self.inner().capacity()
207207
}
208208

209-
#[pymethod]
210209
fn __len__(&self) -> usize {
211210
self.borrow_buf().len()
212211
}

crates/vm/src/builtins/bytes.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ impl PyRef<PyBytes> {
205205
)]
206206
impl PyBytes {
207207
#[inline]
208-
#[pymethod]
209208
pub const fn __len__(&self) -> usize {
210209
self.inner.len()
211210
}

crates/vm/src/builtins/dict.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ impl PyDict {
212212
}
213213
}
214214

215-
#[pymethod]
216215
pub fn __len__(&self) -> usize {
217216
self.entries.len()
218217
}
@@ -764,7 +763,6 @@ trait DictView: PyPayload + PyClassDef + Iterable + Representable {
764763
fn dict(&self) -> &Py<PyDict>;
765764
fn item(vm: &VirtualMachine, key: PyObjectRef, value: PyObjectRef) -> PyObjectRef;
766765

767-
#[pymethod]
768766
fn __len__(&self) -> usize {
769767
self.dict().__len__()
770768
}

crates/vm/src/builtins/list.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ impl PyList {
182182
}
183183

184184
#[allow(clippy::len_without_is_empty)]
185-
#[pymethod]
186185
pub fn __len__(&self) -> usize {
187186
self.borrow_vec().len()
188187
}

crates/vm/src/builtins/mappingproxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ impl PyMappingProxy {
173173
PyGenericAlias::from_args(cls, args, vm)
174174
}
175175

176-
#[pymethod]
177176
fn __len__(&self, vm: &VirtualMachine) -> PyResult<usize> {
178177
let obj = self.to_object(vm)?;
179178
obj.length(vm)
@@ -235,6 +234,7 @@ impl AsMapping for PyMappingProxy {
235234
impl AsSequence for PyMappingProxy {
236235
fn as_sequence() -> &'static PySequenceMethods {
237236
static AS_SEQUENCE: LazyLock<PySequenceMethods> = LazyLock::new(|| PySequenceMethods {
237+
length: atomic_func!(|seq, vm| PyMappingProxy::sequence_downcast(seq).__len__(vm)),
238238
contains: atomic_func!(
239239
|seq, target, vm| PyMappingProxy::sequence_downcast(seq)._contains(target, vm)
240240
),

crates/vm/src/builtins/memory.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,6 @@ impl PyMemoryView {
690690
Err(vm.new_type_error("cannot delete memory"))
691691
}
692692

693-
#[pymethod]
694693
fn __len__(&self, vm: &VirtualMachine) -> PyResult<usize> {
695694
self.try_not_released(vm)?;
696695
if self.desc.ndim() == 0 {

0 commit comments

Comments
 (0)