Skip to content
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
14 changes: 5 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use rustpython_vm::{
compile, match_class,
scope::Scope,
stdlib::{atexit, sys},
InitParameter, Interpreter, PyObjectRef, PyResult, PySettings, TryFromObject, TypeProtocol,
AsPyObject, InitParameter, Interpreter, PyObjectRef, PyResult, PySettings, TryFromObject,
VirtualMachine,
};
use std::{env, path::Path, process, str::FromStr};
Expand Down Expand Up @@ -106,7 +106,7 @@ where
// See if any exception leaked out:
let exitcode = match res {
Ok(()) => 0,
Err(err) if err.isinstance(&vm.ctx.exceptions.system_exit) => {
Err(err) if err.fast_isinstance(&vm.ctx.exceptions.system_exit) => {
let args = err.args();
match args.as_slice() {
[] => 0,
Expand Down Expand Up @@ -536,12 +536,8 @@ fn setup_main_module(vm: &VirtualMachine) -> PyResult<Scope> {
main_module
.dict()
.and_then(|d| {
d.set_item(
"__annotations__",
vm.ctx.new_dict().as_object().to_owned(),
vm,
)
.ok()
d.set_item("__annotations__", vm.ctx.new_dict().into(), vm)
.ok()
})
.expect("Failed to initialize __main__.__annotations__");

Expand Down Expand Up @@ -656,7 +652,7 @@ fn get_importer(path: &str, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>
importer = Some(imp);
break;
}
Err(e) if e.isinstance(&vm.ctx.exceptions.import_error) => continue,
Err(e) if e.fast_isinstance(&vm.ctx.exceptions.import_error) => continue,
Err(e) => return Err(e),
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustpython_vm::{
builtins::PyBaseExceptionRef,
compile::{self, CompileError, CompileErrorType},
scope::Scope,
PyResult, TypeProtocol, VirtualMachine,
AsPyObject, PyResult, VirtualMachine,
};

enum ShellExecResult {
Expand Down Expand Up @@ -127,7 +127,7 @@ pub fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
};

if let Err(exc) = result {
if exc.isinstance(&vm.ctx.exceptions.system_exit) {
if exc.fast_isinstance(&vm.ctx.exceptions.system_exit) {
repl.save_history(&repl_history_path).unwrap();
return Err(exc);
}
Expand Down
5 changes: 2 additions & 3 deletions src/shell/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ impl<'vm> ShellHelper<'vm> {
} else {
// we need to get a variable based off of globals/builtins

let globals = str_iter_method(self.globals.as_object().to_owned(), "keys").ok()?;
let builtins =
str_iter_method(self.vm.builtins.as_object().to_owned(), "__dir__").ok()?;
let globals = str_iter_method(self.globals.clone().into(), "keys").ok()?;
let builtins = str_iter_method(self.vm.builtins.clone().into(), "__dir__").ok()?;
(first, globals, Some(builtins))
};
Some((word_start, iter1.chain(iter2.into_iter().flatten())))
Expand Down
13 changes: 6 additions & 7 deletions stdlib/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ mod array {
AsBuffer, AsMapping, Comparable, Constructor, IterNext, IterNextIterable, Iterable,
PyComparisonOp,
},
IdProtocol, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine,
AsPyObject, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
TryFromBorrowedObject, TryFromObject, VirtualMachine,
},
};
use itertools::Itertools;
use num_traits::ToPrimitive;
use std::cmp::Ordering;
use std::{fmt, os::raw};
use std::{cmp::Ordering, fmt, os::raw};

macro_rules! def_array_enum {
($(($n:ident, $t:ty, $c:literal, $scode:literal)),*$(,)?) => {
Expand Down Expand Up @@ -1116,7 +1115,7 @@ mod array {
return Self::reduce(zelf, vm);
}
let array = zelf.read();
let cls = zelf.as_object().clone_class();
let cls = zelf.class().clone();
let typecode = vm.ctx.new_str(array.typecode_str());
let bytes = vm.ctx.new_bytes(array.get_bytes().to_vec());
let code = MachineFormatCode::from_typecode(array.typecode()).unwrap();
Expand All @@ -1136,7 +1135,7 @@ mod array {
vm: &VirtualMachine,
) -> PyResult<(PyObjectRef, PyTupleRef, Option<PyDictRef>)> {
let array = zelf.read();
let cls = zelf.as_object().clone_class();
let cls = zelf.class().clone();
let typecode = vm.ctx.new_str(array.typecode_str());
let values = if array.typecode() == 'u' {
let s = Self::_wchar_bytes_to_string(array.get_bytes(), array.itemsize(), vm)?;
Expand Down Expand Up @@ -1445,7 +1444,7 @@ mod array {
}

fn check_array_type(typ: PyTypeRef, vm: &VirtualMachine) -> PyResult<PyTypeRef> {
if !typ.issubclass(PyArray::class(vm)) {
if !typ.fast_issubclass(PyArray::class(vm)) {
return Err(
vm.new_type_error(format!("{} is not a subtype of array.array", typ.name()))
);
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod _csv {
match_class,
protocol::{PyIter, PyIterReturn},
types::{IterNext, IterNextIterable},
PyObjectRef, PyObjectView, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine,
AsPyObject, PyObjectRef, PyObjectView, PyResult, PyValue, TryFromObject, VirtualMachine,
};
use itertools::{self, Itertools};
use std::fmt;
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod _json {
function::{IntoPyObject, IntoPyResult, OptionalArg},
protocol::PyIterReturn,
types::{Callable, Constructor},
IdProtocol, PyObjectRef, PyObjectView, PyResult, PyValue, VirtualMachine,
AsPyObject, PyObjectRef, PyObjectView, PyResult, PyValue, VirtualMachine,
};
use num_bigint::BigInt;
use std::str::FromStr;
Expand Down Expand Up @@ -246,7 +246,7 @@ mod _json {
) -> PyBaseExceptionRef {
let get_error = || -> PyResult<_> {
let cls = vm.try_class("json", "JSONDecodeError")?;
let exc = vm.invoke(cls.as_object(), (e.msg, s, e.pos))?;
let exc = vm.invoke(&cls, (e.msg, s, e.pos))?;
exc.try_into_value(vm)
};
match get_error() {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod math {
builtins::{try_bigint_to_f64, try_f64_to_bigint, PyFloat, PyInt, PyIntRef},
function::{ArgIntoFloat, ArgIterable, OptionalArg, PosArgs},
utils::Either,
PyObject, PyObjectRef, PyRef, PyResult, TypeProtocol, VirtualMachine,
AsPyObject, PyObject, PyObjectRef, PyRef, PyResult, VirtualMachine,
};
use num_bigint::BigInt;
use num_traits::{One, Signed, Zero};
Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/pystruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub(crate) mod _struct {
match_class,
protocol::PyIterReturn,
types::{Constructor, IterNext, IterNextIterable},
PyObjectRef, PyObjectView, PyResult, PyValue, TryFromObject, TypeProtocol, VirtualMachine,
AsPyObject, PyObjectRef, PyObjectView, PyResult, PyValue, TryFromObject, VirtualMachine,
};
use crossbeam_utils::atomic::AtomicCell;

Expand Down
2 changes: 1 addition & 1 deletion stdlib/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ mod decl {
common::lock::PyMutex,
function::{IntoPyObject, OptionalArg},
stdlib::io::Fildes,
PyValue, TypeProtocol,
AsPyObject, PyValue,
};
use libc::pollfd;
use num_traits::ToPrimitive;
Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod _socket {
OptionalOption,
},
utils::{Either, ToCString},
PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol,
AsPyObject, PyObjectRef, PyResult, PyValue, TryFromBorrowedObject, TryFromObject,
VirtualMachine,
};
use crossbeam_utils::atomic::AtomicCell;
Expand Down Expand Up @@ -154,7 +154,7 @@ mod _socket {
type CastFrom = libc::c_longlong;

// should really just be to_index() but test_socket tests the error messages explicitly
if obj.isinstance(&vm.ctx.types.float_type) {
if obj.fast_isinstance(&vm.ctx.types.float_type) {
return Err(vm.new_type_error("integer argument expected, got float".to_owned()));
}
let int = vm
Expand Down
2 changes: 1 addition & 1 deletion vm/src/anystr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
cformat::CFormatString,
function::{single_or_tuple_any, OptionalOption},
protocol::PyIterIter,
PyObjectRef, PyResult, TryFromObject, TypeProtocol, VirtualMachine,
AsPyObject, PyObjectRef, PyResult, TryFromObject, VirtualMachine,
};
use num_traits::{cast::ToPrimitive, sign::Signed};
use std::str::FromStr;
Expand Down
8 changes: 4 additions & 4 deletions vm/src/builtins/asyncgenerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
protocol::PyIterReturn,
pyclass::PyClassImpl,
types::{Constructor, IterNext, IterNextIterable, Unconstructible},
IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
};

use crossbeam_utils::atomic::AtomicCell;
Expand Down Expand Up @@ -147,7 +147,7 @@ impl PyAsyncGenWrappedValue {
fn unbox(ag: &PyAsyncGen, val: PyResult<PyIterReturn>, vm: &VirtualMachine) -> PyResult {
let (closed, async_done) = match &val {
Ok(PyIterReturn::StopIteration(_)) => (true, true),
Err(e) if e.isinstance(&vm.ctx.exceptions.generator_exit) => (true, true),
Err(e) if e.fast_isinstance(&vm.ctx.exceptions.generator_exit) => (true, true),
Err(_) => (false, true),
_ => (false, false),
};
Expand Down Expand Up @@ -398,8 +398,8 @@ impl PyAsyncGenAThrow {
self.ag.running_async.store(false);
self.state.store(AwaitableState::Closed);
if self.aclose
&& (exc.isinstance(&vm.ctx.exceptions.stop_async_iteration)
|| exc.isinstance(&vm.ctx.exceptions.generator_exit))
&& (exc.fast_isinstance(&vm.ctx.exceptions.stop_async_iteration)
|| exc.fast_isinstance(&vm.ctx.exceptions.generator_exit))
{
vm.new_stop_iteration(None)
} else {
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/builtinfunc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
function::{FuncArgs, IntoPyNativeFunc, PyNativeFunc},
pyclass::PyClassImpl,
types::{Callable, Constructor, GetDescriptor, Unconstructible},
PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
};
use std::fmt;

Expand Down
8 changes: 4 additions & 4 deletions vm/src/builtins/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use crate::{
IterNextIterable, Iterable, PyComparisonOp, Unconstructible, Unhashable,
},
utils::Either,
IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine,
AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
PyValue, TryFromBorrowedObject, TryFromObject, VirtualMachine,
};
use bstr::ByteSlice;
use std::{borrow::Cow, mem::size_of};
Expand Down Expand Up @@ -688,7 +688,7 @@ impl PyByteArray {
) -> (PyTypeRef, PyTupleRef, Option<PyDictRef>) {
let bytes = PyBytes::from(zelf.borrow_buf().to_vec()).into_pyobject(vm);
(
zelf.as_object().clone_class(),
zelf.class().clone(),
PyTuple::new_ref(vec![bytes], &vm.ctx),
zelf.as_object().dict(),
)
Expand Down Expand Up @@ -719,7 +719,7 @@ impl Comparable for PyByteArray {
op: PyComparisonOp,
vm: &VirtualMachine,
) -> PyResult<PyComparisonValue> {
if let Some(res) = op.identical_optimization(&zelf, &other) {
if let Some(res) = op.identical_optimization(zelf, other) {
return Ok(res.into());
}
Ok(zelf.inner().cmp(other, op, vm))
Expand Down
8 changes: 4 additions & 4 deletions vm/src/builtins/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use crate::{
IterNextIterable, Iterable, PyComparisonOp, Unconstructible,
},
utils::Either,
IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
PyValue, TryFromBorrowedObject, TryFromObject, TypeProtocol, VirtualMachine,
AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyObjectWrap, PyRef, PyResult,
PyValue, TryFromBorrowedObject, TryFromObject, VirtualMachine,
};
use bstr::ByteSlice;
use std::{borrow::Cow, mem::size_of, ops::Deref};
Expand Down Expand Up @@ -544,7 +544,7 @@ impl PyBytes {
) -> (PyTypeRef, PyTupleRef, Option<PyDictRef>) {
let bytes = PyBytes::from(zelf.inner.elements.clone()).into_pyobject(vm);
(
zelf.as_object().clone_class(),
zelf.class().clone(),
PyTuple::new_ref(vec![bytes], &vm.ctx),
zelf.as_object().dict(),
)
Expand Down Expand Up @@ -638,7 +638,7 @@ impl Comparable for PyBytes {
) -> PyResult<PyComparisonValue> {
Ok(if let Some(res) = op.identical_optimization(zelf, other) {
res.into()
} else if other.isinstance(&vm.ctx.types.memoryview_type)
} else if other.fast_isinstance(&vm.ctx.types.memoryview_type)
&& op != PyComparisonOp::Eq
&& op != PyComparisonOp::Ne
{
Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/classmethod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
builtins::PyBoundMethod,
pyclass::PyClassImpl,
types::{Constructor, GetDescriptor},
PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
};

/// classmethod(function) -> method
Expand Down Expand Up @@ -52,7 +52,7 @@ impl GetDescriptor for PyClassMethod {
vm: &VirtualMachine,
) -> PyResult {
let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?;
let cls = cls.unwrap_or_else(|| obj.clone_class().into());
let cls = cls.unwrap_or_else(|| obj.class().clone().into());
Ok(PyBoundMethod::new_ref(cls, zelf.callable.clone(), &vm.ctx).into())
}
}
Expand Down
3 changes: 1 addition & 2 deletions vm/src/builtins/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use crate::{
bytecode::{self, BorrowedConstant, Constant, ConstantBag},
function::FuncArgs,
pyclass::{PyClassImpl, StaticType},
IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
VirtualMachine,
AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
};
use num_traits::Zero;
use std::{fmt, ops::Deref};
Expand Down
5 changes: 2 additions & 3 deletions vm/src/builtins/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use crate::{
},
pyclass::PyClassImpl,
types::{Comparable, Constructor, Hashable, PyComparisonOp},
IdProtocol, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol,
VirtualMachine,
AsPyObject, PyContext, PyObject, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
};
use num_complex::Complex64;
use num_traits::Zero;
Expand Down Expand Up @@ -159,7 +158,7 @@ impl Constructor for PyComplex {
OptionalArg::Present(obj) => {
if let Some(c) = obj.try_complex(vm)? {
c
} else if obj.class().issubclass(&vm.ctx.types.str_type) {
} else if obj.class().fast_issubclass(&vm.ctx.types.str_type) {
return Err(
vm.new_type_error("complex() second arg can't be a string".to_owned())
);
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
protocol::PyIterReturn,
pyclass::PyClassImpl,
types::{Constructor, IterNext, IterNextIterable, Unconstructible},
IdProtocol, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
};

#[pyclass(module = false, name = "coroutine")]
Expand Down
8 changes: 5 additions & 3 deletions vm/src/builtins/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use crate::{
IterNextIterable, Iterable, PyComparisonOp, Unconstructible, Unhashable,
},
vm::{ReprGuard, VirtualMachine},
IdProtocol, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
TryFromObject, TypeProtocol,
AsPyObject, PyContext, PyObject, PyObjectRef, PyObjectView, PyRef, PyResult, PyValue,
TryFromObject,
};
use rustpython_common::lock::PyMutex;
use std::{borrow::Cow, fmt};
Expand Down Expand Up @@ -558,7 +558,9 @@ impl PyObjectView<PyDict> {
} else {
match self.as_object().get_item(key.clone(), vm) {
Ok(value) => Ok(Some(value)),
Err(e) if e.isinstance(&vm.ctx.exceptions.key_error) => self.missing_opt(key, vm),
Err(e) if e.fast_isinstance(&vm.ctx.exceptions.key_error) => {
self.missing_opt(key, vm)
}
Err(e) => Err(e),
}
}
Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
protocol::{PyIter, PyIterReturn},
pyclass::PyClassImpl,
types::{Constructor, IterNext, IterNextIterable},
PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
AsPyObject, PyContext, PyObjectRef, PyRef, PyResult, PyValue, VirtualMachine,
};
use num_bigint::BigInt;
use num_traits::Zero;
Expand Down Expand Up @@ -56,7 +56,7 @@ impl PyEnumerate {
#[pymethod(magic)]
fn reduce(zelf: PyRef<Self>) -> (PyTypeRef, (PyIter, BigInt)) {
(
zelf.clone_class(),
zelf.class().clone(),
(zelf.iterator.clone(), zelf.counter.read().clone()),
)
}
Expand Down
Loading