X Tutup
Skip to content

Commit fbf02df

Browse files
committed
remove len
1 parent 48d135f commit fbf02df

File tree

10 files changed

+8
-31
lines changed

10 files changed

+8
-31
lines changed

crates/vm/src/builtins/bool.rs

Lines changed: 7 additions & 21 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 {
@@ -50,29 +49,16 @@ impl PyObjectRef {
5049
return nb_bool(self.as_object().number(), vm);
5150
}
5251

53-
// 2. Try sq_length slot
54-
if let Some(sq_length) = slots.as_sequence.length.load() {
55-
let len = sq_length(self.as_object().sequence_unchecked(), vm)?;
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)?;
5655
return Ok(len != 0);
5756
}
5857

59-
// 3. Fallback for types not yet using slots
60-
// TODO: Remove this block when all types implement AsSequence
61-
if let Some(method_or_err) = vm.get_method(self, identifier!(vm, __len__)) {
62-
let method = method_or_err?;
63-
let len_obj = method.call((), vm)?;
64-
let int_obj = len_obj.downcast_ref::<PyInt>().ok_or_else(|| {
65-
vm.new_type_error(format!(
66-
"'{}' object cannot be interpreted as an integer",
67-
len_obj.class().name()
68-
))
69-
})?;
70-
71-
let len_val = int_obj.as_bigint();
72-
if len_val.sign() == Sign::Minus {
73-
return Err(vm.new_value_error("__len__() should return >= 0"));
74-
}
75-
return Ok(!len_val.is_zero());
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);
7662
}
7763

7864
// 4. Default: objects without __bool__ or __len__ are truthy

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 {

crates/vm/src/builtins/tuple.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ impl PyTuple {
298298
}
299299

300300
#[inline]
301-
#[pymethod]
302301
pub const fn __len__(&self) -> usize {
303302
self.elements.len()
304303
}

crates/vm/src/stdlib/collections.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ mod _collections {
350350
Ok(zelf)
351351
}
352352

353-
#[pymethod]
354353
fn __len__(&self) -> usize {
355354
self.borrow_deque().len()
356355
}

crates/vm/src/stdlib/ctypes/array.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,6 @@ impl PyCArray {
10871087
Ok(())
10881088
}
10891089

1090-
#[pymethod]
10911090
fn __len__(zelf: &Py<Self>, _vm: &VirtualMachine) -> usize {
10921091
zelf.class().stg_info_opt().map_or(0, |i| i.length)
10931092
}

0 commit comments

Comments
 (0)
X Tutup