FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

slot for numeric ops by youknowone · Pull Request #6619 · RustPython/RustPython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .rs  (17) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 0 additions & 5 deletions crates/stdlib/src/array.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,6 @@ mod array {
self.delitem_inner(&needle, vm)
}

#[pymethod]
fn __add__(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
if let Some(other) = other.downcast_ref::<Self>() {
self.read()
Expand All @@ -1074,7 +1073,6 @@ mod array {
}
}

#[pymethod]
fn __iadd__(
zelf: PyRef<Self>,
other: PyObjectRef,
Expand All @@ -1093,15 +1091,12 @@ mod array {
Ok(zelf)
}

#[pymethod(name = "__rmul__")]
#[pymethod]
fn __mul__(&self, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
self.read()
.mul(value, vm)
.map(|x| Self::from(x).into_ref(&vm.ctx))
}

#[pymethod]
fn __imul__(zelf: PyRef<Self>, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
zelf.try_resizable(vm)?.imul(value, vm)?;
Ok(zelf)
Expand Down
15 changes: 2 additions & 13 deletions crates/vm/src/builtins/bytearray.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ impl PyByteArray {
size_of::<Self>() + self.borrow_buf().len() * size_of::<u8>()
}

#[pymethod]
fn __add__(&self, other: ArgBytesLike) -> Self {
self.inner().add(&other.borrow_buf()).into()
}
Expand All @@ -229,7 +228,6 @@ impl PyByteArray {
self.inner().contains(needle, vm)
}

#[pymethod]
fn __iadd__(
zelf: PyRef<Self>,
other: ArgBytesLike,
Expand Down Expand Up @@ -524,29 +522,20 @@ impl PyByteArray {
self.inner().title().into()
}

#[pymethod(name = "__rmul__")]
#[pymethod]
fn __mul__(&self, value: ArgSize, vm: &VirtualMachine) -> PyResult<Self> {
self.repeat(value.into(), vm)
}

#[pymethod]
fn __imul__(zelf: PyRef<Self>, value: ArgSize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
Self::irepeat(&zelf, value.into(), vm)?;
Ok(zelf)
}

#[pymethod(name = "__mod__")]
fn mod_(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
fn __mod__(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
let formatted = self.inner().cformat(values, vm)?;
Ok(formatted.into())
}

#[pymethod]
fn __rmod__(&self, _values: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.not_implemented()
}

#[pymethod]
fn reverse(&self) {
self.borrow_buf_mut().reverse();
Expand Down Expand Up @@ -822,7 +811,7 @@ impl AsNumber for PyByteArray {
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
remainder: Some(|a, b, vm| {
if let Some(a) = a.downcast_ref::<PyByteArray>() {
a.mod_(b.to_owned(), vm).to_pyresult(vm)
a.__mod__(b.to_owned(), vm).to_pyresult(vm)
} else {
Ok(vm.ctx.not_implemented())
}
Expand Down
13 changes: 2 additions & 11 deletions crates/vm/src/builtins/bytes.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ impl PyBytes {
size_of::<Self>() + self.len() * size_of::<u8>()
}

#[pymethod]
fn __add__(&self, other: ArgBytesLike) -> Vec<u8> {
self.inner.add(&other.borrow_buf())
}
Expand Down Expand Up @@ -512,23 +511,15 @@ impl PyBytes {
self.inner.title().into()
}

#[pymethod(name = "__rmul__")]
#[pymethod]
fn __mul__(zelf: PyRef<Self>, value: ArgIndex, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
zelf.repeat(value.try_to_primitive(vm)?, vm)
}

#[pymethod(name = "__mod__")]
fn mod_(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
fn __mod__(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
let formatted = self.inner.cformat(values, vm)?;
Ok(formatted.into())
}

#[pymethod]
fn __rmod__(&self, _values: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.not_implemented()
}

#[pymethod]
fn __getnewargs__(&self, vm: &VirtualMachine) -> PyTupleRef {
let param: Vec<PyObjectRef> = self.elements().map(|x| x.to_pyobject(vm)).collect();
Expand Down Expand Up @@ -677,7 +668,7 @@ impl AsNumber for PyBytes {
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
remainder: Some(|a, b, vm| {
if let Some(a) = a.downcast_ref::<PyBytes>() {
a.mod_(b.to_owned(), vm).to_pyresult(vm)
a.__mod__(b.to_owned(), vm).to_pyresult(vm)
} else {
Ok(vm.ctx.not_implemented())
}
Expand Down
15 changes: 12 additions & 3 deletions crates/vm/src/builtins/descriptor.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
class::PyClassImpl,
common::hash::PyHash,
convert::{ToPyObject, ToPyResult},
function::{FuncArgs, PyMethodDef, PyMethodFlags, PySetterValue},
function::{ArgSize, FuncArgs, PyMethodDef, PyMethodFlags, PySetterValue},
protocol::{PyNumberBinaryFunc, PyNumberTernaryFunc, PyNumberUnaryFunc},
types::{
Callable, Comparable, DelFunc, DescrGetFunc, DescrSetFunc, GenericMethod, GetDescriptor,
Expand Down Expand Up @@ -593,8 +593,8 @@ impl SlotFunc {
func(obj.sequence_unchecked(), &other, vm)
}
SlotFunc::SeqRepeat(func) => {
let (n,): (isize,) = args.bind(vm)?;
func(obj.sequence_unchecked(), n, vm)
let (n,): (ArgSize,) = args.bind(vm)?;
func(obj.sequence_unchecked(), n.into(), vm)
}
SlotFunc::SeqItem(func) => {
let (index,): (isize,) = args.bind(vm)?;
Expand Down Expand Up @@ -774,6 +774,15 @@ impl Callable for PyMethodWrapper {
type Args = FuncArgs;

fn call(zelf: &Py<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
// bpo-37619: Check type compatibility before calling wrapped slot
if !zelf.obj.fast_isinstance(zelf.wrapper.typ) {
return Err(vm.new_type_error(format!(
"descriptor '{}' requires a '{}' object but received a '{}'",
zelf.wrapper.name.as_str(),
zelf.wrapper.typ.name(),
zelf.obj.class().name()
)));
}
zelf.wrapper.wrapped.call(zelf.obj.clone(), args, vm)
}
}
Expand Down
11 changes: 0 additions & 11 deletions crates/vm/src/builtins/dict.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ impl PyDict {
Ok(())
}

#[pymethod]
fn __or__(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
let other_dict: Result<PyDictRef, _> = other.downcast();
if let Ok(other) = other_dict {
Expand Down Expand Up @@ -403,13 +402,11 @@ impl PyRef<PyDict> {
PyDictReverseKeyIterator::new(self)
}

#[pymethod]
fn __ior__(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<Self> {
self.merge_object(other, vm)?;
Ok(self)
}

#[pymethod]
fn __ror__(self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
let other_dict: Result<Self, _> = other.downcast();
if let Ok(other) = other_dict {
Expand Down Expand Up @@ -1046,38 +1043,30 @@ trait ViewSetOps: DictView {
PySetInner::from_iter(iter, vm)
}

#[pymethod(name = "__rxor__")]
#[pymethod]
fn __xor__(zelf: PyRef<Self>, other: ArgIterable, vm: &VirtualMachine) -> PyResult<PySet> {
let zelf = Self::to_set(zelf, vm)?;
let inner = zelf.symmetric_difference(other, vm)?;
Ok(PySet { inner })
}

#[pymethod(name = "__rand__")]
#[pymethod]
fn __and__(zelf: PyRef<Self>, other: ArgIterable, vm: &VirtualMachine) -> PyResult<PySet> {
let zelf = Self::to_set(zelf, vm)?;
let inner = zelf.intersection(other, vm)?;
Ok(PySet { inner })
}

#[pymethod(name = "__ror__")]
#[pymethod]
fn __or__(zelf: PyRef<Self>, other: ArgIterable, vm: &VirtualMachine) -> PyResult<PySet> {
let zelf = Self::to_set(zelf, vm)?;
let inner = zelf.union(other, vm)?;
Ok(PySet { inner })
}

#[pymethod]
fn __sub__(zelf: PyRef<Self>, other: ArgIterable, vm: &VirtualMachine) -> PyResult<PySet> {
let zelf = Self::to_set(zelf, vm)?;
let inner = zelf.difference(other, vm)?;
Ok(PySet { inner })
}

#[pymethod]
fn __rsub__(zelf: PyRef<Self>, other: ArgIterable, vm: &VirtualMachine) -> PyResult<PySet> {
let left = PySetInner::from_iter(other.iter(vm)?, vm)?;
let right = ArgIterable::try_from_object(vm, Self::iter(zelf, vm)?)?;
Expand Down
2 changes: 0 additions & 2 deletions crates/vm/src/builtins/genericalias.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,10 @@ impl PyGenericAlias {
Err(vm.new_type_error("issubclass() argument 2 cannot be a parameterized generic"))
}

#[pymethod]
fn __ror__(zelf: PyObjectRef, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
type_::or_(other, zelf, vm)
}

#[pymethod]
fn __or__(zelf: PyObjectRef, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
type_::or_(zelf, other, vm)
}
Expand Down
5 changes: 0 additions & 5 deletions crates/vm/src/builtins/list.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ impl PyList {
Ok(Self::from(elements).into_ref(&vm.ctx))
}

#[pymethod]
fn __add__(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
self.concat(&other, vm)
}
Expand All @@ -160,7 +159,6 @@ impl PyList {
Ok(zelf.to_owned().into())
}

#[pymethod]
fn __iadd__(
zelf: PyRef<Self>,
other: PyObjectRef,
Expand Down Expand Up @@ -240,13 +238,10 @@ impl PyList {
self._setitem(&needle, value, vm)
}

#[pymethod]
#[pymethod(name = "__rmul__")]
fn __mul__(&self, n: ArgSize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
self.repeat(n.into(), vm)
}

#[pymethod]
fn __imul__(zelf: PyRef<Self>, n: ArgSize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
Self::irepeat(zelf, n.into(), vm)
}
Expand Down
3 changes: 0 additions & 3 deletions crates/vm/src/builtins/mappingproxy.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,13 @@ impl PyMappingProxy {
)
}

#[pymethod]
fn __ior__(&self, _args: PyObjectRef, vm: &VirtualMachine) -> PyResult {
Err(vm.new_type_error(format!(
r#""'|=' is not supported by {}; use '|' instead""#,
Self::class(&vm.ctx)
)))
}

#[pymethod(name = "__ror__")]
#[pymethod]
fn __or__(&self, args: PyObjectRef, vm: &VirtualMachine) -> PyResult {
vm._or(self.copy(vm)?.as_ref(), args.as_ref())
}
Expand Down
13 changes: 2 additions & 11 deletions crates/vm/src/builtins/str.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,6 @@ impl Py<PyStr> {
)
)]
impl PyStr {
#[pymethod]
fn __add__(zelf: PyRef<Self>, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
if let Some(other) = other.downcast_ref::<Self>() {
let bytes = zelf.as_wtf8().py_add(other.as_wtf8());
Expand Down Expand Up @@ -636,8 +635,6 @@ impl PyStr {
core::mem::size_of::<Self>() + self.byte_len() * core::mem::size_of::<u8>()
}

#[pymethod(name = "__rmul__")]
#[pymethod]
fn __mul__(zelf: PyRef<Self>, value: ArgSize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
Self::repeat(zelf, value.into(), vm)
}
Expand Down Expand Up @@ -947,16 +944,10 @@ impl PyStr {
&& self.char_all(|c| GeneralCategory::of(c) == GeneralCategory::DecimalNumber)
}

#[pymethod(name = "__mod__")]
fn modulo(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<Wtf8Buf> {
fn __mod__(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<Wtf8Buf> {
cformat_string(vm, self.as_wtf8(), values)
}

#[pymethod]
fn __rmod__(&self, _values: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.not_implemented()
}

#[pymethod]
fn format(&self, args: FuncArgs, vm: &VirtualMachine) -> PyResult<Wtf8Buf> {
let format_str =
Expand Down Expand Up @@ -1564,7 +1555,7 @@ impl AsNumber for PyStr {
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
remainder: Some(|a, b, vm| {
if let Some(a) = a.downcast_ref::<PyStr>() {
a.modulo(b.to_owned(), vm).to_pyresult(vm)
a.__mod__(b.to_owned(), vm).to_pyresult(vm)
} else {
Ok(vm.ctx.not_implemented())
}
Expand Down
3 changes: 0 additions & 3 deletions crates/vm/src/builtins/tuple.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ impl<T> PyTuple<PyRef<T>> {
with(AsMapping, AsNumber, AsSequence, Hashable, Comparable, Iterable, Constructor, Representable)
)]
impl PyTuple {
#[pymethod]
fn __add__(
zelf: PyRef<Self>,
other: PyObjectRef,
Expand Down Expand Up @@ -302,8 +301,6 @@ impl PyTuple {
self.elements.len()
}

#[pymethod(name = "__rmul__")]
#[pymethod]
fn __mul__(zelf: PyRef<Self>, value: ArgSize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
Self::repeat(zelf, value.into(), vm)
}
Expand Down
2 changes: 0 additions & 2 deletions crates/vm/src/builtins/type.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -951,12 +951,10 @@ impl PyType {
)
}

#[pymethod]
pub fn __ror__(zelf: PyObjectRef, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
or_(other, zelf, vm)
}

#[pymethod]
pub fn __or__(zelf: PyObjectRef, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
or_(zelf, other, vm)
}
Expand Down
2 changes: 0 additions & 2 deletions crates/vm/src/builtins/union.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ impl PyUnion {
}
}

#[pymethod(name = "__ror__")]
#[pymethod]
fn __or__(zelf: PyObjectRef, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
type_::or_(zelf, other, vm)
}
Expand Down
4 changes: 0 additions & 4 deletions crates/vm/src/stdlib/collections.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,6 @@ mod _collections {
Ok(deque)
}

#[pymethod]
#[pymethod(name = "__rmul__")]
fn __mul__(&self, n: isize, vm: &VirtualMachine) -> PyResult<Self> {
let deque = self._mul(n, vm)?;
Ok(Self {
Expand All @@ -343,7 +341,6 @@ mod _collections {
})
}

#[pymethod]
fn __imul__(zelf: PyRef<Self>, n: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
let mul_deque = zelf._mul(n, vm)?;
*zelf.borrow_deque_mut() = mul_deque;
Expand Down Expand Up @@ -379,7 +376,6 @@ mod _collections {
}
}

#[pymethod]
fn __iadd__(
zelf: PyRef<Self>,
other: PyObjectRef,
Expand Down
Loading

Back | FazBrowse Home | New Git URL