mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
Merge pull request #2861 from youknowone/pymethod-magic
clean up #[pymethod(magic)]
This commit is contained in:
@@ -55,12 +55,12 @@ impl PyAsyncGen {
|
||||
zelf.inner.repr(zelf.get_id())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__aiter__")]
|
||||
#[pymethod(magic)]
|
||||
fn aiter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
|
||||
zelf
|
||||
}
|
||||
|
||||
#[pymethod(name = "__anext__")]
|
||||
#[pymethod(magic)]
|
||||
fn anext(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyAsyncGenASend {
|
||||
Self::asend(zelf, vm.ctx.none(), vm)
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ impl PyByteArray {
|
||||
self.inner.write()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
fn repr(&self) -> String {
|
||||
self.inner().repr("bytearray(", ")")
|
||||
}
|
||||
@@ -133,22 +133,22 @@ impl PyByteArray {
|
||||
self.inner().capacity()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__len__")]
|
||||
#[pymethod(magic)]
|
||||
fn len(&self) -> usize {
|
||||
self.borrow_buf().len()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__sizeof__")]
|
||||
#[pymethod(magic)]
|
||||
fn sizeof(&self) -> usize {
|
||||
size_of::<Self>() + self.borrow_buf().len() * size_of::<u8>()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__add__")]
|
||||
#[pymethod(magic)]
|
||||
fn add(&self, other: PyBytesLike, vm: &VirtualMachine) -> PyObjectRef {
|
||||
vm.ctx.new_bytearray(self.inner().add(&*other.borrow_buf()))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__contains__")]
|
||||
#[pymethod(magic)]
|
||||
fn contains(
|
||||
&self,
|
||||
needle: Either<PyBytesInner, PyIntRef>,
|
||||
@@ -598,8 +598,8 @@ impl PyByteArray {
|
||||
self.inner().title().into()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__mul__")]
|
||||
#[pymethod(name = "__rmul__")]
|
||||
#[pymethod(magic)]
|
||||
fn mul(&self, n: isize) -> Self {
|
||||
self.inner().repeat(n).into()
|
||||
}
|
||||
@@ -610,12 +610,12 @@ impl PyByteArray {
|
||||
}
|
||||
|
||||
#[pymethod(name = "__mod__")]
|
||||
fn modulo(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyByteArray> {
|
||||
fn mod_(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyByteArray> {
|
||||
let formatted = self.inner().cformat(values, vm)?;
|
||||
Ok(formatted.into())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rmod__")]
|
||||
#[pymethod(magic)]
|
||||
fn rmod(&self, _values: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
|
||||
vm.ctx.not_implemented()
|
||||
}
|
||||
|
||||
@@ -109,12 +109,12 @@ impl PyBytes {
|
||||
options.get_bytes(cls, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
pub(crate) fn repr(&self) -> String {
|
||||
self.inner.repr("", "")
|
||||
}
|
||||
|
||||
#[pymethod(name = "__len__")]
|
||||
#[pymethod(magic)]
|
||||
#[inline]
|
||||
pub fn len(&self) -> usize {
|
||||
self.inner.len()
|
||||
@@ -130,17 +130,17 @@ impl PyBytes {
|
||||
&self.inner.elements
|
||||
}
|
||||
|
||||
#[pymethod(name = "__sizeof__")]
|
||||
#[pymethod(magic)]
|
||||
fn sizeof(&self) -> usize {
|
||||
size_of::<Self>() + self.inner.elements.len() * size_of::<u8>()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__add__")]
|
||||
#[pymethod(magic)]
|
||||
fn add(&self, other: PyBytesLike, vm: &VirtualMachine) -> PyObjectRef {
|
||||
vm.ctx.new_bytes(self.inner.add(&*other.borrow_buf()))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__contains__")]
|
||||
#[pymethod(magic)]
|
||||
fn contains(
|
||||
&self,
|
||||
needle: Either<PyBytesInner, PyIntRef>,
|
||||
@@ -149,7 +149,7 @@ impl PyBytes {
|
||||
self.inner.contains(needle, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__getitem__")]
|
||||
#[pymethod(magic)]
|
||||
fn getitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.inner.getitem("byte", needle, vm) // byte != Self::NAME
|
||||
}
|
||||
@@ -428,8 +428,8 @@ impl PyBytes {
|
||||
self.inner.title().into()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__mul__")]
|
||||
#[pymethod(name = "__rmul__")]
|
||||
#[pymethod(magic)]
|
||||
fn mul(zelf: PyRef<Self>, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
|
||||
if value > 0 && zelf.inner.len() as isize > std::isize::MAX / value {
|
||||
return Err(vm.new_overflow_error("repeated bytes are too long".to_owned()));
|
||||
@@ -446,12 +446,12 @@ impl PyBytes {
|
||||
}
|
||||
|
||||
#[pymethod(name = "__mod__")]
|
||||
fn modulo(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyBytes> {
|
||||
fn mod_(&self, values: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyBytes> {
|
||||
let formatted = self.inner.cformat(values, vm)?;
|
||||
Ok(formatted.into())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rmod__")]
|
||||
#[pymethod(magic)]
|
||||
fn rmod(&self, _values: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
|
||||
vm.ctx.not_implemented()
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ impl PyComplex {
|
||||
self.value.im
|
||||
}
|
||||
|
||||
#[pymethod(name = "__abs__")]
|
||||
#[pymethod(magic)]
|
||||
fn abs(&self) -> f64 {
|
||||
let Complex64 { im, re } = self.value;
|
||||
re.hypot(im)
|
||||
@@ -121,8 +121,8 @@ impl PyComplex {
|
||||
)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__add__")]
|
||||
#[pymethod(name = "__radd__")]
|
||||
#[pymethod(magic)]
|
||||
fn add(
|
||||
&self,
|
||||
other: PyObjectRef,
|
||||
@@ -131,7 +131,7 @@ impl PyComplex {
|
||||
self.op(other, |a, b| Ok(a + b), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__sub__")]
|
||||
#[pymethod(magic)]
|
||||
fn sub(
|
||||
&self,
|
||||
other: PyObjectRef,
|
||||
@@ -140,7 +140,7 @@ impl PyComplex {
|
||||
self.op(other, |a, b| Ok(a - b), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rsub__")]
|
||||
#[pymethod(magic)]
|
||||
fn rsub(
|
||||
&self,
|
||||
other: PyObjectRef,
|
||||
@@ -154,18 +154,18 @@ impl PyComplex {
|
||||
self.value.conj()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__float__")]
|
||||
#[pymethod(magic)]
|
||||
fn float(&self, vm: &VirtualMachine) -> PyResult<Never> {
|
||||
Err(vm.new_type_error(String::from("Can't convert complex to float")))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__int__")]
|
||||
#[pymethod(magic)]
|
||||
fn int(&self, vm: &VirtualMachine) -> PyResult<Never> {
|
||||
Err(vm.new_type_error(String::from("Can't convert complex to int")))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__mul__")]
|
||||
#[pymethod(name = "__rmul__")]
|
||||
#[pymethod(magic)]
|
||||
fn mul(
|
||||
&self,
|
||||
other: PyObjectRef,
|
||||
@@ -174,7 +174,7 @@ impl PyComplex {
|
||||
self.op(other, |a, b| Ok(a * b), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__truediv__")]
|
||||
#[pymethod(magic)]
|
||||
fn truediv(
|
||||
&self,
|
||||
other: PyObjectRef,
|
||||
@@ -183,7 +183,7 @@ impl PyComplex {
|
||||
self.op(other, |a, b| inner_div(a, b, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rtruediv__")]
|
||||
#[pymethod(magic)]
|
||||
fn rtruediv(
|
||||
&self,
|
||||
other: PyObjectRef,
|
||||
@@ -198,29 +198,29 @@ impl PyComplex {
|
||||
Err(vm.new_type_error("can't mod complex numbers.".to_owned()))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__floordiv__")]
|
||||
#[pymethod(name = "__rfloordiv__")]
|
||||
#[pymethod(magic)]
|
||||
fn floordiv(&self, _other: PyObjectRef, vm: &VirtualMachine) -> PyResult<Never> {
|
||||
Err(vm.new_type_error("can't take floor of complex number.".to_owned()))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__divmod__")]
|
||||
#[pymethod(name = "__rdivmod__")]
|
||||
#[pymethod(magic)]
|
||||
fn divmod(&self, _other: PyObjectRef, vm: &VirtualMachine) -> PyResult<Never> {
|
||||
Err(vm.new_type_error("can't take floor or mod of complex number.".to_owned()))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__pos__")]
|
||||
#[pymethod(magic)]
|
||||
fn pos(&self) -> Complex64 {
|
||||
self.value
|
||||
}
|
||||
|
||||
#[pymethod(name = "__neg__")]
|
||||
#[pymethod(magic)]
|
||||
fn neg(&self) -> Complex64 {
|
||||
-self.value
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
fn repr(&self) -> String {
|
||||
let Complex64 { re, im } = self.value;
|
||||
if re == 0.0 {
|
||||
@@ -230,7 +230,7 @@ impl PyComplex {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__pow__")]
|
||||
#[pymethod(magic)]
|
||||
fn pow(
|
||||
&self,
|
||||
other: PyObjectRef,
|
||||
@@ -244,7 +244,7 @@ impl PyComplex {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rpow__")]
|
||||
#[pymethod(magic)]
|
||||
fn rpow(
|
||||
&self,
|
||||
other: PyObjectRef,
|
||||
@@ -253,7 +253,7 @@ impl PyComplex {
|
||||
self.op(other, |a, b| inner_pow(b, a, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__bool__")]
|
||||
#[pymethod(magic)]
|
||||
fn bool(&self) -> bool {
|
||||
!Complex64::is_zero(&self.value)
|
||||
}
|
||||
@@ -331,8 +331,8 @@ impl PyComplex {
|
||||
Self::from(value).into_ref_with_type(vm, cls)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__getnewargs__")]
|
||||
fn complex_getnewargs(&self, vm: &VirtualMachine) -> PyObjectRef {
|
||||
#[pymethod(magic)]
|
||||
fn getnewargs(&self, vm: &VirtualMachine) -> PyObjectRef {
|
||||
let Complex64 { re, im } = self.value;
|
||||
vm.ctx
|
||||
.new_tuple(vec![vm.ctx.new_float(re), vm.ctx.new_float(im)])
|
||||
|
||||
@@ -310,7 +310,7 @@ impl PyDict {
|
||||
PyDict::merge(&self.entries, dict_obj, kwargs, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__ior__")]
|
||||
#[pymethod(magic)]
|
||||
fn ior(zelf: PyRef<Self>, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
let dicted: Result<PyDictRef, _> = other.downcast();
|
||||
if let Ok(other) = dicted {
|
||||
@@ -320,7 +320,7 @@ impl PyDict {
|
||||
Ok(vm.ctx.not_implemented())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__ror__")]
|
||||
#[pymethod(magic)]
|
||||
fn ror(zelf: PyRef<Self>, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
let dicted: Result<PyDictRef, _> = other.downcast();
|
||||
if let Ok(other) = dicted {
|
||||
@@ -331,7 +331,7 @@ impl PyDict {
|
||||
Ok(vm.ctx.not_implemented())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__or__")]
|
||||
#[pymethod(magic)]
|
||||
fn or(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
let dicted: Result<PyDictRef, _> = other.downcast();
|
||||
if let Ok(other) = dicted {
|
||||
@@ -387,7 +387,7 @@ impl PyDict {
|
||||
self.entries.size()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__reversed__")]
|
||||
#[pymethod(magic)]
|
||||
fn reversed(zelf: PyRef<Self>) -> PyDictReverseKeyIterator {
|
||||
PyDictReverseKeyIterator::new(zelf)
|
||||
}
|
||||
@@ -604,13 +604,13 @@ macro_rules! dict_iterator {
|
||||
$name { dict }
|
||||
}
|
||||
|
||||
#[pymethod(name = "__len__")]
|
||||
#[pymethod(magic)]
|
||||
fn len(&self) -> usize {
|
||||
self.dict.clone().len()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[allow(clippy::redundant_closure_call)]
|
||||
#[pymethod(magic)]
|
||||
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<String> {
|
||||
let s = if let Some(_guard) = ReprGuard::enter(vm, zelf.as_object()) {
|
||||
let mut str_parts = vec![];
|
||||
@@ -624,7 +624,7 @@ macro_rules! dict_iterator {
|
||||
};
|
||||
Ok(s)
|
||||
}
|
||||
#[pymethod(name = "__reversed__")]
|
||||
#[pymethod(magic)]
|
||||
fn reversed(&self) -> $reverse_iter_name {
|
||||
$reverse_iter_name::new(self.dict.clone())
|
||||
}
|
||||
|
||||
@@ -205,7 +205,7 @@ impl PyFloat {
|
||||
PyFloat::from(float_val).into_ref_with_type(vm, cls)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__format__")]
|
||||
#[pymethod(magic)]
|
||||
fn format(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
|
||||
match FormatSpec::parse(spec.as_str())
|
||||
.and_then(|format_spec| format_spec.format_float(self.value))
|
||||
@@ -215,7 +215,7 @@ impl PyFloat {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__abs__")]
|
||||
#[pymethod(magic)]
|
||||
fn abs(&self) -> f64 {
|
||||
self.value.abs()
|
||||
}
|
||||
@@ -263,18 +263,18 @@ impl PyFloat {
|
||||
)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__add__")]
|
||||
#[pymethod(name = "__radd__")]
|
||||
#[pymethod(magic)]
|
||||
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmaticValue<f64>> {
|
||||
self.simple_op(other, |a, b| Ok(a + b), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__bool__")]
|
||||
#[pymethod(magic)]
|
||||
fn bool(&self) -> bool {
|
||||
self.value != 0.0
|
||||
}
|
||||
|
||||
#[pymethod(name = "__divmod__")]
|
||||
#[pymethod(magic)]
|
||||
fn divmod(
|
||||
&self,
|
||||
other: PyObjectRef,
|
||||
@@ -283,7 +283,7 @@ impl PyFloat {
|
||||
self.tuple_op(other, |a, b| inner_divmod(a, b, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rdivmod__")]
|
||||
#[pymethod(magic)]
|
||||
fn rdivmod(
|
||||
&self,
|
||||
other: PyObjectRef,
|
||||
@@ -292,7 +292,7 @@ impl PyFloat {
|
||||
self.tuple_op(other, |a, b| inner_divmod(b, a, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__floordiv__")]
|
||||
#[pymethod(magic)]
|
||||
fn floordiv(
|
||||
&self,
|
||||
other: PyObjectRef,
|
||||
@@ -301,7 +301,7 @@ impl PyFloat {
|
||||
self.simple_op(other, |a, b| inner_floordiv(a, b, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rfloordiv__")]
|
||||
#[pymethod(magic)]
|
||||
fn rfloordiv(
|
||||
&self,
|
||||
other: PyObjectRef,
|
||||
@@ -315,22 +315,22 @@ impl PyFloat {
|
||||
self.simple_op(other, |a, b| inner_mod(a, b, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rmod__")]
|
||||
#[pymethod(magic)]
|
||||
fn rmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmaticValue<f64>> {
|
||||
self.simple_op(other, |a, b| inner_mod(b, a, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__pos__")]
|
||||
#[pymethod(magic)]
|
||||
fn pos(&self) -> f64 {
|
||||
self.value
|
||||
}
|
||||
|
||||
#[pymethod(name = "__neg__")]
|
||||
#[pymethod(magic)]
|
||||
fn neg(&self) -> f64 {
|
||||
-self.value
|
||||
}
|
||||
|
||||
#[pymethod(name = "__pow__")]
|
||||
#[pymethod(magic)]
|
||||
fn pow(
|
||||
&self,
|
||||
other: PyObjectRef,
|
||||
@@ -344,32 +344,32 @@ impl PyFloat {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rpow__")]
|
||||
#[pymethod(magic)]
|
||||
fn rpow(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.complex_op(other, |a, b| float_pow(b, a, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__sub__")]
|
||||
#[pymethod(magic)]
|
||||
fn sub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmaticValue<f64>> {
|
||||
self.simple_op(other, |a, b| Ok(a - b), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rsub__")]
|
||||
#[pymethod(magic)]
|
||||
fn rsub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmaticValue<f64>> {
|
||||
self.simple_op(other, |a, b| Ok(b - a), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
fn repr(&self) -> String {
|
||||
float_ops::to_string(self.value)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__truediv__")]
|
||||
#[pymethod(magic)]
|
||||
fn truediv(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmaticValue<f64>> {
|
||||
self.simple_op(other, |a, b| inner_div(a, b, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rtruediv__")]
|
||||
#[pymethod(magic)]
|
||||
fn rtruediv(
|
||||
&self,
|
||||
other: PyObjectRef,
|
||||
@@ -378,13 +378,13 @@ impl PyFloat {
|
||||
self.simple_op(other, |a, b| inner_div(b, a, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__mul__")]
|
||||
#[pymethod(name = "__rmul__")]
|
||||
#[pymethod(magic)]
|
||||
fn mul(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmaticValue<f64>> {
|
||||
self.simple_op(other, |a, b| Ok(a * b), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__trunc__")]
|
||||
#[pymethod(magic)]
|
||||
fn trunc(&self, vm: &VirtualMachine) -> PyResult<BigInt> {
|
||||
try_bigint(self.value, vm)
|
||||
}
|
||||
@@ -399,7 +399,7 @@ impl PyFloat {
|
||||
try_bigint(self.value.ceil(), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__round__")]
|
||||
#[pymethod(magic)]
|
||||
fn round(&self, ndigits: OptionalOption<PyIntRef>, vm: &VirtualMachine) -> PyResult {
|
||||
let ndigits = ndigits.flatten();
|
||||
let value = if let Some(ndigits) = ndigits {
|
||||
@@ -429,12 +429,12 @@ impl PyFloat {
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__int__")]
|
||||
#[pymethod(magic)]
|
||||
fn int(&self, vm: &VirtualMachine) -> PyResult<BigInt> {
|
||||
self.trunc(vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__float__")]
|
||||
#[pymethod(magic)]
|
||||
fn float(zelf: PyRef<Self>) -> PyRef<Self> {
|
||||
zelf
|
||||
}
|
||||
|
||||
@@ -23,12 +23,12 @@ impl FrameRef {
|
||||
Err(vm.new_type_error("Cannot directly create frame object".to_owned()))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
fn repr(self) -> String {
|
||||
"<frame object at .. >".to_owned()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__delattr__")]
|
||||
#[pymethod(magic)]
|
||||
fn delattr(self, value: PyStrRef, vm: &VirtualMachine) {
|
||||
// CPython' Frame.f_trace is set to None when deleted.
|
||||
// The strange behavior is mimicked here make bdb.py happy about it.
|
||||
|
||||
@@ -322,107 +322,87 @@ impl PyInt {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__add__")]
|
||||
#[pymethod(name = "__radd__")]
|
||||
#[pymethod(magic)]
|
||||
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
|
||||
self.int_op(other, |a, b| a + b, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__radd__")]
|
||||
fn radd(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
|
||||
self.add(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__sub__")]
|
||||
#[pymethod(magic)]
|
||||
fn sub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
|
||||
self.int_op(other, |a, b| a - b, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rsub__")]
|
||||
#[pymethod(magic)]
|
||||
fn rsub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
|
||||
self.int_op(other, |a, b| b - a, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__mul__")]
|
||||
#[pymethod(name = "__rmul__")]
|
||||
#[pymethod(magic)]
|
||||
fn mul(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
|
||||
self.int_op(other, |a, b| a * b, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rmul__")]
|
||||
fn rmul(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
|
||||
self.mul(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__truediv__")]
|
||||
#[pymethod(magic)]
|
||||
fn truediv(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.general_op(other, |a, b| inner_truediv(a, b, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rtruediv__")]
|
||||
#[pymethod(magic)]
|
||||
fn rtruediv(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.general_op(other, |a, b| inner_truediv(b, a, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__floordiv__")]
|
||||
#[pymethod(magic)]
|
||||
fn floordiv(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.general_op(other, |a, b| inner_floordiv(a, b, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rfloordiv__")]
|
||||
#[pymethod(magic)]
|
||||
fn rfloordiv(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.general_op(other, |a, b| inner_floordiv(b, a, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__lshift__")]
|
||||
#[pymethod(magic)]
|
||||
fn lshift(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.general_op(other, |a, b| inner_shift(a, b, |a, b| a << b, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rlshift__")]
|
||||
#[pymethod(magic)]
|
||||
fn rlshift(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.general_op(other, |a, b| inner_shift(b, a, |a, b| a << b, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rshift__")]
|
||||
#[pymethod(magic)]
|
||||
fn rshift(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.general_op(other, |a, b| inner_shift(a, b, |a, b| a >> b, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rrshift__")]
|
||||
#[pymethod(magic)]
|
||||
fn rrshift(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.general_op(other, |a, b| inner_shift(b, a, |a, b| a >> b, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__xor__")]
|
||||
#[pymethod(name = "__rxor__")]
|
||||
#[pymethod(magic)]
|
||||
pub fn xor(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
|
||||
self.int_op(other, |a, b| a ^ b, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rxor__")]
|
||||
fn rxor(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
|
||||
self.xor(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__or__")]
|
||||
#[pymethod(name = "__ror__")]
|
||||
#[pymethod(magic)]
|
||||
pub fn or(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
|
||||
self.int_op(other, |a, b| a | b, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__ror__")]
|
||||
fn ror(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
|
||||
self.or(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__and__")]
|
||||
#[pymethod(name = "__rand__")]
|
||||
#[pymethod(magic)]
|
||||
pub fn and(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
|
||||
self.int_op(other, |a, b| a & b, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rand__")]
|
||||
fn rand(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
|
||||
self.and(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__pow__")]
|
||||
#[pymethod(magic)]
|
||||
fn pow(
|
||||
&self,
|
||||
other: PyObjectRef,
|
||||
@@ -482,7 +462,7 @@ impl PyInt {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rpow__")]
|
||||
#[pymethod(magic)]
|
||||
fn rpow(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.general_op(other, |a, b| inner_pow(b, a, vm), vm)
|
||||
}
|
||||
@@ -492,32 +472,32 @@ impl PyInt {
|
||||
self.general_op(other, |a, b| inner_mod(a, b, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rmod__")]
|
||||
#[pymethod(magic)]
|
||||
fn rmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.general_op(other, |a, b| inner_mod(b, a, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__divmod__")]
|
||||
#[pymethod(magic)]
|
||||
fn divmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.general_op(other, |a, b| inner_divmod(a, b, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rdivmod__")]
|
||||
#[pymethod(magic)]
|
||||
fn rdivmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.general_op(other, |a, b| inner_divmod(b, a, vm), vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__neg__")]
|
||||
#[pymethod(magic)]
|
||||
fn neg(&self) -> BigInt {
|
||||
-(&self.value)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__abs__")]
|
||||
#[pymethod(magic)]
|
||||
fn abs(&self) -> BigInt {
|
||||
self.value.abs()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__round__")]
|
||||
#[pymethod(magic)]
|
||||
fn round(
|
||||
zelf: PyRef<Self>,
|
||||
precision: OptionalArg<PyObjectRef>,
|
||||
@@ -545,52 +525,52 @@ impl PyInt {
|
||||
Ok(zelf)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__int__")]
|
||||
#[pymethod(magic)]
|
||||
fn int(zelf: PyRef<Self>) -> PyRef<Self> {
|
||||
zelf
|
||||
}
|
||||
|
||||
#[pymethod(name = "__pos__")]
|
||||
#[pymethod(magic)]
|
||||
fn pos(&self) -> BigInt {
|
||||
self.value.clone()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__float__")]
|
||||
#[pymethod(magic)]
|
||||
fn float(&self, vm: &VirtualMachine) -> PyResult<f64> {
|
||||
to_float(&self.value, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__trunc__")]
|
||||
#[pymethod(magic)]
|
||||
fn trunc(zelf: PyRef<Self>) -> PyRef<Self> {
|
||||
zelf
|
||||
}
|
||||
|
||||
#[pymethod(name = "__floor__")]
|
||||
#[pymethod(magic)]
|
||||
fn floor(zelf: PyRef<Self>) -> PyRef<Self> {
|
||||
zelf
|
||||
}
|
||||
|
||||
#[pymethod(name = "__ceil__")]
|
||||
#[pymethod(magic)]
|
||||
fn ceil(zelf: PyRef<Self>) -> PyRef<Self> {
|
||||
zelf
|
||||
}
|
||||
|
||||
#[pymethod(name = "__index__")]
|
||||
#[pymethod(magic)]
|
||||
fn index(zelf: PyRef<Self>) -> PyRef<Self> {
|
||||
zelf
|
||||
}
|
||||
|
||||
#[pymethod(name = "__invert__")]
|
||||
#[pymethod(magic)]
|
||||
fn invert(&self) -> BigInt {
|
||||
!(&self.value)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
pub(crate) fn repr(&self) -> String {
|
||||
self.value.to_string()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__format__")]
|
||||
#[pymethod(magic)]
|
||||
fn format(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
|
||||
match FormatSpec::parse(spec.as_str())
|
||||
.and_then(|format_spec| format_spec.format_int(&self.value))
|
||||
@@ -600,12 +580,12 @@ impl PyInt {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__bool__")]
|
||||
#[pymethod(magic)]
|
||||
fn bool(&self) -> bool {
|
||||
!self.value.is_zero()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__sizeof__")]
|
||||
#[pymethod(magic)]
|
||||
fn sizeof(&self) -> usize {
|
||||
size_of::<Self>() + (((self.value.bits() + 7) & !7) / 8) as usize
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ impl PyList {
|
||||
elements.insert(position, element);
|
||||
}
|
||||
|
||||
#[pymethod(name = "__add__")]
|
||||
#[pymethod(magic)]
|
||||
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
if let Some(other) = other.payload_if_subclass::<PyList>(vm) {
|
||||
let mut elements = self.borrow_vec().to_vec();
|
||||
@@ -120,7 +120,7 @@ impl PyList {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__iadd__")]
|
||||
#[pymethod(magic)]
|
||||
fn iadd(zelf: PyRef<Self>, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
|
||||
if let Ok(new_elements) = vm.extract_elements(&other) {
|
||||
let mut e = new_elements;
|
||||
@@ -131,7 +131,7 @@ impl PyList {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__bool__")]
|
||||
#[pymethod(magic)]
|
||||
fn bool(&self) -> bool {
|
||||
!self.borrow_vec().is_empty()
|
||||
}
|
||||
@@ -146,12 +146,12 @@ impl PyList {
|
||||
vm.ctx.new_list(self.borrow_vec().to_vec())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__len__")]
|
||||
#[pymethod(magic)]
|
||||
fn len(&self) -> usize {
|
||||
self.borrow_vec().len()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__sizeof__")]
|
||||
#[pymethod(magic)]
|
||||
fn sizeof(&self) -> usize {
|
||||
size_of::<Self>() + self.elements.read().capacity() * size_of::<PyObjectRef>()
|
||||
}
|
||||
@@ -161,7 +161,7 @@ impl PyList {
|
||||
self.borrow_vec_mut().reverse();
|
||||
}
|
||||
|
||||
#[pymethod(name = "__reversed__")]
|
||||
#[pymethod(magic)]
|
||||
fn reversed(zelf: PyRef<Self>) -> PyListReverseIterator {
|
||||
let final_position = zelf.borrow_vec().len();
|
||||
PyListReverseIterator {
|
||||
@@ -170,7 +170,7 @@ impl PyList {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__getitem__")]
|
||||
#[pymethod(magic)]
|
||||
fn getitem(zelf: PyRef<Self>, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
let result = match zelf.borrow_vec().get_item(vm, needle, Self::NAME)? {
|
||||
Either::A(obj) => obj,
|
||||
@@ -179,7 +179,7 @@ impl PyList {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__setitem__")]
|
||||
#[pymethod(magic)]
|
||||
fn setitem(
|
||||
&self,
|
||||
needle: PyObjectRef,
|
||||
@@ -214,7 +214,7 @@ impl PyList {
|
||||
elements.set_slice_items(vm, &slice, items.as_slice())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<String> {
|
||||
let s = if let Some(_guard) = ReprGuard::enter(vm, zelf.as_object()) {
|
||||
let elements = zelf.borrow_vec().to_vec();
|
||||
@@ -230,7 +230,7 @@ impl PyList {
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__mul__")]
|
||||
#[pymethod(magic)]
|
||||
fn mul(&self, counter: isize, vm: &VirtualMachine) -> PyObjectRef {
|
||||
let new_elements = sequence::seq_mul(&self.borrow_vec(), counter)
|
||||
.cloned()
|
||||
@@ -238,12 +238,12 @@ impl PyList {
|
||||
vm.ctx.new_list(new_elements)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rmul__")]
|
||||
#[pymethod(magic)]
|
||||
fn rmul(&self, counter: isize, vm: &VirtualMachine) -> PyObjectRef {
|
||||
self.mul(counter, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__imul__")]
|
||||
#[pymethod(magic)]
|
||||
fn imul(zelf: PyRef<Self>, counter: isize) -> PyRef<Self> {
|
||||
let mut elements = zelf.borrow_vec_mut();
|
||||
let mut new_elements: Vec<PyObjectRef> =
|
||||
@@ -263,7 +263,7 @@ impl PyList {
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__contains__")]
|
||||
#[pymethod(magic)]
|
||||
fn contains(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
|
||||
for element in self.borrow_vec().to_vec().iter() {
|
||||
if vm.identical_or_equal(element, &needle)? {
|
||||
@@ -346,7 +346,7 @@ impl PyList {
|
||||
.map(drop)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__delitem__")]
|
||||
#[pymethod(magic)]
|
||||
fn delitem(&self, subscript: SequenceIndex, vm: &VirtualMachine) -> PyResult<()> {
|
||||
match subscript {
|
||||
SequenceIndex::Int(index) => self.delindex(index, vm),
|
||||
@@ -397,7 +397,7 @@ impl PyList {
|
||||
PyList::default().into_ref_with_type(vm, cls)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__init__")]
|
||||
#[pymethod(magic)]
|
||||
fn init(&self, iterable: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult<()> {
|
||||
let mut elements = if let OptionalArg::Present(iterable) = iterable {
|
||||
vm.extract_elements(&iterable)?
|
||||
@@ -494,7 +494,7 @@ impl PyListIterator {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__setstate__")]
|
||||
#[pymethod(magic)]
|
||||
fn setstate(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
|
||||
// When we're exhausted, just return.
|
||||
if let Exhausted = self.status.load() {
|
||||
|
||||
@@ -69,13 +69,13 @@ impl PyMappingProxy {
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__getitem__")]
|
||||
#[pymethod(magic)]
|
||||
pub fn getitem(&self, key: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
self.get_inner(key.clone(), vm)?
|
||||
.ok_or_else(|| vm.new_key_error(key))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__contains__")]
|
||||
#[pymethod(magic)]
|
||||
pub fn contains(&self, key: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
match &self.mapping {
|
||||
MappingProxyInner::Class(class) => {
|
||||
|
||||
@@ -227,7 +227,7 @@ impl PyStr {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__add__")]
|
||||
#[pymethod(magic)]
|
||||
fn add(zelf: PyRef<Self>, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
if let Some(other) = other.payload::<PyStr>() {
|
||||
Ok(vm.ctx.new_str(zelf.value.py_add(other.as_ref())))
|
||||
@@ -242,17 +242,17 @@ impl PyStr {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__bool__")]
|
||||
#[pymethod(magic)]
|
||||
fn bool(&self) -> bool {
|
||||
!self.value.is_empty()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__contains__")]
|
||||
#[pymethod(magic)]
|
||||
fn contains(&self, needle: PyStrRef) -> bool {
|
||||
self.value.contains(needle.as_str())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__getitem__")]
|
||||
#[pymethod(magic)]
|
||||
fn getitem(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
let s = match self.get_item(vm, needle, Self::NAME)? {
|
||||
Either::A(ch) => ch.to_string(),
|
||||
@@ -322,23 +322,23 @@ impl PyStr {
|
||||
ffi::CString::new(self.as_str()).map_err(|err| err.into_pyexception(vm))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__sizeof__")]
|
||||
#[pymethod(magic)]
|
||||
fn sizeof(&self) -> usize {
|
||||
size_of::<Self>() + self.as_str().len() * size_of::<u8>()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__mul__")]
|
||||
#[pymethod(name = "__rmul__")]
|
||||
#[pymethod(magic)]
|
||||
fn mul(&self, value: isize) -> String {
|
||||
self.value.repeat(value.to_usize().unwrap_or(0))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__str__")]
|
||||
#[pymethod(magic)]
|
||||
fn str(zelf: PyRef<Self>) -> PyStrRef {
|
||||
zelf
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
pub(crate) fn repr(&self, vm: &VirtualMachine) -> PyResult<String> {
|
||||
let in_len = self.value.len();
|
||||
let mut out_len = 0usize;
|
||||
@@ -602,7 +602,7 @@ impl PyStr {
|
||||
Ok(formatted)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rmod__")]
|
||||
#[pymethod(magic)]
|
||||
fn rmod(&self, _values: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
|
||||
vm.ctx.not_implemented()
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ impl PySuper {
|
||||
Ok(Self { typ, obj })
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
fn repr(&self) -> String {
|
||||
let typname = &self.typ.name;
|
||||
match self.obj {
|
||||
|
||||
@@ -216,7 +216,7 @@ impl PyRange {
|
||||
self.step.clone()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__reversed__")]
|
||||
#[pymethod(magic)]
|
||||
fn reversed(&self, vm: &VirtualMachine) -> PyResult {
|
||||
let start = self.start.as_bigint();
|
||||
let step = self.step.as_bigint();
|
||||
@@ -252,12 +252,12 @@ impl PyRange {
|
||||
)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__len__")]
|
||||
#[pymethod(magic)]
|
||||
fn len(&self) -> BigInt {
|
||||
self.length()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
fn repr(&self) -> String {
|
||||
if self.step.as_bigint().is_one() {
|
||||
format!("range({}, {})", self.start, self.stop)
|
||||
@@ -266,12 +266,12 @@ impl PyRange {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__bool__")]
|
||||
#[pymethod(magic)]
|
||||
fn bool(&self) -> bool {
|
||||
!self.is_empty()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__contains__")]
|
||||
#[pymethod(magic)]
|
||||
fn contains(&self, needle: PyObjectRef, vm: &VirtualMachine) -> bool {
|
||||
// Only accept ints, not subclasses.
|
||||
if let Some(int) = needle.payload_if_exact::<PyInt>(vm) {
|
||||
@@ -291,7 +291,7 @@ impl PyRange {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__reduce__")]
|
||||
#[pymethod(magic)]
|
||||
fn reduce(&self, vm: &VirtualMachine) -> (PyTypeRef, PyObjectRef) {
|
||||
let range_paramters: Vec<PyObjectRef> = vec![&self.start, &self.stop, &self.step]
|
||||
.iter()
|
||||
@@ -333,7 +333,7 @@ impl PyRange {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__getitem__")]
|
||||
#[pymethod(magic)]
|
||||
fn getitem(&self, subscript: RangeIndex, vm: &VirtualMachine) -> PyResult {
|
||||
match subscript {
|
||||
RangeIndex::Slice(slice) => {
|
||||
|
||||
@@ -327,12 +327,12 @@ impl PySet {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__len__")]
|
||||
#[pymethod(magic)]
|
||||
fn len(&self) -> usize {
|
||||
self.inner.len()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__sizeof__")]
|
||||
#[pymethod(magic)]
|
||||
fn sizeof(&self) -> usize {
|
||||
std::mem::size_of::<Self>() + self.inner.sizeof()
|
||||
}
|
||||
@@ -344,7 +344,7 @@ impl PySet {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__contains__")]
|
||||
#[pymethod(magic)]
|
||||
fn contains(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
|
||||
self.inner.contains(&needle, vm)
|
||||
}
|
||||
@@ -388,47 +388,35 @@ impl PySet {
|
||||
self.inner.isdisjoint(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__or__")]
|
||||
#[pymethod(name = "__ror__")]
|
||||
#[pymethod(magic)]
|
||||
fn or(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
|
||||
self.union(other.iterable, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__ror__")]
|
||||
fn ror(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
|
||||
self.or(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__and__")]
|
||||
#[pymethod(name = "__rand__")]
|
||||
#[pymethod(magic)]
|
||||
fn and(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
|
||||
self.intersection(other.iterable, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rand__")]
|
||||
fn rand(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
|
||||
self.and(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__sub__")]
|
||||
#[pymethod(magic)]
|
||||
fn sub(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
|
||||
self.difference(other.iterable, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rsub__")]
|
||||
#[pymethod(magic)]
|
||||
fn rsub(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
|
||||
self.sub(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__xor__")]
|
||||
#[pymethod(name = "__rxor__")]
|
||||
#[pymethod(magic)]
|
||||
fn xor(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
|
||||
self.symmetric_difference(other.iterable, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rxor__")]
|
||||
fn rxor(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
|
||||
self.xor(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
|
||||
let s = if zelf.inner.len() == 0 {
|
||||
"set()".to_owned()
|
||||
@@ -467,7 +455,7 @@ impl PySet {
|
||||
self.inner.pop(vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__ior__")]
|
||||
#[pymethod(magic)]
|
||||
fn ior(zelf: PyRef<Self>, iterable: SetIterable, vm: &VirtualMachine) -> PyResult {
|
||||
zelf.inner.update(iterable.iterable, vm)?;
|
||||
Ok(zelf.as_object().clone())
|
||||
@@ -485,7 +473,7 @@ impl PySet {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__iand__")]
|
||||
#[pymethod(magic)]
|
||||
fn iand(zelf: PyRef<Self>, iterable: SetIterable, vm: &VirtualMachine) -> PyResult {
|
||||
zelf.inner.intersection_update(iterable.iterable, vm)?;
|
||||
Ok(zelf.as_object().clone())
|
||||
@@ -497,7 +485,7 @@ impl PySet {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__isub__")]
|
||||
#[pymethod(magic)]
|
||||
fn isub(zelf: PyRef<Self>, iterable: SetIterable, vm: &VirtualMachine) -> PyResult {
|
||||
zelf.inner.difference_update(iterable.iterable, vm)?;
|
||||
Ok(zelf.as_object().clone())
|
||||
@@ -513,7 +501,7 @@ impl PySet {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__ixor__")]
|
||||
#[pymethod(magic)]
|
||||
fn ixor(zelf: PyRef<Self>, iterable: SetIterable, vm: &VirtualMachine) -> PyResult {
|
||||
zelf.inner
|
||||
.symmetric_difference_update(iterable.iterable, vm)?;
|
||||
@@ -594,12 +582,12 @@ impl PyFrozenSet {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__len__")]
|
||||
#[pymethod(magic)]
|
||||
fn len(&self) -> usize {
|
||||
self.inner.len()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__sizeof__")]
|
||||
#[pymethod(magic)]
|
||||
fn sizeof(&self) -> usize {
|
||||
std::mem::size_of::<Self>() + self.inner.sizeof()
|
||||
}
|
||||
@@ -611,7 +599,7 @@ impl PyFrozenSet {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__contains__")]
|
||||
#[pymethod(magic)]
|
||||
fn contains(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
|
||||
self.inner.contains(&needle, vm)
|
||||
}
|
||||
@@ -655,47 +643,35 @@ impl PyFrozenSet {
|
||||
self.inner.isdisjoint(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__or__")]
|
||||
#[pymethod(name = "__ror__")]
|
||||
#[pymethod(magic)]
|
||||
fn or(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
|
||||
self.union(other.iterable, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__ror__")]
|
||||
fn ror(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
|
||||
self.or(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__and__")]
|
||||
#[pymethod(name = "__rand__")]
|
||||
#[pymethod(magic)]
|
||||
fn and(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
|
||||
self.intersection(other.iterable, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rand__")]
|
||||
fn rand(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
|
||||
self.and(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__sub__")]
|
||||
#[pymethod(magic)]
|
||||
fn sub(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
|
||||
self.difference(other.iterable, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rsub__")]
|
||||
#[pymethod(magic)]
|
||||
fn rsub(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
|
||||
self.sub(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__xor__")]
|
||||
#[pymethod(name = "__rxor__")]
|
||||
#[pymethod(magic)]
|
||||
fn xor(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
|
||||
self.symmetric_difference(other.iterable, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rxor__")]
|
||||
fn rxor(&self, other: SetIterable, vm: &VirtualMachine) -> PyResult<Self> {
|
||||
self.xor(other, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult {
|
||||
let inner = &zelf.inner;
|
||||
let s = if inner.len() == 0 {
|
||||
|
||||
@@ -59,7 +59,7 @@ impl PySlice {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
fn repr(&self, vm: &VirtualMachine) -> PyResult<String> {
|
||||
let start_repr = vm.to_repr(self.start_ref(vm))?;
|
||||
let stop_repr = vm.to_repr(&self.stop)?;
|
||||
|
||||
@@ -96,7 +96,7 @@ impl PyTuple {
|
||||
&self.elements
|
||||
}
|
||||
|
||||
#[pymethod(name = "__add__")]
|
||||
#[pymethod(magic)]
|
||||
fn add(
|
||||
zelf: PyRef<Self>,
|
||||
other: PyObjectRef,
|
||||
@@ -120,7 +120,7 @@ impl PyTuple {
|
||||
PyArithmaticValue::from_option(added.ok())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__bool__")]
|
||||
#[pymethod(magic)]
|
||||
fn bool(&self) -> bool {
|
||||
!self.elements.is_empty()
|
||||
}
|
||||
@@ -136,7 +136,7 @@ impl PyTuple {
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__len__")]
|
||||
#[pymethod(magic)]
|
||||
#[inline]
|
||||
pub fn len(&self) -> usize {
|
||||
self.elements.len()
|
||||
@@ -147,7 +147,7 @@ impl PyTuple {
|
||||
self.elements.is_empty()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<String> {
|
||||
let s = if let Some(_guard) = ReprGuard::enter(vm, zelf.as_object()) {
|
||||
let mut str_parts = Vec::with_capacity(zelf.elements.len());
|
||||
@@ -167,8 +167,8 @@ impl PyTuple {
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__mul__")]
|
||||
#[pymethod(name = "__rmul__")]
|
||||
#[pymethod(magic)]
|
||||
fn mul(zelf: PyRef<Self>, counter: isize, vm: &VirtualMachine) -> PyRef<Self> {
|
||||
if zelf.elements.is_empty() || counter == 0 {
|
||||
vm.ctx.empty_tuple.clone()
|
||||
@@ -187,7 +187,7 @@ impl PyTuple {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__getitem__")]
|
||||
#[pymethod(magic)]
|
||||
fn getitem(zelf: PyRef<Self>, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
let result = match zelf.elements.as_ref().get_item(vm, needle, Self::NAME)? {
|
||||
Either::A(obj) => obj,
|
||||
@@ -232,7 +232,7 @@ impl PyTuple {
|
||||
Err(vm.new_value_error("tuple.index(x): x not in tuple".to_owned()))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__contains__")]
|
||||
#[pymethod(magic)]
|
||||
fn contains(&self, needle: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
|
||||
for element in self.elements.iter() {
|
||||
if vm.identical_or_equal(element, &needle)? {
|
||||
@@ -344,7 +344,7 @@ impl PyTupleIterator {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__setstate__")]
|
||||
#[pymethod(magic)]
|
||||
fn setstate(&self, state: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
|
||||
// When we're exhausted, just return.
|
||||
if let Exhausted = self.status.load() {
|
||||
|
||||
@@ -37,7 +37,7 @@ impl PyWeakProxy {
|
||||
.into_ref_with_type(vm, cls)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__getattr__")]
|
||||
#[pymethod(magic)]
|
||||
fn getattr(&self, attr_name: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
match self.weak.upgrade() {
|
||||
Some(obj) => vm.get_attribute(obj, attr_name),
|
||||
|
||||
@@ -67,7 +67,7 @@ impl PyBaseException {
|
||||
PyBaseException::new(args.args, vm).into_ref_with_type(vm, cls)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__init__")]
|
||||
#[pymethod(magic)]
|
||||
fn init(&self, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
|
||||
*self.args.write() = PyTupleRef::with_elements(args.args, &vm.ctx);
|
||||
Ok(())
|
||||
@@ -137,7 +137,7 @@ impl PyBaseException {
|
||||
Ok(zelf.as_object().clone())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__str__")]
|
||||
#[pymethod(magic)]
|
||||
fn str(&self, vm: &VirtualMachine) -> PyStrRef {
|
||||
let str_args = exception_args_as_string(vm, self.args(), true);
|
||||
match str_args.into_iter().exactly_one() {
|
||||
@@ -147,7 +147,7 @@ impl PyBaseException {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> String {
|
||||
let repr_args = exception_args_as_string(vm, zelf.args(), false);
|
||||
let cls = zelf.class();
|
||||
|
||||
@@ -711,7 +711,7 @@ impl PyArray {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__delitem__")]
|
||||
#[pymethod(magic)]
|
||||
fn delitem(
|
||||
zelf: PyRef<Self>,
|
||||
needle: Either<isize, PySliceRef>,
|
||||
@@ -723,7 +723,7 @@ impl PyArray {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__add__")]
|
||||
#[pymethod(magic)]
|
||||
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
|
||||
if let Some(other) = other.payload::<PyArray>() {
|
||||
self.read()
|
||||
@@ -737,7 +737,7 @@ impl PyArray {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__iadd__")]
|
||||
#[pymethod(magic)]
|
||||
fn iadd(zelf: PyRef<Self>, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
|
||||
if zelf.is(&other) {
|
||||
zelf.try_resizable(vm)?.imul(2);
|
||||
@@ -753,28 +753,24 @@ impl PyArray {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethod(name = "__mul__")]
|
||||
#[pymethod(name = "__rmul__")]
|
||||
#[pymethod(magic)]
|
||||
fn mul(&self, counter: isize, vm: &VirtualMachine) -> PyRef<Self> {
|
||||
PyArray::from(self.read().mul(counter)).into_ref(vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__rmul__")]
|
||||
fn rmul(&self, counter: isize, vm: &VirtualMachine) -> PyRef<Self> {
|
||||
self.mul(counter, vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__imul__")]
|
||||
#[pymethod(magic)]
|
||||
fn imul(zelf: PyRef<Self>, counter: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
|
||||
zelf.try_resizable(vm)?.imul(counter);
|
||||
Ok(zelf)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
fn repr(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<String> {
|
||||
zelf.read().repr(vm)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__len__")]
|
||||
#[pymethod(magic)]
|
||||
pub(crate) fn len(&self) -> usize {
|
||||
self.read().len()
|
||||
}
|
||||
|
||||
@@ -994,7 +994,7 @@ mod decl {
|
||||
Ok(PyTupleRef::with_elements(tee_vec, &vm.ctx))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__copy__")]
|
||||
#[pymethod(magic)]
|
||||
fn copy(&self, vm: &VirtualMachine) -> PyResult {
|
||||
Ok(PyItertoolsTee {
|
||||
tee_data: PyRc::clone(&self.tee_data),
|
||||
|
||||
@@ -956,12 +956,12 @@ mod _os {
|
||||
self.exhausted.store(true);
|
||||
}
|
||||
|
||||
#[pymethod(name = "__enter__")]
|
||||
#[pymethod(magic)]
|
||||
fn enter(zelf: PyRef<Self>) -> PyRef<Self> {
|
||||
zelf
|
||||
}
|
||||
|
||||
#[pymethod(name = "__exit__")]
|
||||
#[pymethod(magic)]
|
||||
fn exit(zelf: PyRef<Self>, _args: FuncArgs) {
|
||||
zelf.close()
|
||||
}
|
||||
|
||||
@@ -195,7 +195,7 @@ impl PySocket {
|
||||
Self::default().into_ref_with_type(vm, cls)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__init__")]
|
||||
#[pymethod(magic)]
|
||||
fn init(
|
||||
&self,
|
||||
family: OptionalArg<i32>,
|
||||
|
||||
@@ -260,7 +260,7 @@ impl PyJsValue {
|
||||
instance_of(&self.value, &rhs.value).map_err(|err| new_js_error(vm, err))
|
||||
}
|
||||
|
||||
#[pymethod(name = "__repr__")]
|
||||
#[pymethod(magic)]
|
||||
fn repr(&self) -> String {
|
||||
format!("{:?}", self.value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user