Skip to content

Fix sequence protocol concat #1

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

Open
wants to merge 11 commits into
base: binaryops-with-number-protocol
Choose a base branch
from
2 changes: 2 additions & 0 deletions Lib/test/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ def __contains__(self, key):
d = c.new_child(b=20, c=30)
self.assertEqual(d.maps, [{'b': 20, 'c': 30}, {'a': 1, 'b': 2}])

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_union_operators(self):
cm1 = ChainMap(dict(a=1, b=2), dict(c=3, d=4))
cm2 = ChainMap(dict(a=10, e=5), dict(b=20, d=4))
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/test_xml_dom_minicompat.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ def test_emptynodelist___add__(self):
node_list = EmptyNodeList() + NodeList()
self.assertEqual(node_list, NodeList())

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_emptynodelist___radd__(self):
node_list = [1,2] + EmptyNodeList()
self.assertEqual(node_list, [1,2])
Expand Down
29 changes: 21 additions & 8 deletions derive-impl/src/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,14 +548,27 @@ where
other
),
};
quote_spanned! { ident.span() =>
class.set_str_attr(
#py_name,
ctx.make_funcdef(#py_name, Self::#ident)
#doc
#build_func,
ctx,
);
if py_name.starts_with("__") && py_name.ends_with("__") {
let name_ident = Ident::new(&py_name, ident.span());
quote_spanned! { ident.span() =>
class.set_attr(
ctx.names.#name_ident,
ctx.make_funcdef(#py_name, Self::#ident)
#doc
#build_func
.into(),
);
}
} else {
quote_spanned! { ident.span() =>
class.set_str_attr(
#py_name,
ctx.make_funcdef(#py_name, Self::#ident)
#doc
#build_func,
ctx,
);
}
}
};

Expand Down
89 changes: 86 additions & 3 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let array = module
.get_attr("array", vm)
.expect("Expect array has array type.");
array.init_builtin_number_slots(&vm.ctx);

let collections_abc = vm
.import("collections.abc", None, 0)
Expand Down Expand Up @@ -51,22 +52,23 @@ mod array {
},
protocol::{
BufferDescriptor, BufferMethods, BufferResizeGuard, PyBuffer, PyIterReturn,
PyMappingMethods, PySequenceMethods,
PyMappingMethods, PyNumberMethods, PySequenceMethods,
},
sequence::{OptionalRangeArgs, SequenceExt, SequenceMutExt},
sliceable::{
SaturatedSlice, SequenceIndex, SequenceIndexOp, SliceableSequenceMutOp,
SliceableSequenceOp,
},
types::{
AsBuffer, AsMapping, AsSequence, Comparable, Constructor, IterNext,
AsBuffer, AsMapping, AsNumber, AsSequence, Comparable, Constructor, IterNext,
IterNextIterable, Iterable, PyComparisonOp,
},
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
},
};
use itertools::Itertools;
use num_traits::ToPrimitive;
use once_cell::sync::Lazy;
use std::{cmp::Ordering, fmt, os::raw};

macro_rules! def_array_enum {
Expand Down Expand Up @@ -710,7 +712,7 @@ mod array {

#[pyclass(
flags(BASETYPE),
with(Comparable, AsBuffer, AsMapping, Iterable, Constructor)
with(Comparable, AsBuffer, AsMapping, AsNumber, Iterable, Constructor)
)]
impl PyArray {
fn read(&self) -> PyRwLockReadGuard<'_, ArrayContentType> {
Expand Down Expand Up @@ -1323,6 +1325,87 @@ mod array {
}
}

impl AsNumber for PyArray {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
add: Some(|number, other, vm| {
if let Some(number) = number.obj.downcast_ref::<PyArray>() {
number.add(other.to_owned(), vm).to_pyresult(vm)
} else {
Ok(vm.ctx.not_implemented())
}
}),
multiply: Some(|number, other, vm| {
if let Some(number) = number.obj.downcast_ref::<PyArray>() {
number
.mul(
other.try_index(vm)?.as_bigint().to_isize().ok_or_else(|| {
vm.new_overflow_error("repeated array is too long".to_owned())
})?,
vm,
)
.to_pyresult(vm)
} else if let Some(other) = other.downcast_ref::<PyArray>() {
other
.mul(
number
.obj
.try_index(vm)?
.as_bigint()
.to_isize()
.ok_or_else(|| {
vm.new_overflow_error(
"repeated array is too long".to_owned(),
)
})?,
vm,
)
.to_pyresult(vm)
} else {
Ok(vm.ctx.not_implemented())
}
}),
inplace_add: Some(|number, other, vm| {
if let Some(number) = number.obj.downcast_ref::<PyArray>() {
PyArray::iadd(number.to_owned(), other.to_owned(), vm).to_pyresult(vm)
} else {
Ok(vm.ctx.not_implemented())
}
}),
inplace_multiply: Some(|number, other, vm| {
if let Some(number) = number.obj.downcast_ref::<PyArray>() {
PyArray::imul(
number.to_owned(),
other.try_index(vm)?.as_bigint().to_isize().ok_or_else(|| {
vm.new_overflow_error("repeated array is too long".to_owned())
})?,
vm,
)
.to_pyresult(vm)
} else if let Some(other) = other.downcast_ref::<PyArray>() {
PyArray::imul(
other.to_owned(),
number
.obj
.try_index(vm)?
.as_bigint()
.to_isize()
.ok_or_else(|| {
vm.new_overflow_error("repeated array is too long".to_owned())
})?,
vm,
)
.to_pyresult(vm)
} else {
Ok(vm.ctx.not_implemented())
}
}),
..PyNumberMethods::NOT_IMPLEMENTED
});
&AS_NUMBER
}
}

impl AsSequence for PyArray {
fn as_sequence() -> &'static PySequenceMethods {
static AS_SEQUENCE: PySequenceMethods = PySequenceMethods {
Expand Down
52 changes: 50 additions & 2 deletions vm/src/builtins/bool.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use super::{PyInt, PyStrRef, PyType, PyTypeRef};
use crate::{
class::PyClassImpl, convert::ToPyObject, function::OptionalArg, identifier, types::Constructor,
class::PyClassImpl,
convert::{ToPyObject, ToPyResult},
function::OptionalArg,
identifier,
protocol::PyNumberMethods,
types::{AsNumber, Constructor},
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyResult, TryFromBorrowedObject,
VirtualMachine,
};
use num_bigint::Sign;
use num_traits::Zero;
use once_cell::sync::Lazy;
use std::fmt::{Debug, Formatter};

impl ToPyObject for bool {
Expand Down Expand Up @@ -102,7 +108,7 @@ impl Constructor for PyBool {
}
}

#[pyclass(with(Constructor))]
#[pyclass(with(Constructor, AsNumber))]
impl PyBool {
#[pymethod(magic)]
fn repr(zelf: bool, vm: &VirtualMachine) -> PyStrRef {
Expand Down Expand Up @@ -166,6 +172,48 @@ impl PyBool {
}
}

macro_rules! int_method {
($method:ident) => {
PyInt::as_number().$method
};
}

impl AsNumber for PyBool {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
add: int_method!(add),
subtract: int_method!(subtract),
multiply: int_method!(multiply),
remainder: int_method!(remainder),
divmod: int_method!(divmod),
power: int_method!(power),
negative: int_method!(negative),
positive: int_method!(positive),
absolute: int_method!(absolute),
boolean: int_method!(boolean),
invert: int_method!(invert),
lshift: int_method!(lshift),
rshift: int_method!(rshift),
and: Some(|number, other, vm| {
PyBool::and(number.obj.to_owned(), other.to_owned(), vm).to_pyresult(vm)
}),
xor: Some(|number, other, vm| {
PyBool::xor(number.obj.to_owned(), other.to_owned(), vm).to_pyresult(vm)
}),
or: Some(|number, other, vm| {
PyBool::or(number.obj.to_owned(), other.to_owned(), vm).to_pyresult(vm)
}),
int: int_method!(int),
float: int_method!(float),
floor_divide: int_method!(floor_divide),
true_divide: int_method!(true_divide),
index: int_method!(index),
..PyNumberMethods::NOT_IMPLEMENTED
});
&AS_NUMBER
}
}

pub(crate) fn init(context: &Context) {
PyBool::extend_class(context, context.types.bool_type);
}
Expand Down
10 changes: 6 additions & 4 deletions vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,10 +860,12 @@ impl AsSequence for PyByteArray {
impl AsNumber for PyByteArray {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
remainder: atomic_func!(|number, other, vm| {
PyByteArray::number_downcast(number)
.mod_(other.to_owned(), vm)
.to_pyresult(vm)
remainder: Some(|number, other, vm| {
if let Some(number) = number.obj.downcast_ref::<PyByteArray>() {
number.mod_(other.to_owned(), vm).to_pyresult(vm)
} else {
Ok(vm.ctx.not_implemented())
}
}),
..PyNumberMethods::NOT_IMPLEMENTED
});
Expand Down
10 changes: 6 additions & 4 deletions vm/src/builtins/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,10 +630,12 @@ impl AsSequence for PyBytes {
impl AsNumber for PyBytes {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
remainder: atomic_func!(|number, other, vm| {
PyBytes::number_downcast(number)
.mod_(other.to_owned(), vm)
.to_pyresult(vm)
remainder: Some(|number, other, vm| {
if let Some(number) = number.obj.downcast_ref::<PyBytes>() {
number.mod_(other.to_owned(), vm).to_pyresult(vm)
} else {
Ok(vm.ctx.not_implemented())
}
}),
..PyNumberMethods::NOT_IMPLEMENTED
});
Expand Down
27 changes: 11 additions & 16 deletions vm/src/builtins/complex.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::{float, PyStr, PyType, PyTypeRef};
use crate::{
atomic_func,
class::PyClassImpl,
convert::{ToPyObject, ToPyResult},
function::{
Expand Down Expand Up @@ -455,33 +454,29 @@ impl Hashable for PyComplex {
impl AsNumber for PyComplex {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
add: atomic_func!(|number, other, vm| {
add: Some(|number, other, vm| {
PyComplex::number_op(number, other, |a, b, _vm| a + b, vm)
}),
subtract: atomic_func!(|number, other, vm| {
subtract: Some(|number, other, vm| {
PyComplex::number_op(number, other, |a, b, _vm| a - b, vm)
}),
multiply: atomic_func!(|number, other, vm| {
multiply: Some(|number, other, vm| {
PyComplex::number_op(number, other, |a, b, _vm| a * b, vm)
}),
power: atomic_func!(|number, other, vm| PyComplex::number_op(
number, other, inner_pow, vm
)),
negative: atomic_func!(|number, vm| {
power: Some(|number, other, vm| PyComplex::number_op(number, other, inner_pow, vm)),
negative: Some(|number, vm| {
let value = PyComplex::number_downcast(number).value;
(-value).to_pyresult(vm)
}),
positive: atomic_func!(
|number, vm| PyComplex::number_downcast_exact(number, vm).to_pyresult(vm)
),
absolute: atomic_func!(|number, vm| {
positive: Some(|number, vm| {
PyComplex::number_downcast_exact(number, vm).to_pyresult(vm)
}),
absolute: Some(|number, vm| {
let value = PyComplex::number_downcast(number).value;
value.norm().to_pyresult(vm)
}),
boolean: atomic_func!(|number, _vm| Ok(PyComplex::number_downcast(number)
.value
.is_zero())),
true_divide: atomic_func!(|number, other, vm| {
boolean: Some(|number, _vm| Ok(PyComplex::number_downcast(number).value.is_zero())),
true_divide: Some(|number, other, vm| {
PyComplex::number_op(number, other, inner_div, vm)
}),
..PyNumberMethods::NOT_IMPLEMENTED
Expand Down
Loading