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
2 changes: 0 additions & 2 deletions crates/stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,6 @@ mod array {
Ok(zelf)
}

#[pymethod]
pub(crate) fn __len__(&self) -> usize {
self.read().len()
}
Expand Down Expand Up @@ -1177,7 +1176,6 @@ mod array {
))
}

#[pymethod]
fn __contains__(&self, value: PyObjectRef, vm: &VirtualMachine) -> bool {
let array = self.array.read();
for element in array
Expand Down
1 change: 0 additions & 1 deletion crates/stdlib/src/contextvars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ mod _contextvars {
Ok(item.to_owned())
}

#[pymethod]
fn __len__(&self) -> usize {
self.borrow_vars().len()
}
Expand Down
1 change: 0 additions & 1 deletion crates/stdlib/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,6 @@ mod mmap {
.into()
}

#[pymethod]
fn __len__(&self) -> usize {
self.size.load()
}
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 @@ -219,7 +219,6 @@ impl PyByteArray {
self.inner().add(&other.borrow_buf()).into()
}

#[pymethod]
fn __contains__(
&self,
needle: Either<PyBytesInner, PyIntRef>,
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 @@ -228,7 +228,6 @@ impl PyBytes {
self.inner.add(&other.borrow_buf())
}

#[pymethod]
fn __contains__(
&self,
needle: Either<PyBytesInner, PyIntRef>,
Expand Down
36 changes: 24 additions & 12 deletions crates/vm/src/builtins/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,15 @@ pub enum SlotFunc {
SeqConcat(SeqConcatFunc),
SeqRepeat(SeqRepeatFunc),
SeqItem(SeqItemFunc),
SeqAssItem(SeqAssItemFunc),
SeqSetItem(SeqAssItemFunc), // __setitem__ (same func type, value = Some)
SeqDelItem(SeqAssItemFunc), // __delitem__ (same func type, value = None)
SeqContains(SeqContainsFunc),

// Mapping sub-slots (mp_*)
MapLength(MapLenFunc),
MapSubscript(MapSubscriptFunc),
MapAssSubscript(MapAssSubscriptFunc),
MapSetSubscript(MapAssSubscriptFunc), // __setitem__ (same func type, value = Some)
MapDelSubscript(MapAssSubscriptFunc), // __delitem__ (same func type, value = None)

// Number sub-slots (nb_*) - grouped by signature
NumBoolean(PyNumberUnaryFunc<bool>), // __bool__
Expand Down Expand Up @@ -468,12 +470,14 @@ impl core::fmt::Debug for SlotFunc {
SlotFunc::SeqConcat(_) => write!(f, "SlotFunc::SeqConcat(...)"),
SlotFunc::SeqRepeat(_) => write!(f, "SlotFunc::SeqRepeat(...)"),
SlotFunc::SeqItem(_) => write!(f, "SlotFunc::SeqItem(...)"),
SlotFunc::SeqAssItem(_) => write!(f, "SlotFunc::SeqAssItem(...)"),
SlotFunc::SeqSetItem(_) => write!(f, "SlotFunc::SeqSetItem(...)"),
SlotFunc::SeqDelItem(_) => write!(f, "SlotFunc::SeqDelItem(...)"),
SlotFunc::SeqContains(_) => write!(f, "SlotFunc::SeqContains(...)"),
// Mapping sub-slots
SlotFunc::MapLength(_) => write!(f, "SlotFunc::MapLength(...)"),
SlotFunc::MapSubscript(_) => write!(f, "SlotFunc::MapSubscript(...)"),
SlotFunc::MapAssSubscript(_) => write!(f, "SlotFunc::MapAssSubscript(...)"),
SlotFunc::MapSetSubscript(_) => write!(f, "SlotFunc::MapSetSubscript(...)"),
SlotFunc::MapDelSubscript(_) => write!(f, "SlotFunc::MapDelSubscript(...)"),
// Number sub-slots
SlotFunc::NumBoolean(_) => write!(f, "SlotFunc::NumBoolean(...)"),
SlotFunc::NumUnary(_) => write!(f, "SlotFunc::NumUnary(...)"),
Expand Down Expand Up @@ -600,10 +604,14 @@ impl SlotFunc {
let (index,): (isize,) = args.bind(vm)?;
func(obj.sequence_unchecked(), index, vm)
}
SlotFunc::SeqAssItem(func) => {
let (index, value): (isize, crate::function::OptionalArg<PyObjectRef>) =
args.bind(vm)?;
func(obj.sequence_unchecked(), index, value.into_option(), vm)?;
SlotFunc::SeqSetItem(func) => {
let (index, value): (isize, PyObjectRef) = args.bind(vm)?;
func(obj.sequence_unchecked(), index, Some(value), vm)?;
Ok(vm.ctx.none())
}
SlotFunc::SeqDelItem(func) => {
let (index,): (isize,) = args.bind(vm)?;
func(obj.sequence_unchecked(), index, None, vm)?;
Ok(vm.ctx.none())
}
SlotFunc::SeqContains(func) => {
Expand All @@ -621,10 +629,14 @@ impl SlotFunc {
let (key,): (PyObjectRef,) = args.bind(vm)?;
func(obj.mapping_unchecked(), &key, vm)
}
SlotFunc::MapAssSubscript(func) => {
let (key, value): (PyObjectRef, crate::function::OptionalArg<PyObjectRef>) =
args.bind(vm)?;
func(obj.mapping_unchecked(), &key, value.into_option(), vm)?;
SlotFunc::MapSetSubscript(func) => {
let (key, value): (PyObjectRef, PyObjectRef) = args.bind(vm)?;
func(obj.mapping_unchecked(), &key, Some(value), vm)?;
Ok(vm.ctx.none())
}
SlotFunc::MapDelSubscript(func) => {
let (key,): (PyObjectRef,) = args.bind(vm)?;
func(obj.mapping_unchecked(), &key, None, vm)?;
Ok(vm.ctx.none())
}
// Number sub-slots
Expand Down
3 changes: 0 additions & 3 deletions crates/vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ impl PyDict {
core::mem::size_of::<Self>() + self.entries.sizeof()
}

#[pymethod]
fn __contains__(&self, key: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
self.entries.contains(vm, &*key)
}
Expand Down Expand Up @@ -1130,7 +1129,6 @@ impl ViewSetOps for PyDictKeys {}
)
)]
impl PyDictKeys {
#[pymethod]
fn __contains__(zelf: PyObjectRef, key: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
zelf.sequence_unchecked().contains(&key, vm)
}
Expand Down Expand Up @@ -1195,7 +1193,6 @@ impl ViewSetOps for PyDictItems {}
)
)]
impl PyDictItems {
#[pymethod]
fn __contains__(zelf: PyObjectRef, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
zelf.sequence_unchecked().contains(&needle, vm)
}
Expand Down
13 changes: 0 additions & 13 deletions crates/vm/src/builtins/getset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,6 @@ impl PyGetSet {
)))
}
}
#[pymethod]
fn __set__(
zelf: PyObjectRef,
obj: PyObjectRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<()> {
Self::descr_set(&zelf, obj, PySetterValue::Assign(value), vm)
}
#[pymethod]
fn __delete__(zelf: PyObjectRef, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
Self::descr_set(&zelf, obj, PySetterValue::Delete, vm)
}

#[pygetset]
fn __name__(&self) -> String {
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 @@ -251,7 +251,6 @@ impl PyList {
self.mut_count(vm, &needle)
}

#[pymethod]
pub(crate) fn __contains__(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
self.mut_contains(vm, &needle)
}
Expand Down
1 change: 0 additions & 1 deletion crates/vm/src/builtins/mappingproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ impl PyMappingProxy {
}
}

#[pymethod]
pub fn __contains__(&self, key: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
self._contains(&key, vm)
}
Expand Down
13 changes: 0 additions & 13 deletions crates/vm/src/builtins/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,6 @@ impl PyProperty {
}
}
}
#[pymethod]
fn __set__(
zelf: PyObjectRef,
obj: PyObjectRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<()> {
Self::descr_set(&zelf, obj, PySetterValue::Assign(value), vm)
}
#[pymethod]
fn __delete__(zelf: PyObjectRef, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
Self::descr_set(&zelf, obj, PySetterValue::Delete, vm)
}

// Access functions

Expand Down
2 changes: 0 additions & 2 deletions crates/vm/src/builtins/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ impl PyRange {
)
}

#[pymethod]
fn __len__(&self) -> BigInt {
self.compute_length()
}
Expand Down Expand Up @@ -342,7 +341,6 @@ impl Py<PyRange> {
}
}

#[pymethod]
fn __contains__(&self, needle: PyObjectRef, vm: &VirtualMachine) -> bool {
self.contains_inner(&needle, vm)
}
Expand Down
1 change: 0 additions & 1 deletion crates/vm/src/builtins/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,6 @@ impl PyStr {
}
}

#[pymethod]
fn __contains__(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
self._contains(&needle, vm)
}
Expand Down
1 change: 0 additions & 1 deletion crates/vm/src/builtins/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ impl PyTuple {
Ok(false)
}

#[pymethod]
fn __contains__(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
self._contains(&needle, vm)
}
Expand Down
1 change: 0 additions & 1 deletion crates/vm/src/builtins/weakproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ impl PyWeakProxy {
let obj = self.try_upgrade(vm)?;
reversed(obj, vm)
}
#[pymethod]
fn __contains__(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
self.try_upgrade(vm)?
.sequence_unchecked()
Expand Down
5 changes: 0 additions & 5 deletions crates/vm/src/exception_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,6 @@ pub(super) mod types {
)))
}

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

#[pyslot]
fn slot_repr(zelf: &PyObject, vm: &VirtualMachine) -> PyResult<PyStrRef> {
let zelf = zelf
Expand Down
1 change: 0 additions & 1 deletion crates/vm/src/stdlib/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ mod _collections {
.ok_or_else(|| vm.new_index_error("deque index out of range"))
}

#[pymethod]
fn __contains__(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
self._contains(&needle, vm)
}
Expand Down
30 changes: 27 additions & 3 deletions crates/vm/src/stdlib/ctypes/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::{
},
class::StaticType,
function::{ArgBytesLike, FuncArgs, PySetterValue},
protocol::{BufferDescriptor, PyBuffer, PyNumberMethods, PySequenceMethods},
types::{AsBuffer, AsNumber, AsSequence, Constructor, Initializer},
protocol::{BufferDescriptor, PyBuffer, PyMappingMethods, PyNumberMethods, PySequenceMethods},
types::{AsBuffer, AsMapping, AsNumber, AsSequence, Constructor, Initializer},
};
use alloc::borrow::Cow;
use num_traits::{Signed, ToPrimitive};
Expand Down Expand Up @@ -468,9 +468,33 @@ impl AsSequence for PyCArray {
}
}

impl AsMapping for PyCArray {
fn as_mapping() -> &'static PyMappingMethods {
use std::sync::LazyLock;
static AS_MAPPING: LazyLock<PyMappingMethods> = LazyLock::new(|| PyMappingMethods {
length: atomic_func!(|mapping, _vm| {
let zelf = PyCArray::mapping_downcast(mapping);
Ok(zelf.class().stg_info_opt().map_or(0, |i| i.length))
}),
subscript: atomic_func!(|mapping, needle, vm| {
let zelf = PyCArray::mapping_downcast(mapping);
PyCArray::__getitem__(zelf, needle.to_owned(), vm)
}),
ass_subscript: atomic_func!(|mapping, needle, value, vm| {
let zelf = PyCArray::mapping_downcast(mapping);
match value {
Some(value) => PyCArray::__setitem__(zelf, needle.to_owned(), value, vm),
None => PyCArray::__delitem__(zelf, needle.to_owned(), vm),
}
}),
});
&AS_MAPPING
}
}

#[pyclass(
flags(BASETYPE, IMMUTABLETYPE),
with(Constructor, Initializer, AsSequence, AsBuffer)
with(Constructor, Initializer, AsSequence, AsMapping, AsBuffer)
)]
impl PyCArray {
#[pyclassmethod]
Expand Down
15 changes: 0 additions & 15 deletions crates/vm/src/stdlib/ctypes/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1650,21 +1650,6 @@ impl PyCField {
}
}

#[pymethod]
fn __set__(
zelf: PyObjectRef,
obj: PyObjectRef,
value: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<()> {
Self::descr_set(&zelf, obj, PySetterValue::Assign(value), vm)
}

#[pymethod]
fn __delete__(zelf: PyObjectRef, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
Self::descr_set(&zelf, obj, PySetterValue::Delete, vm)
}

#[pygetset]
fn offset(&self) -> isize {
self.offset
Expand Down
28 changes: 25 additions & 3 deletions crates/vm/src/stdlib/ctypes/pointer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::base::CDATA_BUFFER_METHODS;
use super::{PyCArray, PyCData, PyCSimple, PyCStructure, StgInfo, StgInfoFlags};
use crate::protocol::{BufferDescriptor, PyBuffer, PyNumberMethods};
use crate::types::{AsBuffer, AsNumber, Constructor, Initializer};
use crate::atomic_func;
use crate::protocol::{BufferDescriptor, PyBuffer, PyMappingMethods, PyNumberMethods};
use crate::types::{AsBuffer, AsMapping, AsNumber, Constructor, Initializer};
use crate::{
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
builtins::{PyBytes, PyInt, PyList, PySlice, PyStr, PyType, PyTypeRef},
Expand Down Expand Up @@ -260,7 +261,7 @@ impl Initializer for PyCPointer {

#[pyclass(
flags(BASETYPE, IMMUTABLETYPE),
with(Constructor, Initializer, AsNumber, AsBuffer)
with(Constructor, Initializer, AsNumber, AsBuffer, AsMapping)
)]
impl PyCPointer {
/// Get the pointer value stored in buffer as usize
Expand Down Expand Up @@ -785,6 +786,27 @@ impl AsNumber for PyCPointer {
}
}

impl AsMapping for PyCPointer {
fn as_mapping() -> &'static PyMappingMethods {
use std::sync::LazyLock;
static AS_MAPPING: LazyLock<PyMappingMethods> = LazyLock::new(|| PyMappingMethods {
subscript: atomic_func!(|mapping, needle, vm| {
let zelf = PyCPointer::mapping_downcast(mapping);
PyCPointer::__getitem__(zelf, needle.to_owned(), vm)
}),
ass_subscript: atomic_func!(|mapping, needle, value, vm| {
let zelf = PyCPointer::mapping_downcast(mapping);
match value {
Some(value) => PyCPointer::__setitem__(zelf, needle.to_owned(), value, vm),
None => Err(vm.new_type_error("Pointer does not support item deletion")),
}
}),
..PyMappingMethods::NOT_IMPLEMENTED
});
&AS_MAPPING
}
}

impl AsBuffer for PyCPointer {
fn as_buffer(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<PyBuffer> {
let stg_info = zelf
Expand Down
Loading
Loading