mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
clean up PyNumber interfaces
This commit is contained in:
@@ -194,41 +194,37 @@ impl PyNumberMethods {
|
||||
inplace_matrix_multiply: None,
|
||||
};
|
||||
|
||||
pub fn get_binary_op(
|
||||
&self,
|
||||
op_slot: PyNumberBinaryOp,
|
||||
) -> PyResult<&Option<PyNumberBinaryFunc>> {
|
||||
pub fn binary_op(&self, op_slot: PyNumberBinaryOp) -> Option<PyNumberBinaryFunc> {
|
||||
use PyNumberBinaryOp::*;
|
||||
let binary_op = match op_slot {
|
||||
Add => &self.add,
|
||||
Subtract => &self.subtract,
|
||||
Multiply => &self.multiply,
|
||||
Remainder => &self.remainder,
|
||||
Divmod => &self.divmod,
|
||||
Power => &self.power,
|
||||
Lshift => &self.lshift,
|
||||
Rshift => &self.rshift,
|
||||
And => &self.and,
|
||||
Xor => &self.xor,
|
||||
Or => &self.or,
|
||||
InplaceAdd => &self.inplace_add,
|
||||
InplaceSubtract => &self.inplace_subtract,
|
||||
InplaceMultiply => &self.inplace_multiply,
|
||||
InplaceRemainder => &self.inplace_remainder,
|
||||
InplacePower => &self.inplace_power,
|
||||
InplaceLshift => &self.inplace_lshift,
|
||||
InplaceRshift => &self.inplace_rshift,
|
||||
InplaceAnd => &self.inplace_and,
|
||||
InplaceXor => &self.inplace_xor,
|
||||
InplaceOr => &self.inplace_or,
|
||||
FloorDivide => &self.floor_divide,
|
||||
TrueDivide => &self.true_divide,
|
||||
InplaceFloorDivide => &self.inplace_floor_divide,
|
||||
InplaceTrueDivide => &self.inplace_true_divide,
|
||||
MatrixMultiply => &self.matrix_multiply,
|
||||
InplaceMatrixMultiply => &self.inplace_matrix_multiply,
|
||||
};
|
||||
Ok(binary_op)
|
||||
match op_slot {
|
||||
Add => self.add,
|
||||
Subtract => self.subtract,
|
||||
Multiply => self.multiply,
|
||||
Remainder => self.remainder,
|
||||
Divmod => self.divmod,
|
||||
Power => self.power,
|
||||
Lshift => self.lshift,
|
||||
Rshift => self.rshift,
|
||||
And => self.and,
|
||||
Xor => self.xor,
|
||||
Or => self.or,
|
||||
InplaceAdd => self.inplace_add,
|
||||
InplaceSubtract => self.inplace_subtract,
|
||||
InplaceMultiply => self.inplace_multiply,
|
||||
InplaceRemainder => self.inplace_remainder,
|
||||
InplacePower => self.inplace_power,
|
||||
InplaceLshift => self.inplace_lshift,
|
||||
InplaceRshift => self.inplace_rshift,
|
||||
InplaceAnd => self.inplace_and,
|
||||
InplaceXor => self.inplace_xor,
|
||||
InplaceOr => self.inplace_or,
|
||||
FloorDivide => self.floor_divide,
|
||||
TrueDivide => self.true_divide,
|
||||
InplaceFloorDivide => self.inplace_floor_divide,
|
||||
InplaceTrueDivide => self.inplace_true_divide,
|
||||
MatrixMultiply => self.matrix_multiply,
|
||||
InplaceMatrixMultiply => self.inplace_matrix_multiply,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,7 +262,7 @@ pub enum PyNumberBinaryOp {
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct PyNumber<'a> {
|
||||
pub obj: &'a PyObject,
|
||||
methods: &'a PyNumberMethods,
|
||||
pub(crate) methods: &'a PyNumberMethods,
|
||||
}
|
||||
|
||||
impl<'a> From<&'a PyObject> for PyNumber<'a> {
|
||||
@@ -285,17 +281,6 @@ impl PyNumber<'_> {
|
||||
obj.class().mro_find_map(|x| x.slots.as_number.load())
|
||||
}
|
||||
|
||||
pub fn methods(&self) -> &PyNumberMethods {
|
||||
self.methods
|
||||
}
|
||||
|
||||
pub fn get_binary_op(
|
||||
&self,
|
||||
op_slot: PyNumberBinaryOp,
|
||||
) -> PyResult<&Option<PyNumberBinaryFunc>> {
|
||||
self.methods().get_binary_op(op_slot)
|
||||
}
|
||||
|
||||
// PyNumber_Check
|
||||
pub fn check(obj: &PyObject) -> bool {
|
||||
let methods = &obj.class().slots.number;
|
||||
|
||||
@@ -161,7 +161,7 @@ pub struct PyNumberSlots {
|
||||
}
|
||||
|
||||
impl PyNumberSlots {
|
||||
pub fn get_left_binary_op(
|
||||
pub fn left_binary_op(
|
||||
&self,
|
||||
op_slot: PyNumberBinaryOp,
|
||||
) -> PyResult<Option<PyNumberBinaryFunc>> {
|
||||
@@ -198,7 +198,7 @@ impl PyNumberSlots {
|
||||
Ok(binary_op)
|
||||
}
|
||||
|
||||
pub fn get_right_binary_op(
|
||||
pub fn right_binary_op(
|
||||
&self,
|
||||
op_slot: PyNumberBinaryOp,
|
||||
) -> PyResult<Option<PyNumberBinaryFunc>> {
|
||||
@@ -1281,7 +1281,7 @@ macro_rules! extend_number_slot {
|
||||
if $methods.$method.is_some() {
|
||||
$slots.number.$method.store($methods.$method);
|
||||
$slots.number.$right_method.store(Some(|num, other, vm| {
|
||||
num.get_binary_op(PyNumberBinaryOp::$op_slot)?.unwrap()(
|
||||
num.methods.binary_op(PyNumberBinaryOp::$op_slot).unwrap()(
|
||||
other.to_number(),
|
||||
num.obj,
|
||||
vm,
|
||||
|
||||
@@ -132,11 +132,11 @@ impl VirtualMachine {
|
||||
///
|
||||
/// [*] only when Py_TYPE(a) != Py_TYPE(b) && Py_TYPE(b) is a subclass of Py_TYPE(a)
|
||||
pub fn binary_op1(&self, a: &PyObject, b: &PyObject, op_slot: PyNumberBinaryOp) -> PyResult {
|
||||
let slot_a = a.class().slots.number.get_left_binary_op(op_slot)?;
|
||||
let slot_a = a.class().slots.number.left_binary_op(op_slot)?;
|
||||
let mut slot_b = if b.class().is(a.class()) {
|
||||
None
|
||||
} else {
|
||||
match b.class().slots.number.get_right_binary_op(op_slot)? {
|
||||
match b.class().slots.number.right_binary_op(op_slot)? {
|
||||
Some(slot_b)
|
||||
if slot_b as usize == slot_a.map(|s| s as usize).unwrap_or_default() =>
|
||||
{
|
||||
@@ -206,7 +206,7 @@ impl VirtualMachine {
|
||||
iop_slot: PyNumberBinaryOp,
|
||||
op_slot: PyNumberBinaryOp,
|
||||
) -> PyResult {
|
||||
if let Some(slot) = a.class().slots.number.get_left_binary_op(iop_slot)? {
|
||||
if let Some(slot) = a.class().slots.number.left_binary_op(iop_slot)? {
|
||||
let x = slot(a.to_number(), b, self)?;
|
||||
if !x.is(&self.ctx.not_implemented) {
|
||||
return Ok(x);
|
||||
|
||||
Reference in New Issue
Block a user