Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 84c27f3

Browse filesBrowse files
authored
Merge pull request #4535 from youknowone/fix-mul-and-union
Fix sequence mul types and add AsNumber for PyUnion
2 parents 42c0752 + afb64c8 commit 84c27f3
Copy full SHA for 84c27f3

File tree

Expand file treeCollapse file tree

8 files changed

+33
-22
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+33
-22
lines changed

‎vm/src/builtins/bytearray.rs

Copy file name to clipboardExpand all lines: vm/src/builtins/bytearray.rs
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ impl AsSequence for PyByteArray {
820820
}),
821821
repeat: atomic_func!(|seq, n, vm| {
822822
PyByteArray::sequence_downcast(seq)
823-
.mul(n as isize, vm)
823+
.mul(n, vm)
824824
.map(|x| x.into_pyobject(vm))
825825
}),
826826
item: atomic_func!(|seq, i, vm| {
@@ -849,7 +849,7 @@ impl AsSequence for PyByteArray {
849849
}),
850850
inplace_repeat: atomic_func!(|seq, n, vm| {
851851
let zelf = PyByteArray::sequence_downcast(seq).to_owned();
852-
PyByteArray::imul(zelf, n as isize, vm).map(|x| x.into())
852+
PyByteArray::imul(zelf, n, vm).map(|x| x.into())
853853
}),
854854
};
855855
&AS_SEQUENCE

‎vm/src/builtins/bytes.rs

Copy file name to clipboardExpand all lines: vm/src/builtins/bytes.rs
+5-4Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,10 +604,11 @@ impl AsSequence for PyBytes {
604604
.map(|x| vm.ctx.new_bytes(x).into())
605605
}),
606606
repeat: atomic_func!(|seq, n, vm| {
607-
Ok(vm
608-
.ctx
609-
.new_bytes(PyBytes::sequence_downcast(seq).repeat(n))
610-
.into())
607+
if let Ok(zelf) = seq.obj.to_owned().downcast::<PyBytes>() {
608+
PyBytes::mul(zelf, n, vm).to_pyresult(vm)
609+
} else {
610+
Err(vm.new_type_error("bad argument type for built-in operation".to_owned()))
611+
}
611612
}),
612613
item: atomic_func!(|seq, i, vm| {
613614
PyBytes::sequence_downcast(seq)

‎vm/src/builtins/list.rs

Copy file name to clipboardExpand all lines: vm/src/builtins/list.rs
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,7 @@ impl AsSequence for PyList {
431431
.map(|x| x.into())
432432
}),
433433
repeat: atomic_func!(|seq, n, vm| {
434-
PyList::sequence_downcast(seq)
435-
.mul(n as isize, vm)
436-
.map(|x| x.into())
434+
PyList::sequence_downcast(seq).mul(n, vm).map(|x| x.into())
437435
}),
438436
item: atomic_func!(|seq, i, vm| {
439437
PyList::sequence_downcast(seq)
@@ -458,7 +456,7 @@ impl AsSequence for PyList {
458456
}),
459457
inplace_repeat: atomic_func!(|seq, n, vm| {
460458
let zelf = PyList::sequence_downcast(seq);
461-
zelf.borrow_vec_mut().imul(vm, n as isize)?;
459+
zelf.borrow_vec_mut().imul(vm, n)?;
462460
Ok(zelf.to_owned().into())
463461
}),
464462
};

‎vm/src/builtins/str.rs

Copy file name to clipboardExpand all lines: vm/src/builtins/str.rs
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ impl AsSequence for PyStr {
12911291
}),
12921292
repeat: atomic_func!(|seq, n, vm| {
12931293
let zelf = PyStr::sequence_downcast(seq);
1294-
PyStr::mul(zelf.to_owned(), n as isize, vm).map(|x| x.into())
1294+
PyStr::mul(zelf.to_owned(), n, vm).map(|x| x.into())
12951295
}),
12961296
item: atomic_func!(|seq, i, vm| {
12971297
let zelf = PyStr::sequence_downcast(seq);

‎vm/src/builtins/tuple.rs

Copy file name to clipboardExpand all lines: vm/src/builtins/tuple.rs
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ impl AsSequence for PyTuple {
353353
}),
354354
repeat: atomic_func!(|seq, n, vm| {
355355
let zelf = PyTuple::sequence_downcast(seq);
356-
PyTuple::mul(zelf.to_owned(), n as isize, vm).map(|x| x.into())
356+
PyTuple::mul(zelf.to_owned(), n, vm).map(|x| x.into())
357357
}),
358358
item: atomic_func!(|seq, i, vm| {
359359
let zelf = PyTuple::sequence_downcast(seq);

‎vm/src/builtins/union.rs

Copy file name to clipboardExpand all lines: vm/src/builtins/union.rs
+16-4Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use crate::{
66
builtins::{PyFrozenSet, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType},
77
class::PyClassImpl,
88
common::hash,
9-
convert::ToPyObject,
9+
convert::{ToPyObject, ToPyResult},
1010
function::PyComparisonValue,
11-
protocol::PyMappingMethods,
12-
types::{AsMapping, Comparable, GetAttr, Hashable, PyComparisonOp},
11+
protocol::{PyMappingMethods, PyNumberMethods},
12+
types::{AsMapping, AsNumber, Comparable, GetAttr, Hashable, PyComparisonOp},
1313
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
1414
VirtualMachine,
1515
};
@@ -35,7 +35,7 @@ impl PyPayload for PyUnion {
3535
}
3636
}
3737

38-
#[pyclass(with(Hashable, Comparable, AsMapping), flags(BASETYPE))]
38+
#[pyclass(flags(BASETYPE), with(Hashable, Comparable, AsMapping, AsNumber))]
3939
impl PyUnion {
4040
pub fn new(args: PyTupleRef, vm: &VirtualMachine) -> Self {
4141
let parameters = make_parameters(&args, vm);
@@ -255,6 +255,18 @@ impl AsMapping for PyUnion {
255255
}
256256
}
257257

258+
impl AsNumber for PyUnion {
259+
fn as_number() -> &'static PyNumberMethods {
260+
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
261+
or: atomic_func!(|num, other, vm| {
262+
PyUnion::or(num.obj.to_owned(), other.to_owned(), vm).to_pyresult(vm)
263+
}),
264+
..PyNumberMethods::NOT_IMPLEMENTED
265+
});
266+
&AS_NUMBER
267+
}
268+
}
269+
258270
impl Comparable for PyUnion {
259271
fn cmp(
260272
zelf: &crate::Py<Self>,

‎vm/src/protocol/sequence.rs

Copy file name to clipboardExpand all lines: vm/src/protocol/sequence.rs
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ impl PyObject {
2929
pub struct PySequenceMethods {
3030
pub length: AtomicCell<Option<fn(PySequence, &VirtualMachine) -> PyResult<usize>>>,
3131
pub concat: AtomicCell<Option<fn(PySequence, &PyObject, &VirtualMachine) -> PyResult>>,
32-
pub repeat: AtomicCell<Option<fn(PySequence, usize, &VirtualMachine) -> PyResult>>,
32+
pub repeat: AtomicCell<Option<fn(PySequence, isize, &VirtualMachine) -> PyResult>>,
3333
pub item: AtomicCell<Option<fn(PySequence, isize, &VirtualMachine) -> PyResult>>,
3434
pub ass_item: AtomicCell<
3535
Option<fn(PySequence, isize, Option<PyObjectRef>, &VirtualMachine) -> PyResult<()>>,
3636
>,
3737
pub contains: AtomicCell<Option<fn(PySequence, &PyObject, &VirtualMachine) -> PyResult<bool>>>,
3838
pub inplace_concat: AtomicCell<Option<fn(PySequence, &PyObject, &VirtualMachine) -> PyResult>>,
39-
pub inplace_repeat: AtomicCell<Option<fn(PySequence, usize, &VirtualMachine) -> PyResult>>,
39+
pub inplace_repeat: AtomicCell<Option<fn(PySequence, isize, &VirtualMachine) -> PyResult>>,
4040
}
4141

4242
impl Debug for PySequenceMethods {
@@ -130,7 +130,7 @@ impl PySequence<'_> {
130130
)))
131131
}
132132

133-
pub fn repeat(self, n: usize, vm: &VirtualMachine) -> PyResult {
133+
pub fn repeat(self, n: isize, vm: &VirtualMachine) -> PyResult {
134134
if let Some(f) = self.methods.repeat.load() {
135135
return f(self, n, vm);
136136
}
@@ -168,7 +168,7 @@ impl PySequence<'_> {
168168
)))
169169
}
170170

171-
pub fn inplace_repeat(self, n: usize, vm: &VirtualMachine) -> PyResult {
171+
pub fn inplace_repeat(self, n: isize, vm: &VirtualMachine) -> PyResult {
172172
if let Some(f) = self.methods.inplace_repeat.load() {
173173
return f(self, n, vm);
174174
}

‎vm/src/stdlib/collections.rs

Copy file name to clipboardExpand all lines: vm/src/stdlib/collections.rs
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ mod _collections {
525525
}),
526526
repeat: atomic_func!(|seq, n, vm| {
527527
PyDeque::sequence_downcast(seq)
528-
.mul(n as isize, vm)
528+
.mul(n, vm)
529529
.map(|x| x.into_ref(vm).into())
530530
}),
531531
item: atomic_func!(|seq, i, vm| PyDeque::sequence_downcast(seq).getitem(i, vm)),
@@ -547,7 +547,7 @@ mod _collections {
547547
}),
548548
inplace_repeat: atomic_func!(|seq, n, vm| {
549549
let zelf = PyDeque::sequence_downcast(seq);
550-
PyDeque::imul(zelf.to_owned(), n as isize, vm).map(|x| x.into())
550+
PyDeque::imul(zelf.to_owned(), n, vm).map(|x| x.into())
551551
}),
552552
};
553553

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.