Implement Number Protocol for PyDict

This commit is contained in:
Zhiyan Xiao
2023-03-06 05:34:18 +09:00
parent 003f3b4fe1
commit 6f499013f6

View File

@@ -16,11 +16,11 @@ use crate::{
ArgIterable, FuncArgs, KwArgs, OptionalArg, PyArithmeticValue::*, PyComparisonValue,
},
iter::PyExactSizeIterator,
protocol::{PyIterIter, PyIterReturn, PyMappingMethods, PySequenceMethods},
protocol::{PyIterIter, PyIterReturn, PyMappingMethods, PyNumberMethods, PySequenceMethods},
recursion::ReprGuard,
types::{
AsMapping, AsSequence, Callable, Comparable, Constructor, Hashable, Initializer, IterNext,
IterNextIterable, Iterable, PyComparisonOp, Unconstructible, Unhashable,
AsMapping, AsNumber, AsSequence, Callable, Comparable, Constructor, Hashable, Initializer,
IterNext, IterNextIterable, Iterable, PyComparisonOp, Unconstructible, Unhashable,
},
vm::VirtualMachine,
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyRefExact, PyResult,
@@ -209,7 +209,8 @@ impl PyDict {
Hashable,
Comparable,
Iterable,
AsSequence
AsSequence,
AsNumber
),
flags(BASETYPE)
)]
@@ -477,6 +478,26 @@ impl AsSequence for PyDict {
}
}
impl AsNumber for PyDict {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
or: atomic_func!(|num, args, vm| {
PyDict::number_downcast(num).or(args.to_pyobject(vm), vm)
}),
inplace_or: atomic_func!(|num, args, vm| {
PyDict::ior(
PyDict::number_downcast(num).to_owned(),
args.to_pyobject(vm),
vm,
)
.map(|d| d.into())
}),
..PyNumberMethods::NOT_IMPLEMENTED
});
&AS_NUMBER
}
}
impl Comparable for PyDict {
fn cmp(
zelf: &Py<Self>,