Skip to content

Implement Number protocol for PyComplex #3838

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
merged 3 commits into from
Jul 10, 2022
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 Lib/test/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,6 @@ def test_compress(self):
next(testIntermediate)
self.assertEqual(list(op(testIntermediate)), list(result2))

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_count(self):
self.assertEqual(lzip('abc',count()), [('a', 0), ('b', 1), ('c', 2)])
self.assertEqual(lzip('abc',count(3)), [('a', 3), ('b', 4), ('c', 5)])
Expand Down
73 changes: 70 additions & 3 deletions vm/src/builtins/complex.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use super::{float, PyStr, PyType, PyTypeRef};
use crate::{
class::PyClassImpl,
convert::ToPyObject,
convert::{ToPyObject, ToPyResult},
function::{
OptionalArg, OptionalOption,
PyArithmeticValue::{self, *},
PyComparisonValue,
},
identifier,
types::{Comparable, Constructor, Hashable, PyComparisonOp},
protocol::{PyNumber, PyNumberMethods},
types::{AsNumber, Comparable, Constructor, Hashable, PyComparisonOp},
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
};
use num_complex::Complex64;
Expand Down Expand Up @@ -203,7 +204,7 @@ impl PyComplex {
}
}

#[pyimpl(flags(BASETYPE), with(Comparable, Hashable, Constructor))]
#[pyimpl(flags(BASETYPE), with(Comparable, Hashable, Constructor, AsNumber))]
impl PyComplex {
#[pymethod(magic)]
fn complex(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyRef<PyComplex> {
Expand Down Expand Up @@ -419,6 +420,72 @@ impl Hashable for PyComplex {
}
}

impl AsNumber for PyComplex {
const AS_NUMBER: PyNumberMethods = PyNumberMethods {
add: Some(|number, other, vm| Self::number_complex_op(number, other, |a, b| a + b, vm)),
subtract: Some(|number, other, vm| {
Self::number_complex_op(number, other, |a, b| a - b, vm)
}),
multiply: Some(|number, other, vm| {
Self::number_complex_op(number, other, |a, b| a * b, vm)
}),
power: Some(|number, other, vm| Self::number_general_op(number, other, inner_pow, vm)),
negative: Some(|number, vm| {
let value = Self::number_downcast(number).value;
(-value).to_pyresult(vm)
}),
positive: Some(|number, vm| Self::number_complex(number, vm).to_pyresult(vm)),
absolute: Some(|number, vm| {
let value = Self::number_downcast(number).value;
value.norm().to_pyresult(vm)
}),
boolean: Some(|number, _vm| Ok(Self::number_downcast(number).value.is_zero())),
true_divide: Some(|number, other, vm| {
Self::number_general_op(number, other, inner_div, vm)
}),
..PyNumberMethods::NOT_IMPLEMENTED
};
}

impl PyComplex {
fn number_general_op<F, R>(
number: &PyNumber,
other: &PyObject,
op: F,
vm: &VirtualMachine,
) -> PyResult
where
F: FnOnce(Complex64, Complex64, &VirtualMachine) -> R,
R: ToPyResult,
{
if let (Some(a), Some(b)) = (number.obj.payload::<Self>(), other.payload::<Self>()) {
op(a.value, b.value, vm).to_pyresult(vm)
} else {
Ok(vm.ctx.not_implemented())
}
}

fn number_complex_op<F>(
number: &PyNumber,
other: &PyObject,
op: F,
vm: &VirtualMachine,
) -> PyResult
where
F: FnOnce(Complex64, Complex64) -> Complex64,
{
Self::number_general_op(number, other, |a, b, _vm| op(a, b), vm)
}

fn number_complex(number: &PyNumber, vm: &VirtualMachine) -> PyRef<PyComplex> {
if let Some(zelf) = number.obj.downcast_ref_if_exact::<Self>(vm) {
zelf.to_owned()
} else {
vm.ctx.new_complex(Self::number_downcast(number).value)
}
}
}

#[derive(FromArgs)]
pub struct ComplexArgs {
#[pyarg(any, optional)]
Expand Down