Skip to content

Commit 5a7ea06

Browse files
authored
Merge branch 'main' into relocate-rich_compare
2 parents 7eb600b + 68379da commit 5a7ea06

14 files changed

Lines changed: 34 additions & 31 deletions

File tree

benches/microbenchmarks.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use criterion::{
44
};
55
use rustpython_compiler::Mode;
66
use rustpython_vm::{
7-
common::ascii, InitParameter, Interpreter, ItemProtocol, PyResult, PySettings,
7+
common::ascii, InitParameter, Interpreter, ItemProtocol, PyObjectWrap, PyResult, PySettings,
88
};
99
use std::path::{Path, PathBuf};
1010
use std::{ffi, fs, io};
@@ -132,6 +132,7 @@ fn bench_rustpy_code(group: &mut BenchmarkGroup<WallTime>, bench: &MicroBenchmar
132132
if let Some(idx) = iterations {
133133
scope
134134
.locals
135+
.as_object()
135136
.set_item(vm.new_pyobj(ascii!("ITERATIONS")), vm.new_pyobj(idx), vm)
136137
.expect("Error adding ITERATIONS local variable");
137138
}

derive/src/compile_bytecode.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,16 @@ impl CompilationSource {
185185
Err(e)
186186
});
187187

188-
if code.is_err() && stem.starts_with("badsyntax_") {
189-
continue;
190-
}
188+
let code = match code {
189+
Ok(code) => code,
190+
Err(_) if stem.starts_with("badsyntax_") => continue,
191+
Err(e) => return Err(e),
192+
};
191193

192194
code_map.insert(
193195
module_name,
194196
FrozenModule {
195-
code: code?,
197+
code,
196198
package: is_init,
197199
},
198200
);

stdlib/src/bisect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ mod _bisect {
7272
BisectArgs { a, x, lo, hi }: BisectArgs,
7373
vm: &VirtualMachine,
7474
) -> PyResult<usize> {
75-
let (mut lo, mut hi) = as_usize(lo, hi, vm.obj_len(&a)?, vm)?;
75+
let (mut lo, mut hi) = as_usize(lo, hi, a.length(vm)?, vm)?;
7676

7777
while lo < hi {
7878
// Handles issue 13496.
@@ -100,7 +100,7 @@ mod _bisect {
100100
BisectArgs { a, x, lo, hi }: BisectArgs,
101101
vm: &VirtualMachine,
102102
) -> PyResult<usize> {
103-
let (mut lo, mut hi) = as_usize(lo, hi, vm.obj_len(&a)?, vm)?;
103+
let (mut lo, mut hi) = as_usize(lo, hi, a.length(vm)?, vm)?;
104104

105105
while lo < hi {
106106
// Handles issue 13496.

vm/src/builtins/enumerate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl PyReverseSequenceIterator {
8888
fn length_hint(&self, vm: &VirtualMachine) -> PyResult<usize> {
8989
let internal = self.internal.lock();
9090
if let IterStatus::Active(obj) = &internal.status {
91-
if internal.position <= vm.obj_len(obj)? {
91+
if internal.position <= obj.length(vm)? {
9292
return Ok(internal.position + 1);
9393
}
9494
}

vm/src/builtins/genericalias.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ fn make_parameters(args: &PyTupleRef, vm: &VirtualMachine) -> PyTupleRef {
167167
impl Hashable for PyGenericAlias {
168168
#[inline]
169169
fn hash(zelf: &PyRef<Self>, vm: &VirtualMachine) -> PyResult<hash::PyHash> {
170-
Ok(vm._hash(zelf.origin.as_object())? ^ vm._hash(zelf.args.as_object())?)
170+
Ok(zelf.origin.as_object().hash(vm)? ^ zelf.args.as_object().hash(vm)?)
171171
}
172172
}
173173

vm/src/builtins/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl PySequenceIterator {
181181
fn length_hint(&self, vm: &VirtualMachine) -> PyObjectRef {
182182
let internal = self.internal.lock();
183183
if let IterStatus::Active(obj) = &internal.status {
184-
vm.obj_len(obj)
184+
obj.length(vm)
185185
.map(|x| PyInt::from(x).into_object(vm))
186186
.unwrap_or_else(|_| vm.ctx.not_implemented())
187187
} else {

vm/src/builtins/weakref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl Hashable for PyWeak {
9393
let obj = zelf
9494
.upgrade()
9595
.ok_or_else(|| vm.new_type_error("weak object has gone away".to_owned()))?;
96-
let hash = vm._hash(&obj)?;
96+
let hash = obj.hash(vm)?;
9797
zelf.hash.store(Some(hash));
9898
Ok(hash)
9999
}

vm/src/dictdatatype.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ pub trait DictKey: IntoPyObject {
652652
/// to index dictionaries.
653653
impl DictKey for PyObjectRef {
654654
fn key_hash(&self, vm: &VirtualMachine) -> PyResult<HashValue> {
655-
vm._hash(self)
655+
self.hash(vm)
656656
}
657657

658658
fn key_is(&self, other: &PyObjectRef) -> bool {

vm/src/protocol/object.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ impl PyObjectRef {
190190
}
191191

192192
pub fn hash(&self, vm: &VirtualMachine) -> PyResult<PyHash> {
193-
vm._hash(self)
193+
let hash = self
194+
.class()
195+
.mro_find_map(|cls| cls.slots.hash.load())
196+
.unwrap(); // hash always exist
197+
hash(self, vm)
194198
}
195199

196200
// const hash_not_implemented: fn(&PyObjectRef, &VirtualMachine) ->PyResult<PyHash> = crate::types::Unhashable::slot_hash;
@@ -209,7 +213,12 @@ impl PyObjectRef {
209213
// int PyObject_TypeCheck(PyObject *o, PyTypeObject *type)
210214

211215
pub fn length(&self, vm: &VirtualMachine) -> PyResult<usize> {
212-
vm.obj_len(self)
216+
vm.obj_len_opt(self).unwrap_or_else(|| {
217+
Err(vm.new_type_error(format!(
218+
"object of type '{}' has no len()",
219+
self.class().name()
220+
)))
221+
})
213222
}
214223

215224
pub fn length_hint(

vm/src/stdlib/builtins.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ mod builtins {
337337

338338
#[pyfunction]
339339
fn hash(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyHash> {
340-
vm._hash(&obj)
340+
obj.hash(vm)
341341
}
342342

343343
// builtin_help
@@ -424,7 +424,7 @@ mod builtins {
424424

425425
#[pyfunction]
426426
fn len(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
427-
vm.obj_len(&obj)
427+
obj.length(vm)
428428
}
429429

430430
#[pyfunction]
@@ -690,7 +690,7 @@ mod builtins {
690690
vm.get_method_or_type_error(obj.clone(), "__getitem__", || {
691691
"argument to reversed() must be a sequence".to_owned()
692692
})?;
693-
let len = vm.obj_len(&obj)?;
693+
let len = obj.length(vm)?;
694694
let obj_iterator = PyReverseSequenceIterator::new(obj, len);
695695
Ok(obj_iterator.into_object(vm))
696696
}

0 commit comments

Comments
 (0)