add rich_compare on GenericAlias

Signed-off-by: snowapril <sinjihng@gmail.com>
This commit is contained in:
snowapril
2021-10-22 17:21:20 +09:00
parent 92b81e5dd8
commit d9f561caa2

View File

@@ -2,9 +2,9 @@ use crate::{
builtins::{PyList, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef},
common::hash,
function::{FuncArgs, IntoPyObject},
types::{Callable, Constructor, GetAttr, Hashable},
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject,
TypeProtocol, VirtualMachine,
types::{Callable, Comparable, Constructor, GetAttr, Hashable, PyComparisonOp},
IdProtocol, PyClassImpl, PyComparisonValue, PyContext, PyObject, PyObjectRef, PyRef, PyResult,
PyValue, TryFromObject, TypeProtocol, VirtualMachine,
};
use std::fmt;
@@ -52,7 +52,10 @@ impl Constructor for PyGenericAlias {
}
}
#[pyimpl(with(Callable, Constructor, GetAttr, Hashable), flags(BASETYPE))]
#[pyimpl(
with(Callable, Comparable, Constructor, GetAttr, Hashable),
flags(BASETYPE)
)]
impl PyGenericAlias {
pub fn new(origin: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> Self {
let args: PyTupleRef = if let Ok(tuple) = PyTupleRef::try_from_object(vm, args.clone()) {
@@ -205,6 +208,30 @@ impl Callable for PyGenericAlias {
}
}
impl Comparable for PyGenericAlias {
fn cmp(
zelf: &crate::PyObjectView<Self>,
other: &PyObject,
op: PyComparisonOp,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
op.eq_only(|| {
let other = class_or_notimplemented!(Self, other);
Ok(PyComparisonValue::Implemented(
if !zelf
.origin()
.rich_compare_bool(&other.origin(), PyComparisonOp::Eq, vm)?
{
false
} else {
zelf.args()
.rich_compare_bool(&other.args(), PyComparisonOp::Eq, vm)?
},
))
})
}
}
impl Hashable for PyGenericAlias {
#[inline]
fn hash(zelf: &crate::PyObjectView<Self>, vm: &VirtualMachine) -> PyResult<hash::PyHash> {