Skip to content

Implement Number Protocol for PyType, PySet and PyFrozenSet #4619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
113 changes: 106 additions & 7 deletions vm/src/builtins/set.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use once_cell::sync::Lazy;

/*
* Builtin set type with a sequence of unique items.
*/
use super::{
builtins_iter, IterStatus, PositionIterInternal, PyDict, PyDictRef, PyGenericAlias, PyTupleRef,
PyType, PyTypeRef,
};
use crate::atomic_func;
use crate::common::{ascii, hash::PyHash, lock::PyMutex, rc::PyRc};
use crate::{
atomic_func,
class::PyClassImpl,
common::{ascii, hash::PyHash, lock::PyMutex, rc::PyRc},
convert::ToPyResult,
dictdatatype::{self, DictSize},
function::{ArgIterable, FuncArgs, OptionalArg, PosArgs, PyArithmeticValue, PyComparisonValue},
protocol::{PyIterReturn, PySequenceMethods},
protocol::{PyIterReturn, PyNumberMethods, PySequenceMethods},
recursion::ReprGuard,
types::AsNumber,
types::{
AsSequence, Comparable, Constructor, Hashable, Initializer, IterNext, IterNextIterable,
Iterable, PyComparisonOp, Unconstructible, Unhashable,
Expand All @@ -23,6 +23,7 @@ use crate::{
vm::VirtualMachine,
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
};
use once_cell::sync::Lazy;
use std::{fmt, ops::Deref};

pub type SetContentType = dictdatatype::Dict<()>;
Expand Down Expand Up @@ -488,7 +489,15 @@ fn reduce_set(
}

#[pyclass(
with(Constructor, Initializer, AsSequence, Hashable, Comparable, Iterable),
with(
Constructor,
Initializer,
AsSequence,
Hashable,
Comparable,
Iterable,
AsNumber
),
flags(BASETYPE)
)]
impl PySet {
Expand Down Expand Up @@ -792,6 +801,67 @@ impl Iterable for PySet {
}
}

impl AsNumber for PySet {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
subtract: atomic_func!(|number, other, vm| {
PySet::number_downcast(number)
.sub(other.to_owned(), vm)
.to_pyresult(vm)
}),
and: atomic_func!(|number, other, vm| {
PySet::number_downcast(number)
.and(other.to_owned(), vm)
.to_pyresult(vm)
}),
xor: atomic_func!(|number, other, vm| {
PySet::number_downcast(number)
.xor(other.to_owned(), vm)
.to_pyresult(vm)
}),
or: atomic_func!(|number, other, vm| {
PySet::number_downcast(number)
.or(other.to_owned(), vm)
.to_pyresult(vm)
}),
inplace_subtract: atomic_func!(|number, other, vm| {
PySet::isub(
PySet::number_downcast(number).to_owned(),
AnySet::try_from_object(vm, other.to_owned())?,
vm,
)
.to_pyresult(vm)
}),
inplace_and: atomic_func!(|number, other, vm| {
PySet::iand(
PySet::number_downcast(number).to_owned(),
AnySet::try_from_object(vm, other.to_owned())?,
vm,
)
.to_pyresult(vm)
}),
inplace_xor: atomic_func!(|number, other, vm| {
PySet::ixor(
PySet::number_downcast(number).to_owned(),
AnySet::try_from_object(vm, other.to_owned())?,
vm,
)
.to_pyresult(vm)
}),
inplace_or: atomic_func!(|number, other, vm| {
PySet::ior(
PySet::number_downcast(number).to_owned(),
AnySet::try_from_object(vm, other.to_owned())?,
vm,
)
.to_pyresult(vm)
}),
..PyNumberMethods::NOT_IMPLEMENTED
});
&AS_NUMBER
}
}

impl Constructor for PyFrozenSet {
type Args = OptionalArg<PyObjectRef>;

Expand Down Expand Up @@ -822,7 +892,7 @@ impl Constructor for PyFrozenSet {

#[pyclass(
flags(BASETYPE),
with(Constructor, AsSequence, Hashable, Comparable, Iterable)
with(Constructor, AsSequence, Hashable, Comparable, Iterable, AsNumber)
)]
impl PyFrozenSet {
#[pymethod(magic)]
Expand Down Expand Up @@ -1019,6 +1089,35 @@ impl Iterable for PyFrozenSet {
}
}

impl AsNumber for PyFrozenSet {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
subtract: atomic_func!(|number, other, vm| {
PyFrozenSet::number_downcast(number)
.sub(other.to_owned(), vm)
.to_pyresult(vm)
}),
and: atomic_func!(|number, other, vm| {
PyFrozenSet::number_downcast(number)
.and(other.to_owned(), vm)
.to_pyresult(vm)
}),
xor: atomic_func!(|number, other, vm| {
PyFrozenSet::number_downcast(number)
.xor(other.to_owned(), vm)
.to_pyresult(vm)
}),
or: atomic_func!(|number, other, vm| {
PyFrozenSet::number_downcast(number)
.or(other.to_owned(), vm)
.to_pyresult(vm)
}),
..PyNumberMethods::NOT_IMPLEMENTED
});
&AS_NUMBER
}
}

struct AnySet {
object: PyObjectRef,
}
Expand Down
29 changes: 22 additions & 7 deletions vm/src/builtins/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ use super::{
mappingproxy::PyMappingProxy, object, union_, PyClassMethod, PyDictRef, PyList, PyStaticMethod,
PyStr, PyStrInterned, PyStrRef, PyTuple, PyTupleRef, PyWeak,
};
use crate::common::{
ascii,
borrow::BorrowedValue,
lock::{PyRwLock, PyRwLockReadGuard},
};
use crate::{
atomic_func,
builtins::{
descriptor::{
DescrObject, MemberDef, MemberDescrObject, MemberGetter, MemberKind, MemberSetter,
Expand All @@ -17,15 +13,22 @@ use crate::{
PyBaseExceptionRef,
},
class::{PyClassImpl, StaticType},
convert::ToPyObject,
common::{
ascii,
borrow::BorrowedValue,
lock::{PyRwLock, PyRwLockReadGuard},
},
convert::{ToPyObject, ToPyResult},
function::{FuncArgs, KwArgs, OptionalArg, PySetterValue},
identifier,
protocol::{PyIterReturn, PyMappingMethods, PyNumberMethods, PySequenceMethods},
types::AsNumber,
types::{Callable, GetAttr, PyTypeFlags, PyTypeSlots, SetAttr},
AsObject, Context, Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
};
use indexmap::{map::Entry, IndexMap};
use itertools::Itertools;
use once_cell::sync::Lazy;
use std::{borrow::Borrow, collections::HashSet, fmt, ops::Deref, pin::Pin, ptr::NonNull};

#[pyclass(module = false, name = "type")]
Expand Down Expand Up @@ -371,7 +374,7 @@ impl Py<PyType> {
}
}

#[pyclass(with(GetAttr, SetAttr, Callable), flags(BASETYPE))]
#[pyclass(with(GetAttr, SetAttr, Callable, AsNumber), flags(BASETYPE))]
impl PyType {
// bound method for every type
pub(crate) fn __new__(zelf: PyRef<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
Expand Down Expand Up @@ -1058,6 +1061,18 @@ impl Callable for PyType {
}
}

impl AsNumber for PyType {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
or: atomic_func!(|num, other, vm| {
or_(num.obj.to_owned(), other.to_owned(), vm).to_pyresult(vm)
}),
..PyNumberMethods::NOT_IMPLEMENTED
});
&AS_NUMBER
}
}

fn find_base_dict_descr(cls: &Py<PyType>, vm: &VirtualMachine) -> Option<PyObjectRef> {
cls.iter_base_chain().skip(1).find_map(|cls| {
// TODO: should actually be some translation of:
Expand Down