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
63 changes: 22 additions & 41 deletions crates/vm/src/builtins/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::{
types::{AsNumber, Constructor, Representable},
};
use core::fmt::{Debug, Formatter};
use malachite_bigint::Sign;
use num_traits::Zero;

impl ToPyObject for bool {
Expand Down Expand Up @@ -42,46 +41,28 @@ impl PyObjectRef {
if self.is(&vm.ctx.false_value) {
return Ok(false);
}
let rs_bool = if let Some(nb_bool) = self.class().slots.as_number.boolean.load() {
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__)) {
Some(method_or_err) => {
// If descriptor returns Error, propagate it further
let method = method_or_err?;
let bool_obj = method.call((), vm)?;
if !bool_obj.fast_isinstance(vm.ctx.types.bool_type) {
return Err(vm.new_type_error(format!(
"__bool__ should return bool, returned type {}",
bool_obj.class().name()
)));
}

get_value(&bool_obj)
}
None => match vm.get_method(self, identifier!(vm, __len__)) {
Some(method_or_err) => {
let method = method_or_err?;
let bool_obj = method.call((), vm)?;
let int_obj = bool_obj.downcast_ref::<PyInt>().ok_or_else(|| {
vm.new_type_error(format!(
"'{}' object cannot be interpreted as an integer",
bool_obj.class().name()
))
})?;

let len_val = int_obj.as_bigint();
if len_val.sign() == Sign::Minus {
return Err(vm.new_value_error("__len__() should return >= 0"));
}
!len_val.is_zero()
}
None => true,
},
}
};
Ok(rs_bool)

let slots = &self.class().slots;

// 1. Try nb_bool slot first
if let Some(nb_bool) = slots.as_number.boolean.load() {
return nb_bool(self.as_object().number(), vm);
}

// 2. Try mp_length slot (mapping protocol)
if let Some(mp_length) = slots.as_mapping.length.load() {
let len = mp_length(self.as_object().mapping_unchecked(), vm)?;
return Ok(len != 0);
}

// 3. Try sq_length slot (sequence protocol)
if let Some(sq_length) = slots.as_sequence.length.load() {
let len = sq_length(self.as_object().sequence_unchecked(), vm)?;
return Ok(len != 0);
}

// 4. Default: objects without __bool__ or __len__ are truthy
Ok(true)
}
}

Expand Down
1 change: 0 additions & 1 deletion crates/vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ impl PyByteArray {
self.inner().capacity()
}

#[pymethod]
fn __len__(&self) -> usize {
self.borrow_buf().len()
}
Expand Down
1 change: 0 additions & 1 deletion crates/vm/src/builtins/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ impl PyRef<PyBytes> {
)]
impl PyBytes {
#[inline]
#[pymethod]
pub const fn __len__(&self) -> usize {
self.inner.len()
}
Expand Down
2 changes: 0 additions & 2 deletions crates/vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ impl PyDict {
}
}

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

#[pymethod]
fn __len__(&self) -> usize {
self.dict().__len__()
}
Expand Down
1 change: 0 additions & 1 deletion crates/vm/src/builtins/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ impl PyList {
}

#[allow(clippy::len_without_is_empty)]
#[pymethod]
pub fn __len__(&self) -> usize {
self.borrow_vec().len()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/builtins/mappingproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ impl PyMappingProxy {
PyGenericAlias::from_args(cls, args, vm)
}

#[pymethod]
fn __len__(&self, vm: &VirtualMachine) -> PyResult<usize> {
let obj = self.to_object(vm)?;
obj.length(vm)
Expand Down Expand Up @@ -235,6 +234,7 @@ impl AsMapping for PyMappingProxy {
impl AsSequence for PyMappingProxy {
fn as_sequence() -> &'static PySequenceMethods {
static AS_SEQUENCE: LazyLock<PySequenceMethods> = LazyLock::new(|| PySequenceMethods {
length: atomic_func!(|seq, vm| PyMappingProxy::sequence_downcast(seq).__len__(vm)),
contains: atomic_func!(
|seq, target, vm| PyMappingProxy::sequence_downcast(seq)._contains(target, vm)
),
Expand Down
1 change: 0 additions & 1 deletion crates/vm/src/builtins/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,6 @@ impl PyMemoryView {
Err(vm.new_type_error("cannot delete memory"))
}

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