Skip to content

Refactor and new sequence traits, generic sequence operation #3445

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 7 commits into from
Nov 22, 2021
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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 19 additions & 29 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod array {
BufferDescriptor, BufferMethods, BufferResizeGuard, PyBuffer, PyIterReturn,
PyMappingMethods,
},
sequence::{SequenceMutOp, SequenceOp},
sliceable::{PySliceableSequence, PySliceableSequenceMut, SaturatedSlice, SequenceIndex},
types::{
AsBuffer, AsMapping, Comparable, Constructor, IterNext, IterNextIterable, Iterable,
Expand Down Expand Up @@ -387,34 +388,24 @@ mod array {
}
}

fn mul(&self, counter: usize) -> Self {
fn mul(&self, value: isize, vm: &VirtualMachine) -> PyResult<Self> {
match self {
$(ArrayContentType::$n(v) => {
let elements = v.repeat(counter);
ArrayContentType::$n(elements)
// MemoryError instead Overflow Error, hard to says it is right
// but it is how cpython doing right now
let elements = v.mul(vm, value).map_err(|_| vm.new_memory_error("".to_owned()))?;
Ok(ArrayContentType::$n(elements))
})*
}
}

fn clear(&mut self) {
fn imul(&mut self, value: isize, vm: &VirtualMachine) -> PyResult<()> {
match self {
$(ArrayContentType::$n(v) => v.clear(),)*
}
}

fn imul(&mut self, counter: usize) {
if counter == 0 {
self.clear();
} else if counter != 1 {
match self {
$(ArrayContentType::$n(v) => {
let old = v.clone();
v.reserve((counter - 1) * old.len());
for _ in 1..counter {
v.extend(&old);
}
})*
}
$(ArrayContentType::$n(v) => {
// MemoryError instead Overflow Error, hard to says it is right
// but it is how cpython doing right now
v.imul(vm, value).map_err(|_| vm.new_memory_error("".to_owned()))
})*
}
}

Expand Down Expand Up @@ -742,8 +733,7 @@ mod array {
fn extend(zelf: PyRef<Self>, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
let mut w = zelf.try_resizable(vm)?;
if zelf.is(&obj) {
w.imul(2);
Ok(())
w.imul(2, vm)
} else if let Some(array) = obj.payload::<PyArray>() {
w.iadd(&*array.read(), vm)
} else {
Expand Down Expand Up @@ -856,7 +846,7 @@ mod array {
if n < 0 {
return Err(vm.new_value_error("negative count".to_owned()));
}
let n = vm.check_repeat_or_memory_error(itemsize, n)?;
let n = vm.check_repeat_or_overflow_error(itemsize, n)?;
let nbytes = n * itemsize;

let b = vm.call_method(&f, "read", (nbytes,))?;
Expand Down Expand Up @@ -1044,7 +1034,7 @@ mod array {
vm: &VirtualMachine,
) -> PyResult<PyRef<Self>> {
if zelf.is(&other) {
zelf.try_resizable(vm)?.imul(2);
zelf.try_resizable(vm)?.imul(2, vm)?;
} else if let Some(other) = other.payload::<PyArray>() {
zelf.try_resizable(vm)?.iadd(&*other.read(), vm)?;
} else {
Expand All @@ -1059,14 +1049,14 @@ mod array {
#[pymethod(name = "__rmul__")]
#[pymethod(magic)]
fn mul(&self, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
let value = vm.check_repeat_or_memory_error(self.len(), value)?;
Ok(Self::from(self.read().mul(value)).into_ref(vm))
self.read()
.mul(value, vm)
.map(|x| Self::from(x).into_ref(vm))
}

#[pymethod(magic)]
fn imul(zelf: PyRef<Self>, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
let value = vm.check_repeat_or_memory_error(zelf.len(), value)?;
zelf.try_resizable(vm)?.imul(value);
zelf.try_resizable(vm)?.imul(value, vm)?;
Ok(zelf)
}

Expand Down
1 change: 1 addition & 0 deletions vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ adler32 = "1.0.3"
flate2 = "1.0.20"
once_cell = "1"
memoffset = "0.6"
optional = "0.5"

# RustPython crates implementing functionality based on CPython
sre-engine = "0.1.2"
Expand Down
22 changes: 5 additions & 17 deletions vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl PyByteArray {
}
}

fn irepeat(zelf: &crate::PyObjectView<Self>, n: usize, vm: &VirtualMachine) -> PyResult<()> {
fn irepeat(zelf: &crate::PyObjectView<Self>, n: isize, vm: &VirtualMachine) -> PyResult<()> {
if n == 1 {
return Ok(());
}
Expand All @@ -300,19 +300,8 @@ impl PyByteArray {
};
}
};
let elements = &mut w.elements;

if n == 0 {
elements.clear();
} else if n != 1 {
let old = elements.clone();

elements.reserve((n - 1) * old.len());
for _ in 1..n {
elements.extend(&old);
}
}
Ok(())
w.imul(n, vm)
}

#[pymethod]
Expand Down Expand Up @@ -635,14 +624,13 @@ impl PyByteArray {
#[pymethod(name = "__rmul__")]
#[pymethod(magic)]
fn mul(&self, value: isize, vm: &VirtualMachine) -> PyResult<Self> {
vm.check_repeat_or_memory_error(self.len(), value)
.map(|value| self.inner().repeat(value).into())
self.inner().mul(value, vm).map(|x| x.into())
}

#[pymethod(magic)]
fn imul(zelf: PyRef<Self>, value: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
vm.check_repeat_or_memory_error(zelf.len(), value)
.and_then(|value| Self::irepeat(&zelf, value, vm).map(|_| zelf))
Self::irepeat(&zelf, value, vm)?;
Ok(zelf)
}

#[pymethod(name = "__mod__")]
Expand Down
11 changes: 3 additions & 8 deletions vm/src/builtins/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,9 @@ impl PyBytes {
// This only works for `bytes` itself, not its subclasses.
return Ok(zelf);
}
// todo: map err to overflow.
vm.check_repeat_or_memory_error(zelf.inner.len(), value)
.map(|value| {
let bytes: PyBytes = zelf.inner.repeat(value).into();
bytes.into_ref(vm)
})
// see issue 45044 on b.p.o.
.map_err(|_| vm.new_overflow_error("repeated bytes are too long".to_owned()))
zelf.inner
.mul(value, vm)
.map(|x| Self::from(x).into_ref(vm))
}

#[pymethod(name = "__mod__")]
Expand Down
Loading