Skip to content

Commit b65849a

Browse files
committed
fix rebase
1 parent a15bb3c commit b65849a

File tree

7 files changed

+65
-48
lines changed

7 files changed

+65
-48
lines changed

vm/src/builtins/bytearray.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ use crate::{
1717
PyMappedRwLockReadGuard, PyMappedRwLockWriteGuard, PyMutex, PyRwLock,
1818
PyRwLockReadGuard, PyRwLockWriteGuard,
1919
},
20+
static_cell,
2021
},
2122
function::{ArgBytesLike, ArgIterable, FuncArgs, IntoPyObject, OptionalArg, OptionalOption},
2223
protocol::{
2324
BufferDescriptor, BufferMethods, BufferResizeGuard, PyBuffer, PyIterReturn,
24-
PyMappingMethods,
25+
PyMappingMethods, PySequenceMethods,
2526
},
2627
sliceable::{PySliceableSequence, PySliceableSequenceMut, SequenceIndex},
2728
types::{
@@ -30,10 +31,11 @@ use crate::{
3031
},
3132
utils::Either,
3233
IdProtocol, PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyObject, PyObjectRef,
33-
PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
34+
PyObjectView, PyObjectWrap, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
35+
VirtualMachine,
3436
};
3537
use bstr::ByteSlice;
36-
use std::mem::size_of;
38+
use std::{borrow::Cow, mem::size_of};
3739

3840
/// "bytearray(iterable_of_ints) -> bytearray\n\
3941
/// bytearray(string, encoding[, errors]) -> bytearray\n\
@@ -800,7 +802,10 @@ impl AsMapping for PyByteArray {
800802
}
801803

802804
impl AsSequence for PyByteArray {
803-
fn as_sequence(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> Cow<'static, PySequenceMethods> {
805+
fn as_sequence(
806+
_zelf: &PyObjectView<Self>,
807+
_vm: &VirtualMachine,
808+
) -> Cow<'static, PySequenceMethods> {
804809
static_cell! {
805810
static METHODS: PySequenceMethods;
806811
}
@@ -829,16 +834,17 @@ impl AsSequence for PyByteArray {
829834
}
830835
}),
831836
contains: Some(|zelf, other, vm| {
832-
let other = <Either<PyBytesInner, PyIntRef>>::try_from_object(vm, other.clone())?;
837+
let other =
838+
<Either<PyBytesInner, PyIntRef>>::try_from_object(vm, other.to_owned())?;
833839
zelf.payload::<Self>().unwrap().contains(other, vm)
834840
}),
835841
inplace_concat: Some(|zelf, other, vm| {
836-
let other = ArgBytesLike::try_from_object(vm, other.clone())?;
837-
let zelf = zelf.clone().downcast::<Self>().unwrap();
842+
let other = ArgBytesLike::try_from_object(vm, other.to_owned())?;
843+
let zelf = zelf.downcast_ref::<Self>().unwrap().to_owned();
838844
Self::iadd(zelf, other, vm).map(|x| x.into())
839845
}),
840846
inplace_repeat: Some(|zelf, n, vm| {
841-
let zelf = zelf.clone().downcast::<Self>().unwrap();
847+
let zelf = zelf.downcast_ref::<Self>().unwrap().to_owned();
842848
Self::imul(zelf, n as isize, vm).map(|x| x.into())
843849
}),
844850
}))

vm/src/builtins/bytes.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ use crate::{
66
bytes_decode, ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions,
77
ByteInnerSplitOptions, ByteInnerTranslateOptions, DecodeArgs, PyBytesInner,
88
},
9-
common::{hash::PyHash, lock::PyMutex},
9+
common::{hash::PyHash, lock::PyMutex, static_cell},
1010
function::{
1111
ArgBytesLike, ArgIterable, IntoPyObject, IntoPyResult, OptionalArg, OptionalOption,
1212
},
13-
protocol::{BufferDescriptor, BufferMethods, PyBuffer, PyIterReturn, PyMappingMethods},
13+
protocol::{
14+
BufferDescriptor, BufferMethods, PyBuffer, PyIterReturn, PyMappingMethods,
15+
PySequenceMethods,
16+
},
1417
types::{
1518
AsBuffer, AsMapping, AsSequence, Callable, Comparable, Constructor, Hashable, IterNext,
1619
IterNextIterable, Iterable, PyComparisonOp, Unconstructible,
1720
},
1821
utils::Either,
1922
IdProtocol, PyClassImpl, PyComparisonValue, PyContext, PyObject, PyObjectRef, PyObjectView,
20-
PyObjectWrap, PyRef, PyResult, PyValue, TryFromBorrowedObject, TypeProtocol, VirtualMachine,
23+
PyObjectWrap, PyRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol,
24+
VirtualMachine,
2125
};
2226
use bstr::ByteSlice;
23-
use std::mem::size_of;
2427
use std::ops::Deref;
2528
use std::{borrow::Cow, mem::size_of};
2629

@@ -595,7 +598,10 @@ impl AsMapping for PyBytes {
595598
}
596599

597600
impl AsSequence for PyBytes {
598-
fn as_sequence(_zelf: &PyRef<Self>, _vm: &VirtualMachine) -> Cow<'static, PySequenceMethods> {
601+
fn as_sequence(
602+
_zelf: &PyObjectView<Self>,
603+
_vm: &VirtualMachine,
604+
) -> Cow<'static, PySequenceMethods> {
599605
static_cell! {
600606
static METHODS: PySequenceMethods;
601607
}
@@ -616,7 +622,8 @@ impl AsSequence for PyBytes {
616622
}),
617623
item: Some(|zelf, i, vm| zelf.payload::<Self>().unwrap().inner.item(i, vm)),
618624
contains: Some(|zelf, other, vm| {
619-
let other = <Either<PyBytesInner, PyIntRef>>::try_from_object(vm, other.clone())?;
625+
let other =
626+
<Either<PyBytesInner, PyIntRef>>::try_from_object(vm, other.to_owned())?;
620627
zelf.payload::<Self>().unwrap().contains(other, vm)
621628
}),
622629
..Default::default()

vm/src/builtins/iter.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use crate::{
77
function::ArgCallable,
88
protocol::{PyIterReturn, PySequence},
99
types::{IterNext, IterNextIterable},
10-
ItemProtocol, PyClassImpl, PyContext, PyObject, PyObjectRef, PyResult, PyValue, VirtualMachine,
10+
PyClassImpl, PyContext, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject,
11+
VirtualMachine,
1112
};
1213
use rustpython_common::{
1314
lock::{PyMutex, PyRwLock, PyRwLockUpgradableReadGuard},

vm/src/builtins/memory.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -991,11 +991,14 @@ impl AsSequence for PyMemoryView {
991991
zelf.len(vm)
992992
}),
993993
item: Some(|zelf, i, vm| {
994-
let zelf = zelf.clone().downcast::<Self>().unwrap();
994+
let zelf = zelf.downcast_ref::<Self>().unwrap();
995995
zelf.try_not_released(vm)?;
996+
zelf.getitem_by_idx(i, vm)
996997
}),
998+
..Default::default()
997999
}))
9981000
}
1001+
}
9991002

10001003
impl Comparable for PyMemoryView {
10011004
fn cmp(

vm/src/bytesinner.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ impl PyBytesInner {
914914
self.elements.repeat(n)
915915
}
916916

917-
pub fn concat(&self, other: &PyObjectRef, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
917+
pub fn concat(&self, other: &PyObject, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
918918
let buffer = PyBuffer::try_from_borrowed_object(vm, other)?;
919919
let borrowed = buffer.as_contiguous();
920920
if let Some(other) = borrowed {
@@ -924,7 +924,7 @@ impl PyBytesInner {
924924
Ok(v)
925925
} else {
926926
let mut v = self.elements.clone();
927-
v.append(&mut buffer.to_contiguous());
927+
buffer.append_to(&mut v);
928928
Ok(v)
929929
}
930930
}

vm/src/protocol/sequence.rs

+23-27
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use crate::{
77
builtins::{PyList, PySlice},
88
common::static_cell,
99
function::IntoPyObject,
10-
IdProtocol, PyArithmeticValue, PyObjectRef, PyResult, PyValue, TryFromObject, TypeProtocol,
11-
VirtualMachine,
10+
IdProtocol, PyArithmeticValue, PyObject, PyObjectRef, PyResult, PyValue, TryFromObject,
11+
TypeProtocol, VirtualMachine,
1212
};
1313

1414
// Sequence Protocol
@@ -17,15 +17,15 @@ use crate::{
1717
#[allow(clippy::type_complexity)]
1818
#[derive(Default, Clone)]
1919
pub struct PySequenceMethods {
20-
pub length: Option<fn(&PyObjectRef, &VirtualMachine) -> PyResult<usize>>,
21-
pub concat: Option<fn(&PyObjectRef, &PyObjectRef, &VirtualMachine) -> PyResult>,
22-
pub repeat: Option<fn(&PyObjectRef, usize, &VirtualMachine) -> PyResult>,
23-
pub item: Option<fn(&PyObjectRef, isize, &VirtualMachine) -> PyResult>,
20+
pub length: Option<fn(&PyObject, &VirtualMachine) -> PyResult<usize>>,
21+
pub concat: Option<fn(&PyObject, &PyObject, &VirtualMachine) -> PyResult>,
22+
pub repeat: Option<fn(&PyObject, usize, &VirtualMachine) -> PyResult>,
23+
pub item: Option<fn(&PyObject, isize, &VirtualMachine) -> PyResult>,
2424
pub ass_item:
25-
Option<fn(&PyObjectRef, isize, Option<PyObjectRef>, &VirtualMachine) -> PyResult<()>>,
26-
pub contains: Option<fn(&PyObjectRef, &PyObjectRef, &VirtualMachine) -> PyResult<bool>>,
27-
pub inplace_concat: Option<fn(&PyObjectRef, &PyObjectRef, &VirtualMachine) -> PyResult>,
28-
pub inplace_repeat: Option<fn(&PyObjectRef, usize, &VirtualMachine) -> PyResult>,
25+
Option<fn(&PyObject, isize, Option<PyObjectRef>, &VirtualMachine) -> PyResult<()>>,
26+
pub contains: Option<fn(&PyObject, &PyObject, &VirtualMachine) -> PyResult<bool>>,
27+
pub inplace_concat: Option<fn(&PyObject, &PyObject, &VirtualMachine) -> PyResult>,
28+
pub inplace_repeat: Option<fn(&PyObject, usize, &VirtualMachine) -> PyResult>,
2929
}
3030

3131
impl PySequenceMethods {
@@ -59,7 +59,7 @@ pub struct PySequence {
5959
}
6060

6161
impl PySequence {
62-
pub fn check(obj: &PyObjectRef, vm: &VirtualMachine) -> bool {
62+
pub fn check(obj: &PyObject, vm: &VirtualMachine) -> bool {
6363
let cls = obj.class();
6464
if cls.is(&vm.ctx.types.dict_type) {
6565
return false;
@@ -99,7 +99,7 @@ impl PySequence {
9999
}
100100
}
101101

102-
pub fn concat(&self, other: &PyObjectRef, vm: &VirtualMachine) -> PyResult {
102+
pub fn concat(&self, other: &PyObject, vm: &VirtualMachine) -> PyResult {
103103
if let Some(f) = self.methods().concat {
104104
return f(&self.obj, other, vm);
105105
}
@@ -113,7 +113,7 @@ impl PySequence {
113113
try_mul_for_repeat(&self.obj, n, vm)
114114
}
115115

116-
pub fn inplace_concat(&self, other: &PyObjectRef, vm: &VirtualMachine) -> PyResult {
116+
pub fn inplace_concat(&self, other: &PyObject, vm: &VirtualMachine) -> PyResult {
117117
if let Some(f) = self.methods().inplace_concat {
118118
return f(&self.obj, other, vm);
119119
}
@@ -251,13 +251,13 @@ impl PySequence {
251251
Ok(list.into())
252252
}
253253

254-
pub fn contains(&self, target: &PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
254+
pub fn contains(&self, target: &PyObject, vm: &VirtualMachine) -> PyResult<bool> {
255255
if let Some(f) = self.methods().contains {
256256
return f(&self.obj, target, vm);
257257
}
258258

259259
let iter = self.obj.clone().get_iter(vm)?;
260-
let iter = iter.iter(vm)?;
260+
let iter = iter.iter::<PyObjectRef>(vm)?;
261261

262262
for elem in iter {
263263
let elem = elem?;
@@ -268,11 +268,11 @@ impl PySequence {
268268
Ok(false)
269269
}
270270

271-
pub fn count(&self, target: &PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
271+
pub fn count(&self, target: &PyObject, vm: &VirtualMachine) -> PyResult<usize> {
272272
let mut n = 0;
273273

274274
let iter = self.obj.clone().get_iter(vm)?;
275-
let iter = iter.iter(vm)?;
275+
let iter = iter.iter::<PyObjectRef>(vm)?;
276276

277277
for elem in iter {
278278
let elem = elem?;
@@ -287,11 +287,11 @@ impl PySequence {
287287
Ok(n)
288288
}
289289

290-
pub fn index(&self, target: &PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
290+
pub fn index(&self, target: &PyObject, vm: &VirtualMachine) -> PyResult<usize> {
291291
let mut index: isize = -1;
292292

293293
let iter = self.obj.clone().get_iter(vm)?;
294-
let iter = iter.iter(vm)?;
294+
let iter = iter.iter::<PyObjectRef>(vm)?;
295295

296296
for elem in iter {
297297
if index == isize::MAX {
@@ -316,7 +316,7 @@ impl TryFromObject for PySequence {
316316
}
317317
}
318318

319-
pub fn try_add_for_concat(a: &PyObjectRef, b: &PyObjectRef, vm: &VirtualMachine) -> PyResult {
319+
pub fn try_add_for_concat(a: &PyObject, b: &PyObject, vm: &VirtualMachine) -> PyResult {
320320
if PySequence::check(b, vm) {
321321
let ret = vm._add(a, b)?;
322322
if let PyArithmeticValue::Implemented(ret) = PyArithmeticValue::from_object(vm, ret) {
@@ -329,19 +329,15 @@ pub fn try_add_for_concat(a: &PyObjectRef, b: &PyObjectRef, vm: &VirtualMachine)
329329
)))
330330
}
331331

332-
pub fn try_mul_for_repeat(a: &PyObjectRef, n: usize, vm: &VirtualMachine) -> PyResult {
332+
pub fn try_mul_for_repeat(a: &PyObject, n: usize, vm: &VirtualMachine) -> PyResult {
333333
let ret = vm._mul(a, &n.into_pyobject(vm))?;
334334
if let PyArithmeticValue::Implemented(ret) = PyArithmeticValue::from_object(vm, ret) {
335335
return Ok(ret);
336336
}
337337
Err(vm.new_type_error(format!("'{}' object can't be repeated", a.class().name())))
338338
}
339339

340-
pub fn try_iadd_for_inplace_concat(
341-
a: &PyObjectRef,
342-
b: &PyObjectRef,
343-
vm: &VirtualMachine,
344-
) -> PyResult {
340+
pub fn try_iadd_for_inplace_concat(a: &PyObject, b: &PyObject, vm: &VirtualMachine) -> PyResult {
345341
if PySequence::check(b, vm) {
346342
let ret = vm._iadd(a, b)?;
347343
if let PyArithmeticValue::Implemented(ret) = PyArithmeticValue::from_object(vm, ret) {
@@ -354,7 +350,7 @@ pub fn try_iadd_for_inplace_concat(
354350
)))
355351
}
356352

357-
pub fn try_imul_for_inplace_repeat(a: &PyObjectRef, n: usize, vm: &VirtualMachine) -> PyResult {
353+
pub fn try_imul_for_inplace_repeat(a: &PyObject, n: usize, vm: &VirtualMachine) -> PyResult {
358354
let ret = vm._imul(a, &n.into_pyobject(vm))?;
359355
if let PyArithmeticValue::Implemented(ret) = PyArithmeticValue::from_object(vm, ret) {
360356
return Ok(ret);

vm/src/types/slot.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,20 @@ fn as_sequence_wrapper(zelf: &PyObject, _vm: &VirtualMachine) -> Cow<'static, Py
205205
vm.obj_len_opt(zelf).unwrap()
206206
}),
207207
item: Some(|zelf, i, vm| {
208-
vm.call_special_method(zelf.clone(), "__getitem__", (i.into_pyobject(vm),))
208+
vm.call_special_method(zelf.to_owned(), "__getitem__", (i.into_pyobject(vm),))
209209
}),
210210
ass_item: then_some_closure!(
211211
zelf.has_class_attr("__setitem__") | zelf.has_class_attr("__delitem__"),
212212
|zelf, i, value, vm| match value {
213213
Some(value) => vm
214-
.call_special_method(zelf.clone(), "__setitem__", (i.into_pyobject(vm), value),)
214+
.call_special_method(
215+
zelf.to_owned(),
216+
"__setitem__",
217+
(i.into_pyobject(vm), value),
218+
)
215219
.map(|_| Ok(()))?,
216220
None => vm
217-
.call_special_method(zelf.clone(), "__delitem__", (i.into_pyobject(vm),))
221+
.call_special_method(zelf.to_owned(), "__delitem__", (i.into_pyobject(vm),))
218222
.map(|_| Ok(()))?,
219223
}
220224
),

0 commit comments

Comments
 (0)