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
72 changes: 0 additions & 72 deletions crates/vm/src/builtins/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,66 +326,6 @@ impl PyBaseObject {
Ok(res)
}

/// Return self==value.
#[pymethod]
fn __eq__(
zelf: PyObjectRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
Self::cmp(&zelf, &value, PyComparisonOp::Eq, vm)
}

/// Return self!=value.
#[pymethod]
fn __ne__(
zelf: PyObjectRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
Self::cmp(&zelf, &value, PyComparisonOp::Ne, vm)
}

/// Return self<value.
#[pymethod]
fn __lt__(
zelf: PyObjectRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
Self::cmp(&zelf, &value, PyComparisonOp::Lt, vm)
}

/// Return self<=value.
#[pymethod]
fn __le__(
zelf: PyObjectRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
Self::cmp(&zelf, &value, PyComparisonOp::Le, vm)
}

/// Return self>=value.
#[pymethod]
fn __ge__(
zelf: PyObjectRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
Self::cmp(&zelf, &value, PyComparisonOp::Ge, vm)
}

/// Return self>value.
#[pymethod]
fn __gt__(
zelf: PyObjectRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
Self::cmp(&zelf, &value, PyComparisonOp::Gt, vm)
}

/// Implement setattr(self, name, value).
#[pymethod]
fn __setattr__(
Expand Down Expand Up @@ -450,12 +390,6 @@ impl PyBaseObject {
}
}

/// Return repr(self).
#[pymethod]
fn __repr__(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyStrRef> {
Self::slot_repr(&zelf, vm)
}

#[pyclassmethod]
fn __subclasshook__(_args: FuncArgs, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.not_implemented()
Expand Down Expand Up @@ -590,12 +524,6 @@ impl PyBaseObject {
Ok(zelf.get_id() as _)
}

/// Return hash(self).
#[pymethod]
fn __hash__(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyHash> {
Self::slot_hash(&zelf, vm)
}

#[pymethod]
fn __sizeof__(zelf: PyObjectRef) -> usize {
zelf.class().slots.basicsize
Expand Down
23 changes: 16 additions & 7 deletions crates/vm/src/builtins/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::{
class::PyClassImpl,
common::hash::PyHash,
function::{ArgIndex, FuncArgs, OptionalArg, PyComparisonValue},
protocol::{PyIterReturn, PyMappingMethods, PySequenceMethods},
protocol::{PyIterReturn, PyMappingMethods, PyNumberMethods, PySequenceMethods},
types::{
AsMapping, AsSequence, Comparable, Hashable, IterNext, Iterable, PyComparisonOp,
AsMapping, AsNumber, AsSequence, Comparable, Hashable, IterNext, Iterable, PyComparisonOp,
Representable, SelfIter,
},
};
Expand Down Expand Up @@ -176,6 +176,7 @@ pub fn init(context: &Context) {
with(
Py,
AsMapping,
AsNumber,
AsSequence,
Hashable,
Comparable,
Expand Down Expand Up @@ -269,11 +270,6 @@ impl PyRange {
self.compute_length()
}

#[pymethod]
fn __bool__(&self) -> bool {
!self.is_empty()
}

#[pymethod]
fn __reduce__(&self, vm: &VirtualMachine) -> (PyTypeRef, PyTupleRef) {
let range_parameters: Vec<PyObjectRef> = [&self.start, &self.stop, &self.step]
Expand Down Expand Up @@ -426,6 +422,19 @@ impl AsSequence for PyRange {
}
}

impl AsNumber for PyRange {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
boolean: Some(|number, _vm| {
let zelf = number.obj.downcast_ref::<PyRange>().unwrap();
Ok(!zelf.is_empty())
}),
..PyNumberMethods::NOT_IMPLEMENTED
};
&AS_NUMBER
}
}

impl Hashable for PyRange {
fn hash(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
let length = zelf.compute_length();
Expand Down
30 changes: 15 additions & 15 deletions crates/vm/src/builtins/singletons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@ impl Constructor for PyNone {
}

#[pyclass(with(Constructor, AsNumber, Representable))]
impl PyNone {
#[pymethod]
const fn __bool__(&self) -> bool {
false
}
}
impl PyNone {}

impl Representable for PyNone {
#[inline]
Expand Down Expand Up @@ -103,22 +98,27 @@ impl Constructor for PyNotImplemented {
}
}

#[pyclass(with(Constructor))]
#[pyclass(with(Constructor, AsNumber, Representable))]
impl PyNotImplemented {
// TODO: As per https://bugs.python.org/issue35712, using NotImplemented
// in boolean contexts will need to raise a DeprecationWarning in 3.9
// and, eventually, a TypeError.
#[pymethod]
const fn __bool__(&self) -> bool {
true
}

#[pymethod]
fn __reduce__(&self, vm: &VirtualMachine) -> PyStrRef {
vm.ctx.names.NotImplemented.to_owned()
}
}

impl AsNumber for PyNotImplemented {
fn as_number() -> &'static PyNumberMethods {
// TODO: As per https://bugs.python.org/issue35712, using NotImplemented
// in boolean contexts will need to raise a DeprecationWarning in 3.9
// and, eventually, a TypeError.
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
boolean: Some(|_number, _vm| Ok(true)),
..PyNumberMethods::NOT_IMPLEMENTED
};
&AS_NUMBER
}
}

impl Representable for PyNotImplemented {
#[inline]
fn repr(_zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyStrRef> {
Expand Down
24 changes: 16 additions & 8 deletions crates/vm/src/builtins/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use crate::{
convert::{ToPyObject, TransmuteFromObject},
function::{ArgSize, FuncArgs, OptionalArg, PyArithmeticValue, PyComparisonValue},
iter::PyExactSizeIterator,
protocol::{PyIterReturn, PyMappingMethods, PySequenceMethods},
protocol::{PyIterReturn, PyMappingMethods, PyNumberMethods, PySequenceMethods},
recursion::ReprGuard,
sequence::{OptionalRangeArgs, SequenceExt},
sliceable::{SequenceIndex, SliceableSequenceOp},
types::{
AsMapping, AsSequence, Comparable, Constructor, Hashable, IterNext, Iterable,
AsMapping, AsNumber, AsSequence, Comparable, Constructor, Hashable, IterNext, Iterable,
PyComparisonOp, Representable, SelfIter,
},
utils::collection_repr,
Expand Down Expand Up @@ -260,7 +260,7 @@ impl<T> PyTuple<PyRef<T>> {
#[pyclass(
itemsize = core::mem::size_of::<crate::PyObjectRef>(),
flags(BASETYPE, SEQUENCE, _MATCH_SELF),
with(AsMapping, AsSequence, Hashable, Comparable, Iterable, Constructor, Representable)
with(AsMapping, AsNumber, AsSequence, Hashable, Comparable, Iterable, Constructor, Representable)
)]
impl PyTuple {
#[pymethod]
Expand All @@ -286,11 +286,6 @@ impl PyTuple {
PyArithmeticValue::from_option(added.ok())
}

#[pymethod]
const fn __bool__(&self) -> bool {
!self.elements.is_empty()
}

#[pymethod]
fn count(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
let mut count: usize = 0;
Expand Down Expand Up @@ -423,6 +418,19 @@ impl AsSequence for PyTuple {
}
}

impl AsNumber for PyTuple {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
boolean: Some(|number, _vm| {
let zelf = number.obj.downcast_ref::<PyTuple>().unwrap();
Ok(!zelf.elements.is_empty())
}),
..PyNumberMethods::NOT_IMPLEMENTED
};
&AS_NUMBER
}
}

impl Hashable for PyTuple {
#[inline]
fn hash(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
Expand Down
25 changes: 17 additions & 8 deletions crates/vm/src/builtins/weakproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use crate::{
class::PyClassImpl,
common::hash::PyHash,
function::{OptionalArg, PyComparisonValue, PySetterValue},
protocol::{PyIter, PyIterReturn, PyMappingMethods, PySequenceMethods},
protocol::{PyIter, PyIterReturn, PyMappingMethods, PyNumberMethods, PySequenceMethods},
stdlib::builtins::reversed,
types::{
AsMapping, AsSequence, Comparable, Constructor, GetAttr, Hashable, IterNext, Iterable,
PyComparisonOp, Representable, SetAttr,
AsMapping, AsNumber, AsSequence, Comparable, Constructor, GetAttr, Hashable, IterNext,
Iterable, PyComparisonOp, Representable, SetAttr,
},
};
use std::sync::LazyLock;
Expand Down Expand Up @@ -68,6 +68,7 @@ crate::common::static_cell! {
SetAttr,
Constructor,
Comparable,
AsNumber,
AsSequence,
AsMapping,
Representable,
Expand All @@ -87,11 +88,6 @@ impl PyWeakProxy {
self.try_upgrade(vm)?.length(vm)
}

#[pymethod]
fn __bool__(&self, vm: &VirtualMachine) -> PyResult<bool> {
self.try_upgrade(vm)?.is_true(vm)
}

#[pymethod]
fn __bytes__(&self, vm: &VirtualMachine) -> PyResult {
self.try_upgrade(vm)?.bytes(vm)
Expand Down Expand Up @@ -171,6 +167,19 @@ impl SetAttr for PyWeakProxy {
}
}

impl AsNumber for PyWeakProxy {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: LazyLock<PyNumberMethods> = LazyLock::new(|| PyNumberMethods {
boolean: Some(|number, vm| {
let zelf = number.obj.downcast_ref::<PyWeakProxy>().unwrap();
zelf.try_upgrade(vm)?.is_true(vm)
}),
..PyNumberMethods::NOT_IMPLEMENTED
});
&AS_NUMBER
}
}

impl Comparable for PyWeakProxy {
fn cmp(
zelf: &Py<Self>,
Expand Down
7 changes: 7 additions & 0 deletions crates/vm/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ pub fn add_operators(class: &'static Py<PyType>, ctx: &Context) {
continue;
}

// __getattr__ should only have a wrapper if the type explicitly defines it.
// Unlike __getattribute__, __getattr__ is not present on object by default.
// Both map to TpGetattro, but only __getattribute__ gets a wrapper from the slot.
if def.name == "__getattr__" {
continue;
}

// Get the slot function wrapped in SlotFunc
let Some(slot_func) = def.accessor.get_slot_func_with_op(&class.slots, def.op) else {
continue;
Expand Down
25 changes: 17 additions & 8 deletions crates/vm/src/stdlib/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ mod _collections {
common::lock::{PyMutex, PyRwLock, PyRwLockReadGuard, PyRwLockWriteGuard},
function::{KwArgs, OptionalArg, PyComparisonValue},
iter::PyExactSizeIterator,
protocol::{PyIterReturn, PySequenceMethods},
protocol::{PyIterReturn, PyNumberMethods, PySequenceMethods},
recursion::ReprGuard,
sequence::{MutObjectSequenceOp, OptionalRangeArgs},
sliceable::SequenceIndexOp,
types::{
AsSequence, Comparable, Constructor, DefaultConstructor, Initializer, IterNext,
Iterable, PyComparisonOp, Representable, SelfIter,
AsNumber, AsSequence, Comparable, Constructor, DefaultConstructor, Initializer,
IterNext, Iterable, PyComparisonOp, Representable, SelfIter,
},
utils::collection_repr,
};
Expand Down Expand Up @@ -60,6 +60,7 @@ mod _collections {
with(
Constructor,
Initializer,
AsNumber,
AsSequence,
Comparable,
Iterable,
Expand Down Expand Up @@ -354,11 +355,6 @@ mod _collections {
self.borrow_deque().len()
}

#[pymethod]
fn __bool__(&self) -> bool {
!self.borrow_deque().is_empty()
}

#[pymethod]
fn __add__(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
self.concat(&other, vm)
Expand Down Expand Up @@ -496,6 +492,19 @@ mod _collections {
}
}

impl AsNumber for PyDeque {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
boolean: Some(|number, _vm| {
let zelf = number.obj.downcast_ref::<PyDeque>().unwrap();
Ok(!zelf.borrow_deque().is_empty())
}),
..PyNumberMethods::NOT_IMPLEMENTED
};
&AS_NUMBER
}
}

impl AsSequence for PyDeque {
fn as_sequence() -> &'static PySequenceMethods {
static AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
Expand Down
Loading
Loading