mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
remove vm.get_none and clean up its use cases
This commit is contained in:
1 parent
3d908dc754
commit
0f7ed21eda
42 files changed
+194
-255
No files matched your search
+11
-24
@@ -1,14 +1,14 @@
|
||||
use crate::common::cell::PyRwLock;
|
||||
use crate::function::PyFuncArgs;
|
||||
use crate::obj::objsingletons::PyNone;
|
||||
use crate::obj::objsingletons::{PyNone, PyNoneRef};
|
||||
use crate::obj::objstr::{PyString, PyStringRef};
|
||||
use crate::obj::objtraceback::PyTracebackRef;
|
||||
use crate::obj::objtuple::{PyTuple, PyTupleRef};
|
||||
use crate::obj::objtype::{self, PyClass, PyClassRef};
|
||||
use crate::py_io::{self, Write};
|
||||
use crate::pyobject::{
|
||||
BorrowValue, PyClassDef, PyClassImpl, PyContext, PyIterable, PyObjectRef, PyRef, PyResult,
|
||||
PyValue, TryFromObject, TypeProtocol,
|
||||
BorrowValue, IntoPyObject, PyClassDef, PyClassImpl, PyContext, PyIterable, PyObjectRef, PyRef,
|
||||
PyResult, PyValue, TryFromObject, TypeProtocol,
|
||||
};
|
||||
use crate::types::create_type_with_slots;
|
||||
use crate::VirtualMachine;
|
||||
@@ -371,7 +371,7 @@ pub fn split(
|
||||
exc: PyBaseExceptionRef,
|
||||
vm: &VirtualMachine,
|
||||
) -> (PyObjectRef, PyObjectRef, PyObjectRef) {
|
||||
let tb = exc.traceback().map_or(vm.get_none(), |tb| tb.into_object());
|
||||
let tb = exc.traceback().into_pyobject(vm);
|
||||
(exc.class().into_object(), exc.into_object(), tb)
|
||||
}
|
||||
|
||||
@@ -626,35 +626,22 @@ fn import_error_init(exc_self: PyObjectRef, args: PyFuncArgs, vm: &VirtualMachin
|
||||
vm.set_attr(
|
||||
&exc_self,
|
||||
"name",
|
||||
args.kwargs
|
||||
.get("name")
|
||||
.cloned()
|
||||
.unwrap_or_else(|| vm.get_none()),
|
||||
vm.unwrap_or_none(args.kwargs.get("name").cloned()),
|
||||
)?;
|
||||
vm.set_attr(
|
||||
&exc_self,
|
||||
"path",
|
||||
args.kwargs
|
||||
.get("path")
|
||||
.cloned()
|
||||
.unwrap_or_else(|| vm.get_none()),
|
||||
vm.unwrap_or_none(args.kwargs.get("path").cloned()),
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn none_getter(_obj: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
|
||||
vm.get_none()
|
||||
fn none_getter(_obj: PyObjectRef, vm: &VirtualMachine) -> PyNoneRef {
|
||||
vm.ctx.none.clone()
|
||||
}
|
||||
|
||||
fn make_arg_getter(idx: usize) -> impl Fn(PyBaseExceptionRef, &VirtualMachine) -> PyObjectRef {
|
||||
move |exc, vm| {
|
||||
exc.args
|
||||
.read()
|
||||
.borrow_value()
|
||||
.get(idx)
|
||||
.cloned()
|
||||
.unwrap_or_else(|| vm.get_none())
|
||||
}
|
||||
fn make_arg_getter(idx: usize) -> impl Fn(PyBaseExceptionRef) -> Option<PyObjectRef> {
|
||||
move |exc| exc.args.read().borrow_value().get(idx).cloned()
|
||||
}
|
||||
|
||||
fn key_error_str(exc: PyBaseExceptionRef, vm: &VirtualMachine) -> PyStringRef {
|
||||
@@ -678,7 +665,7 @@ fn system_exit_code(exc: PyBaseExceptionRef, vm: &VirtualMachine) -> PyObjectRef
|
||||
},
|
||||
other => other.clone(),
|
||||
}),
|
||||
None => vm.get_none(),
|
||||
None => vm.ctx.none(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-13
@@ -170,7 +170,7 @@ impl Frame {
|
||||
stack: Vec::new(),
|
||||
blocks: Vec::new(),
|
||||
}),
|
||||
trace: PyMutex::new(vm.get_none()),
|
||||
trace: PyMutex::new(vm.ctx.none()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -540,14 +540,11 @@ impl ExecutingFrame<'_> {
|
||||
let exit = self.pop_value();
|
||||
|
||||
let args = if let Some(exc) = exc {
|
||||
let exc_type = exc.class().into_object();
|
||||
let exc_val = exc.clone();
|
||||
let exc_tb = exc.traceback().map_or(vm.get_none(), |tb| tb.into_object());
|
||||
vec![exc_type, exc_val.into_object(), exc_tb]
|
||||
exceptions::split(exc, vm)
|
||||
} else {
|
||||
vec![vm.ctx.none(), vm.ctx.none(), vm.ctx.none()]
|
||||
(vm.ctx.none(), vm.ctx.none(), vm.ctx.none())
|
||||
};
|
||||
let exit_res = vm.invoke(&exit, args)?;
|
||||
let exit_res = vm.invoke(&exit, vec![args.0, args.1, args.2])?;
|
||||
self.push_value(exit_res);
|
||||
|
||||
Ok(None)
|
||||
@@ -1287,7 +1284,7 @@ impl ExecutingFrame<'_> {
|
||||
.ctx
|
||||
.new_pyfunction(code_obj, scope, defaults, kw_only_defaults);
|
||||
|
||||
vm.set_attr(&func_obj, "__doc__", vm.get_none())?;
|
||||
vm.set_attr(&func_obj, "__doc__", vm.ctx.none())?;
|
||||
|
||||
let name = qualified_name
|
||||
.borrow_value()
|
||||
@@ -1296,11 +1293,7 @@ impl ExecutingFrame<'_> {
|
||||
.unwrap();
|
||||
vm.set_attr(&func_obj, "__name__", vm.ctx.new_str(name))?;
|
||||
vm.set_attr(&func_obj, "__qualname__", qualified_name)?;
|
||||
let module = self
|
||||
.scope
|
||||
.globals
|
||||
.get_item_option("__name__", vm)?
|
||||
.unwrap_or_else(|| vm.get_none());
|
||||
let module = vm.unwrap_or_none(self.scope.globals.get_item_option("__name__", vm)?);
|
||||
vm.set_attr(&func_obj, "__module__", module)?;
|
||||
vm.set_attr(&func_obj, "__annotations__", annotations)?;
|
||||
|
||||
|
||||
@@ -394,6 +394,12 @@ pub enum OptionalArg<T = PyObjectRef> {
|
||||
|
||||
impl_option_like!(OptionalArg, Present, Missing);
|
||||
|
||||
impl OptionalArg<PyObjectRef> {
|
||||
pub fn unwrap_or_none(self, vm: &VirtualMachine) -> PyObjectRef {
|
||||
self.unwrap_or_else(|| vm.ctx.none())
|
||||
}
|
||||
}
|
||||
|
||||
pub type OptionalOption<T> = OptionalArg<Option<T>>;
|
||||
|
||||
impl<T> OptionalOption<T> {
|
||||
|
||||
@@ -39,9 +39,7 @@ impl PyAsyncGen {
|
||||
|
||||
// TODO: fix function names situation
|
||||
#[pyproperty(magic)]
|
||||
fn name(&self, vm: &VirtualMachine) -> PyObjectRef {
|
||||
vm.get_none()
|
||||
}
|
||||
fn name(&self) {}
|
||||
|
||||
#[pymethod(name = "__aiter__")]
|
||||
fn aiter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
|
||||
@@ -50,7 +48,7 @@ impl PyAsyncGen {
|
||||
|
||||
#[pymethod(name = "__anext__")]
|
||||
fn anext(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyAsyncGenASend {
|
||||
Self::asend(zelf, vm.get_none(), vm)
|
||||
Self::asend(zelf, vm.ctx.none(), vm)
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
@@ -76,8 +74,8 @@ impl PyAsyncGen {
|
||||
state: AtomicCell::new(AwaitableState::Init),
|
||||
value: (
|
||||
exc_type,
|
||||
exc_val.unwrap_or_else(|| vm.get_none()),
|
||||
exc_tb.unwrap_or_else(|| vm.get_none()),
|
||||
exc_val.unwrap_or_none(vm),
|
||||
exc_tb.unwrap_or_none(vm),
|
||||
),
|
||||
}
|
||||
}
|
||||
@@ -90,8 +88,8 @@ impl PyAsyncGen {
|
||||
state: AtomicCell::new(AwaitableState::Init),
|
||||
value: (
|
||||
vm.ctx.exceptions.generator_exit.clone().into_object(),
|
||||
vm.get_none(),
|
||||
vm.get_none(),
|
||||
vm.ctx.none(),
|
||||
vm.ctx.none(),
|
||||
),
|
||||
}
|
||||
}
|
||||
@@ -185,7 +183,7 @@ impl PyAsyncGenASend {
|
||||
|
||||
#[pymethod(name = "__next__")]
|
||||
fn next(&self, vm: &VirtualMachine) -> PyResult {
|
||||
self.send(vm.get_none(), vm)
|
||||
self.send(vm.ctx.none(), vm)
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
@@ -236,8 +234,8 @@ impl PyAsyncGenASend {
|
||||
|
||||
let res = self.ag.inner.throw(
|
||||
exc_type,
|
||||
exc_val.unwrap_or_else(|| vm.get_none()),
|
||||
exc_tb.unwrap_or_else(|| vm.get_none()),
|
||||
exc_val.unwrap_or_none(vm),
|
||||
exc_tb.unwrap_or_none(vm),
|
||||
vm,
|
||||
);
|
||||
let res = PyAsyncGenWrappedValue::unbox(&self.ag, res, vm);
|
||||
@@ -281,7 +279,7 @@ impl PyAsyncGenAThrow {
|
||||
|
||||
#[pymethod(name = "__next__")]
|
||||
fn next(&self, vm: &VirtualMachine) -> PyResult {
|
||||
self.send(vm.get_none(), vm)
|
||||
self.send(vm.ctx.none(), vm)
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
@@ -353,8 +351,8 @@ impl PyAsyncGenAThrow {
|
||||
) -> PyResult {
|
||||
let ret = self.ag.inner.throw(
|
||||
exc_type,
|
||||
exc_val.unwrap_or_else(|| vm.get_none()),
|
||||
exc_tb.unwrap_or_else(|| vm.get_none()),
|
||||
exc_val.unwrap_or_none(vm),
|
||||
exc_tb.unwrap_or_none(vm),
|
||||
vm,
|
||||
);
|
||||
let res = if self.aclose {
|
||||
|
||||
@@ -3,9 +3,7 @@ use std::fmt;
|
||||
use crate::function::{OptionalArg, PyFuncArgs, PyNativeFunc};
|
||||
use crate::obj::objstr::PyStringRef;
|
||||
use crate::obj::objtype::PyClassRef;
|
||||
use crate::pyobject::{
|
||||
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyResult, PyValue, TypeProtocol,
|
||||
};
|
||||
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyResult, PyValue, TypeProtocol};
|
||||
use crate::slots::{SlotCall, SlotDescriptor};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
@@ -118,7 +116,7 @@ impl SlotDescriptor for PyBuiltinMethod {
|
||||
Ok(obj) => obj,
|
||||
Err(result) => return result,
|
||||
};
|
||||
if obj.is(&vm.get_none()) && !Self::_cls_is(&cls, &obj.class()) {
|
||||
if vm.is_none(&obj) && !Self::_cls_is(&cls, &obj.class()) {
|
||||
Ok(zelf.into_object())
|
||||
} else {
|
||||
Ok(vm.ctx.new_bound_method(zelf.into_object(), obj))
|
||||
|
||||
@@ -139,8 +139,8 @@ impl Coro {
|
||||
f.gen_throw(
|
||||
vm,
|
||||
vm.ctx.exceptions.generator_exit.clone().into_object(),
|
||||
vm.get_none(),
|
||||
vm.get_none(),
|
||||
vm.ctx.none(),
|
||||
vm.ctx.none(),
|
||||
)
|
||||
});
|
||||
self.closed.store(true);
|
||||
|
||||
@@ -37,7 +37,7 @@ impl PyCoroutine {
|
||||
// TODO: fix function names situation
|
||||
#[pyproperty(magic)]
|
||||
fn name(&self, vm: &VirtualMachine) -> PyObjectRef {
|
||||
vm.get_none()
|
||||
vm.ctx.none()
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
@@ -55,8 +55,8 @@ impl PyCoroutine {
|
||||
) -> PyResult {
|
||||
self.inner.throw(
|
||||
exc_type,
|
||||
exc_val.unwrap_or_else(|| vm.get_none()),
|
||||
exc_tb.unwrap_or_else(|| vm.get_none()),
|
||||
exc_val.unwrap_or_none(vm),
|
||||
exc_tb.unwrap_or_none(vm),
|
||||
vm,
|
||||
)
|
||||
}
|
||||
@@ -116,7 +116,7 @@ impl PyCoroutineWrapper {
|
||||
|
||||
#[pymethod(name = "__next__")]
|
||||
fn next(&self, vm: &VirtualMachine) -> PyResult {
|
||||
self.coro.send(vm.get_none(), vm)
|
||||
self.coro.send(vm.ctx.none(), vm)
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
|
||||
@@ -136,7 +136,7 @@ impl PyDict {
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<PyRef<Self>> {
|
||||
let dict = DictContentType::default();
|
||||
let value = value.unwrap_or_else(|| vm.ctx.none());
|
||||
let value = value.unwrap_or_none(vm);
|
||||
for elem in iterable.iter(vm)? {
|
||||
let elem = elem?;
|
||||
dict.insert(vm, elem, value.clone())?;
|
||||
@@ -314,7 +314,7 @@ impl PyDict {
|
||||
) -> PyResult {
|
||||
match self.entries.get(vm, &key)? {
|
||||
Some(value) => Ok(value),
|
||||
None => Ok(default.unwrap_or_else(|| vm.ctx.none())),
|
||||
None => Ok(default.unwrap_or_none(vm)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -328,7 +328,7 @@ impl PyDict {
|
||||
match self.entries.get(vm, &key)? {
|
||||
Some(value) => Ok(value),
|
||||
None => {
|
||||
let set_value = default.unwrap_or_else(|| vm.ctx.none());
|
||||
let set_value = default.unwrap_or_none(vm);
|
||||
self.entries.insert(vm, key, set_value.clone())?;
|
||||
Ok(set_value)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::objbool;
|
||||
use super::objiter;
|
||||
use super::objtype::PyClassRef;
|
||||
use crate::pyobject::{IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
|
||||
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
pub type PyFilterRef = PyRef<PyFilter>;
|
||||
@@ -47,7 +47,7 @@ impl PyFilter {
|
||||
let iterator = &self.iterator;
|
||||
loop {
|
||||
let next_obj = objiter::call_next(vm, iterator)?;
|
||||
let predicate_value = if predicate.is(&vm.get_none()) {
|
||||
let predicate_value = if vm.is_none(predicate) {
|
||||
next_obj.clone()
|
||||
} else {
|
||||
// the predicate itself can raise StopIteration which does stop the filter
|
||||
|
||||
@@ -30,7 +30,7 @@ impl FrameRef {
|
||||
// CPython' Frame.f_trace is set to None when deleted.
|
||||
// The strange behavior is mimicked here make bdb.py happy about it.
|
||||
if value.to_string() == "f_trace" {
|
||||
self.set_f_trace(vm.get_none());
|
||||
self.set_f_trace(vm.ctx.none());
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -15,8 +15,8 @@ use crate::obj::objgenerator::PyGenerator;
|
||||
#[cfg(feature = "jit")]
|
||||
use crate::pyobject::IntoPyObject;
|
||||
use crate::pyobject::{
|
||||
BorrowValue, IdProtocol, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult,
|
||||
PyValue, TypeProtocol,
|
||||
BorrowValue, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
|
||||
TypeProtocol,
|
||||
};
|
||||
use crate::scope::Scope;
|
||||
use crate::slots::{SlotCall, SlotDescriptor};
|
||||
@@ -48,7 +48,7 @@ impl SlotDescriptor for PyFunction {
|
||||
cls: OptionalArg<PyObjectRef>,
|
||||
) -> PyResult {
|
||||
let (zelf, obj) = Self::_unwrap(zelf, obj, vm)?;
|
||||
if obj.is(&vm.get_none()) && !Self::_cls_is(&cls, &obj.class()) {
|
||||
if vm.is_none(&obj) && !Self::_cls_is(&cls, &obj.class()) {
|
||||
Ok(zelf.into_object())
|
||||
} else {
|
||||
Ok(vm.ctx.new_bound_method(zelf.into_object(), obj))
|
||||
|
||||
@@ -40,7 +40,7 @@ impl PyGenerator {
|
||||
// TODO: fix function names situation
|
||||
#[pyproperty(magic)]
|
||||
fn name(&self, vm: &VirtualMachine) -> PyObjectRef {
|
||||
vm.get_none()
|
||||
vm.ctx.none()
|
||||
}
|
||||
|
||||
#[pymethod(name = "__iter__")]
|
||||
@@ -50,7 +50,7 @@ impl PyGenerator {
|
||||
|
||||
#[pymethod(name = "__next__")]
|
||||
fn next(&self, vm: &VirtualMachine) -> PyResult {
|
||||
self.send(vm.get_none(), vm)
|
||||
self.send(vm.ctx.none(), vm)
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
@@ -68,8 +68,8 @@ impl PyGenerator {
|
||||
) -> PyResult {
|
||||
self.inner.throw(
|
||||
exc_type,
|
||||
exc_val.unwrap_or_else(|| vm.get_none()),
|
||||
exc_tb.unwrap_or_else(|| vm.get_none()),
|
||||
exc_val.unwrap_or_none(vm),
|
||||
exc_tb.unwrap_or_none(vm),
|
||||
vm,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -494,7 +494,7 @@ impl PyInt {
|
||||
match precision {
|
||||
OptionalArg::Missing => (),
|
||||
OptionalArg::Present(ref value) => {
|
||||
if !vm.get_none().is(value) {
|
||||
if !vm.is_none(value) {
|
||||
// Only accept int type ndigits
|
||||
let _ndigits = value.payload_if_subclass::<PyInt>(vm).ok_or_else(|| {
|
||||
vm.new_type_error(format!(
|
||||
|
||||
@@ -98,11 +98,7 @@ pub fn stop_iter_with_value(val: PyObjectRef, vm: &VirtualMachine) -> PyBaseExce
|
||||
|
||||
pub fn stop_iter_value(vm: &VirtualMachine, exc: &PyBaseExceptionRef) -> PyResult {
|
||||
let args = exc.args();
|
||||
let val = args
|
||||
.borrow_value()
|
||||
.first()
|
||||
.cloned()
|
||||
.unwrap_or_else(|| vm.get_none());
|
||||
let val = vm.unwrap_or_none(args.borrow_value().first().cloned());
|
||||
Ok(val)
|
||||
}
|
||||
|
||||
|
||||
@@ -57,12 +57,15 @@ impl PyMappingProxy {
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn get(&self, key: PyObjectRef, default: OptionalArg, vm: &VirtualMachine) -> PyResult {
|
||||
fn get(
|
||||
&self,
|
||||
key: PyObjectRef,
|
||||
default: OptionalArg,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<Option<PyObjectRef>> {
|
||||
let default = default.into_option();
|
||||
Ok(self
|
||||
.get_inner(key, vm)?
|
||||
.or(default)
|
||||
.unwrap_or_else(|| vm.get_none()))
|
||||
let value = self.get_inner(key, vm)?.or(default);
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
#[pymethod(name = "__getitem__")]
|
||||
|
||||
@@ -3,7 +3,8 @@ use super::objstr::{PyString, PyStringRef};
|
||||
use super::objtype::PyClassRef;
|
||||
use crate::function::{OptionalOption, PyFuncArgs};
|
||||
use crate::pyobject::{
|
||||
BorrowValue, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
|
||||
BorrowValue, IntoPyObject, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult,
|
||||
PyValue,
|
||||
};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
@@ -31,13 +32,13 @@ pub fn init_module_dict(
|
||||
.set_item("__doc__", doc, vm)
|
||||
.expect("Failed to set __doc__ on module");
|
||||
module_dict
|
||||
.set_item("__package__", vm.get_none(), vm)
|
||||
.set_item("__package__", vm.ctx.none(), vm)
|
||||
.expect("Failed to set __package__ on module");
|
||||
module_dict
|
||||
.set_item("__loader__", vm.get_none(), vm)
|
||||
.set_item("__loader__", vm.ctx.none(), vm)
|
||||
.expect("Failed to set __loader__ on module");
|
||||
module_dict
|
||||
.set_item("__spec__", vm.get_none(), vm)
|
||||
.set_item("__spec__", vm.ctx.none(), vm)
|
||||
.expect("Failed to set __spec__ on module");
|
||||
}
|
||||
|
||||
@@ -63,8 +64,7 @@ impl PyModuleRef {
|
||||
vm,
|
||||
&self.as_object().dict().unwrap(),
|
||||
name.into_object(),
|
||||
doc.flatten()
|
||||
.map_or_else(|| vm.get_none(), PyRef::into_object),
|
||||
doc.flatten().into_pyobject(vm),
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -369,12 +369,12 @@ impl PyRange {
|
||||
fn hash(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
|
||||
let length = zelf.length();
|
||||
let elements = if length.is_zero() {
|
||||
vec![vm.ctx.new_int(length), vm.get_none(), vm.get_none()]
|
||||
vec![vm.ctx.new_int(length), vm.ctx.none(), vm.ctx.none()]
|
||||
} else if length.is_one() {
|
||||
vec![
|
||||
vm.ctx.new_int(length),
|
||||
zelf.start().into_object(),
|
||||
vm.get_none(),
|
||||
vm.ctx.none(),
|
||||
]
|
||||
} else {
|
||||
vec![
|
||||
|
||||
@@ -558,13 +558,13 @@ impl PySet {
|
||||
#[pymethod]
|
||||
fn update(&self, others: Args<PyIterable>, vm: &VirtualMachine) -> PyResult {
|
||||
self.inner.update(others, vm)?;
|
||||
Ok(vm.get_none())
|
||||
Ok(vm.ctx.none())
|
||||
}
|
||||
|
||||
#[pymethod]
|
||||
fn intersection_update(&self, others: Args<PyIterable>, vm: &VirtualMachine) -> PyResult {
|
||||
self.inner.intersection_update(others, vm)?;
|
||||
Ok(vm.get_none())
|
||||
Ok(vm.ctx.none())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__iand__")]
|
||||
@@ -576,7 +576,7 @@ impl PySet {
|
||||
#[pymethod]
|
||||
fn difference_update(&self, others: Args<PyIterable>, vm: &VirtualMachine) -> PyResult {
|
||||
self.inner.difference_update(others, vm)?;
|
||||
Ok(vm.get_none())
|
||||
Ok(vm.ctx.none())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__isub__")]
|
||||
@@ -592,7 +592,7 @@ impl PySet {
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult {
|
||||
self.inner.symmetric_difference_update(others, vm)?;
|
||||
Ok(vm.get_none())
|
||||
Ok(vm.ctx.none())
|
||||
}
|
||||
|
||||
#[pymethod(name = "__ixor__")]
|
||||
|
||||
@@ -1011,7 +1011,7 @@ impl PyString {
|
||||
}
|
||||
if let OptionalArg::Present(none_str) = none_str {
|
||||
for c in none_str.value.chars() {
|
||||
new_dict.set_item(vm.ctx.new_int(c as u32), vm.get_none(), vm)?;
|
||||
new_dict.set_item(vm.ctx.new_int(c as u32), vm.ctx.none(), vm)?;
|
||||
}
|
||||
}
|
||||
Ok(new_dict.into_pyobject(vm))
|
||||
@@ -1311,7 +1311,7 @@ mod tests {
|
||||
Interpreter::default().enter(|vm| {
|
||||
let table = vm.ctx.new_dict();
|
||||
table.set_item("a", vm.ctx.new_str("🎅"), &vm).unwrap();
|
||||
table.set_item("b", vm.get_none(), &vm).unwrap();
|
||||
table.set_item("b", vm.ctx.none(), &vm).unwrap();
|
||||
table.set_item("c", vm.ctx.new_str("xda"), &vm).unwrap();
|
||||
let translated = PyString::maketrans(
|
||||
table.into_object(),
|
||||
|
||||
@@ -122,7 +122,7 @@ impl PySuper {
|
||||
vm.new_type_error(format!("super argument {} was not supplied", first_arg))
|
||||
})?
|
||||
} else {
|
||||
vm.get_none()
|
||||
vm.ctx.none()
|
||||
}
|
||||
};
|
||||
|
||||
@@ -147,7 +147,7 @@ impl SlotDescriptor for PySuper {
|
||||
.into_ref(vm)
|
||||
.into_object())
|
||||
} else {
|
||||
let obj = zelf.obj.clone().map_or(vm.get_none(), |(o, _)| o);
|
||||
let obj = vm.unwrap_or_none(zelf.obj.clone().map(|(o, _)| o));
|
||||
vm.invoke(
|
||||
zelf_class.as_object(),
|
||||
vec![zelf.typ.clone().into_object(), obj],
|
||||
|
||||
@@ -194,7 +194,7 @@ impl PyClassRef {
|
||||
} else if let Some(ref descriptor) = attr_class.get_attr("__get__") {
|
||||
drop(mcl);
|
||||
// TODO: is this nessessary?
|
||||
return vm.invoke(descriptor, vec![attr, vm.get_none(), self.into_object()]);
|
||||
return vm.invoke(descriptor, vec![attr, vm.ctx.none(), self.into_object()]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ pub type PyWeakRef = PyRef<PyWeak>;
|
||||
impl SlotCall for PyWeak {
|
||||
fn call(&self, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
|
||||
args.bind::<()>(vm)?;
|
||||
Ok(self.upgrade().unwrap_or_else(|| vm.get_none()))
|
||||
Ok(vm.unwrap_or_none(self.upgrade()))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -7,7 +7,7 @@ use crate::obj::{
|
||||
objbool, objdict::PyDictRef, objfloat, objint, objlist::PyList, objstr, objtuple::PyTuple,
|
||||
objtype,
|
||||
};
|
||||
use crate::pyobject::{BorrowValue, IdProtocol, ItemProtocol, PyObjectRef, TypeProtocol};
|
||||
use crate::pyobject::{BorrowValue, ItemProtocol, PyObjectRef, TypeProtocol};
|
||||
use crate::VirtualMachine;
|
||||
|
||||
#[inline]
|
||||
@@ -96,7 +96,7 @@ impl<'s> serde::Serialize for PyObjectSerializer<'s> {
|
||||
map.serialize_entry(&self.clone_with_object(key), &self.clone_with_object(&e))?;
|
||||
}
|
||||
map.end()
|
||||
} else if self.pyobject.is(&self.vm.get_none()) {
|
||||
} else if self.vm.is_none(&self.pyobject) {
|
||||
serializer.serialize_none()
|
||||
} else {
|
||||
Err(serde::ser::Error::custom(format!(
|
||||
@@ -187,7 +187,7 @@ impl<'de> Visitor<'de> for PyObjectDeserializer<'de> {
|
||||
where
|
||||
E: serde::de::Error,
|
||||
{
|
||||
Ok(self.vm.get_none())
|
||||
Ok(self.vm.ctx.none())
|
||||
}
|
||||
|
||||
fn visit_seq<A>(self, mut access: A) -> Result<Self::Value, A::Error>
|
||||
|
||||
+1
-1
@@ -939,7 +939,7 @@ impl TryFromObject for PyObjectRef {
|
||||
|
||||
impl<T: TryFromObject> TryFromObject for Option<T> {
|
||||
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
|
||||
if vm.get_none().is(&obj) {
|
||||
if vm.is_none(&obj) {
|
||||
Ok(None)
|
||||
} else {
|
||||
T::try_from_object(vm, obj).map(Some)
|
||||
|
||||
+1
-1
@@ -105,7 +105,7 @@ pub trait SlotDescriptor: PyValue {
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<(PyRef<Self>, PyObjectRef)> {
|
||||
let zelf = Self::_zelf(zelf, vm)?;
|
||||
let obj = obj.unwrap_or_else(|| vm.get_none());
|
||||
let obj = vm.unwrap_or_none(obj);
|
||||
Ok((zelf, obj))
|
||||
}
|
||||
|
||||
|
||||
@@ -559,8 +559,8 @@ fn parameters_to_ast(vm: &VirtualMachine, args: &ast::Parameters) -> PyResult<As
|
||||
|
||||
fn vararg_to_ast(vm: &VirtualMachine, vararg: &ast::Varargs) -> PyResult {
|
||||
let py_node = match vararg {
|
||||
ast::Varargs::None => vm.get_none(),
|
||||
ast::Varargs::Unnamed => vm.get_none(),
|
||||
ast::Varargs::None => vm.ctx.none(),
|
||||
ast::Varargs::Unnamed => vm.ctx.none(),
|
||||
ast::Varargs::Named(parameter) => parameter_to_ast(vm, parameter)?.into_object(),
|
||||
};
|
||||
Ok(py_node)
|
||||
|
||||
@@ -37,7 +37,7 @@ mod decl {
|
||||
fn disassemble(co: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
let code = &PyCodeRef::try_from_object(vm, co)?.code;
|
||||
print!("{}", code);
|
||||
Ok(vm.get_none())
|
||||
Ok(vm.ctx.none())
|
||||
}
|
||||
|
||||
#[pyattr(name = "COMPILER_FLAG_NAMES")]
|
||||
|
||||
+41
-46
@@ -67,9 +67,8 @@ impl PyHasher {
|
||||
}
|
||||
|
||||
#[pymethod(name = "update")]
|
||||
fn update(&self, data: PyBytesRef, vm: &VirtualMachine) -> PyResult {
|
||||
fn update(&self, data: PyBytesRef) {
|
||||
self.borrow_value_mut().input(data.borrow_value());
|
||||
Ok(vm.get_none())
|
||||
}
|
||||
|
||||
#[pymethod(name = "digest")]
|
||||
@@ -94,74 +93,70 @@ fn hashlib_new(
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<PyHasher> {
|
||||
match name.borrow_value() {
|
||||
"md5" => md5(data, vm),
|
||||
"sha1" => sha1(data, vm),
|
||||
"sha224" => sha224(data, vm),
|
||||
"sha256" => sha256(data, vm),
|
||||
"sha384" => sha384(data, vm),
|
||||
"sha512" => sha512(data, vm),
|
||||
"sha3_224" => sha3_224(data, vm),
|
||||
"sha3_256" => sha3_256(data, vm),
|
||||
"sha3_384" => sha3_384(data, vm),
|
||||
"sha3_512" => sha3_512(data, vm),
|
||||
// TODO: "shake128" => shake128(data, vm),
|
||||
// TODO: "shake256" => shake256(data, vm),
|
||||
"blake2b" => blake2b(data, vm),
|
||||
"blake2s" => blake2s(data, vm),
|
||||
"md5" => md5(data),
|
||||
"sha1" => sha1(data),
|
||||
"sha224" => sha224(data),
|
||||
"sha256" => sha256(data),
|
||||
"sha384" => sha384(data),
|
||||
"sha512" => sha512(data),
|
||||
"sha3_224" => sha3_224(data),
|
||||
"sha3_256" => sha3_256(data),
|
||||
"sha3_384" => sha3_384(data),
|
||||
"sha3_512" => sha3_512(data),
|
||||
// TODO: "shake128" => shake128(data, ),
|
||||
// TODO: "shake256" => shake256(data, ),
|
||||
"blake2b" => blake2b(data),
|
||||
"blake2s" => blake2s(data),
|
||||
other => Err(vm.new_value_error(format!("Unknown hashing algorithm: {}", other))),
|
||||
}
|
||||
}
|
||||
|
||||
fn init(
|
||||
hasher: PyHasher,
|
||||
data: OptionalArg<PyBytesRef>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<PyHasher> {
|
||||
fn init(hasher: PyHasher, data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
|
||||
if let OptionalArg::Present(data) = data {
|
||||
hasher.update(data, vm)?;
|
||||
hasher.update(data);
|
||||
}
|
||||
|
||||
Ok(hasher)
|
||||
}
|
||||
|
||||
fn md5(data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("md5", HashWrapper::md5()), data, vm)
|
||||
fn md5(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("md5", HashWrapper::md5()), data)
|
||||
}
|
||||
|
||||
fn sha1(data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha1", HashWrapper::sha1()), data, vm)
|
||||
fn sha1(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha1", HashWrapper::sha1()), data)
|
||||
}
|
||||
|
||||
fn sha224(data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha224", HashWrapper::sha224()), data, vm)
|
||||
fn sha224(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha224", HashWrapper::sha224()), data)
|
||||
}
|
||||
|
||||
fn sha256(data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha256", HashWrapper::sha256()), data, vm)
|
||||
fn sha256(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha256", HashWrapper::sha256()), data)
|
||||
}
|
||||
|
||||
fn sha384(data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha384", HashWrapper::sha384()), data, vm)
|
||||
fn sha384(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha384", HashWrapper::sha384()), data)
|
||||
}
|
||||
|
||||
fn sha512(data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha512", HashWrapper::sha512()), data, vm)
|
||||
fn sha512(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha512", HashWrapper::sha512()), data)
|
||||
}
|
||||
|
||||
fn sha3_224(data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha3_224", HashWrapper::sha3_224()), data, vm)
|
||||
fn sha3_224(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha3_224", HashWrapper::sha3_224()), data)
|
||||
}
|
||||
|
||||
fn sha3_256(data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha3_256", HashWrapper::sha3_256()), data, vm)
|
||||
fn sha3_256(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha3_256", HashWrapper::sha3_256()), data)
|
||||
}
|
||||
|
||||
fn sha3_384(data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha3_384", HashWrapper::sha3_384()), data, vm)
|
||||
fn sha3_384(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha3_384", HashWrapper::sha3_384()), data)
|
||||
}
|
||||
|
||||
fn sha3_512(data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha3_512", HashWrapper::sha3_512()), data, vm)
|
||||
fn sha3_512(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
|
||||
init(PyHasher::new("sha3_512", HashWrapper::sha3_512()), data)
|
||||
}
|
||||
|
||||
fn shake128(_data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
|
||||
@@ -172,14 +167,14 @@ fn shake256(_data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyH
|
||||
Err(vm.new_not_implemented_error("shake256".to_owned()))
|
||||
}
|
||||
|
||||
fn blake2b(data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
|
||||
fn blake2b(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
|
||||
// TODO: handle parameters
|
||||
init(PyHasher::new("blake2b", HashWrapper::blake2b()), data, vm)
|
||||
init(PyHasher::new("blake2b", HashWrapper::blake2b()), data)
|
||||
}
|
||||
|
||||
fn blake2s(data: OptionalArg<PyBytesRef>, vm: &VirtualMachine) -> PyResult<PyHasher> {
|
||||
fn blake2s(data: OptionalArg<PyBytesRef>) -> PyResult<PyHasher> {
|
||||
// TODO: handle parameters
|
||||
init(PyHasher::new("blake2s", HashWrapper::blake2s()), data, vm)
|
||||
init(PyHasher::new("blake2s", HashWrapper::blake2s()), data)
|
||||
}
|
||||
|
||||
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
|
||||
@@ -67,7 +67,7 @@ fn imp_create_builtin(spec: PyObjectRef, vm: &VirtualMachine) -> PyResult {
|
||||
} else if let Some(make_module_func) = vm.state.stdlib_inits.get(name) {
|
||||
Ok(make_module_func(vm))
|
||||
} else {
|
||||
Ok(vm.get_none())
|
||||
Ok(vm.ctx.none())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ fn imp_fix_co_filename(_code: PyObjectRef, _path: PyStringRef) {
|
||||
|
||||
fn imp_source_hash(_key: u64, _source: PyBytesRef, vm: &VirtualMachine) -> PyResult {
|
||||
// TODO:
|
||||
Ok(vm.get_none())
|
||||
Ok(vm.ctx.none())
|
||||
}
|
||||
|
||||
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
|
||||
+4
-16
@@ -963,11 +963,7 @@ fn text_io_wrapper_init(
|
||||
|
||||
// let readuniversal = args.newline.map_or_else(true, |s| s.borrow_value().is_empty());
|
||||
|
||||
vm.set_attr(
|
||||
&instance,
|
||||
"encoding",
|
||||
self_encoding.map_or_else(|| vm.get_none(), |s| vm.ctx.new_str(s)),
|
||||
)?;
|
||||
vm.set_attr(&instance, "encoding", self_encoding.into_pyobject(vm))?;
|
||||
vm.set_attr(&instance, "errors", errors)?;
|
||||
vm.set_attr(&instance, "buffer", args.buffer)?;
|
||||
|
||||
@@ -1022,11 +1018,7 @@ fn text_io_wrapper_read(
|
||||
return Err(vm.new_value_error("not readable".to_owned()));
|
||||
}
|
||||
|
||||
let bytes = vm.call_method(
|
||||
&raw,
|
||||
"read",
|
||||
vec![size.flatten().unwrap_or_else(|| vm.get_none())],
|
||||
)?;
|
||||
let bytes = vm.call_method(&raw, "read", vec![vm.unwrap_or_none(size.flatten())])?;
|
||||
let bytes = PyBytesLike::try_from_object(vm, bytes)?;
|
||||
//format bytes into string
|
||||
let rust_string = String::from_utf8(bytes.to_cow().into_owned()).map_err(|e| {
|
||||
@@ -1079,11 +1071,7 @@ fn text_io_wrapper_readline(
|
||||
return Err(vm.new_value_error("not readable".to_owned()));
|
||||
}
|
||||
|
||||
let bytes = vm.call_method(
|
||||
&raw,
|
||||
"readline",
|
||||
vec![size.flatten().unwrap_or_else(|| vm.get_none())],
|
||||
)?;
|
||||
let bytes = vm.call_method(&raw, "readline", vec![vm.unwrap_or_none(size.flatten())])?;
|
||||
let bytes = PyBytesLike::try_from_object(vm, bytes)?;
|
||||
//format bytes into string
|
||||
let rust_string = String::from_utf8(bytes.to_cow().into_owned()).map_err(|e| {
|
||||
@@ -1224,7 +1212,7 @@ pub fn io_open(
|
||||
Args::new(vec![file, vm.ctx.new_str(mode.clone())]),
|
||||
KwArgs::new(maplit::hashmap! {
|
||||
"closefd".to_owned() => vm.ctx.new_bool(opts.closefd),
|
||||
"opener".to_owned() => opts.opener.unwrap_or_else(|| vm.get_none()),
|
||||
"opener".to_owned() => vm.unwrap_or_none(opts.opener),
|
||||
}),
|
||||
)),
|
||||
)?;
|
||||
|
||||
@@ -732,7 +732,7 @@ mod decl {
|
||||
PyObjectRef,
|
||||
) = args.bind(vm)?;
|
||||
|
||||
let start = if !start.is(&vm.get_none()) {
|
||||
let start = if !vm.is_none(&start) {
|
||||
pyobject_to_opt_usize(start, &vm).ok_or_else(|| {
|
||||
vm.new_value_error(
|
||||
"Indices for islice() must be None or an integer: 0 <= x <= sys.maxsize.".to_owned(),
|
||||
@@ -742,7 +742,7 @@ mod decl {
|
||||
0usize
|
||||
};
|
||||
|
||||
let step = if !step.is(&vm.get_none()) {
|
||||
let step = if !vm.is_none(&step) {
|
||||
pyobject_to_opt_usize(step, &vm).ok_or_else(|| {
|
||||
vm.new_value_error(
|
||||
"Step for islice() must be a positive integer or None.".to_owned(),
|
||||
@@ -756,7 +756,7 @@ mod decl {
|
||||
}
|
||||
};
|
||||
|
||||
let stop = if !stop.is(&vm.get_none()) {
|
||||
let stop = if !vm.is_none(&stop) {
|
||||
Some(pyobject_to_opt_usize(stop, &vm).ok_or_else(|| {
|
||||
vm.new_value_error(
|
||||
"Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize."
|
||||
@@ -847,7 +847,7 @@ mod decl {
|
||||
|
||||
loop {
|
||||
let obj = call_next(vm, iterable)?;
|
||||
let pred_value = if predicate.is(&vm.get_none()) {
|
||||
let pred_value = if vm.is_none(predicate) {
|
||||
obj.clone()
|
||||
} else {
|
||||
vm.invoke(predicate, vec![obj.clone()])?
|
||||
@@ -893,7 +893,7 @@ mod decl {
|
||||
|
||||
PyItertoolsAccumulate {
|
||||
iterable: iter,
|
||||
binop: binop.unwrap_or_else(|| vm.get_none()),
|
||||
binop: binop.unwrap_or_none(vm),
|
||||
acc_value: PyRwLock::new(None),
|
||||
}
|
||||
.into_ref_with_type(vm, cls)
|
||||
@@ -909,7 +909,7 @@ mod decl {
|
||||
let next_acc_value = match acc_value {
|
||||
None => obj,
|
||||
Some(value) => {
|
||||
if self.binop.is(&vm.get_none()) {
|
||||
if vm.is_none(&self.binop) {
|
||||
vm._add(value, obj)?
|
||||
} else {
|
||||
vm.invoke(&self.binop, vec![value, obj])?
|
||||
@@ -1502,11 +1502,7 @@ mod decl {
|
||||
args: ZiplongestArgs,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<PyRef<Self>> {
|
||||
let fillvalue = match args.fillvalue.into_option() {
|
||||
Some(i) => i,
|
||||
None => vm.get_none(),
|
||||
};
|
||||
|
||||
let fillvalue = args.fillvalue.unwrap_or_none(vm);
|
||||
let iterators = iterables
|
||||
.into_iter()
|
||||
.map(|iterable| get_iter(vm, &iterable))
|
||||
|
||||
@@ -104,10 +104,8 @@ mod _json {
|
||||
.new_tuple(vec![pystr.into_object(), vm.ctx.new_int(next_idx)]),
|
||||
vm.ctx.new_bool(self.strict),
|
||||
scan_once,
|
||||
self.object_hook.clone().unwrap_or_else(|| vm.get_none()),
|
||||
self.object_pairs_hook
|
||||
.clone()
|
||||
.unwrap_or_else(|| vm.get_none()),
|
||||
vm.unwrap_or_none(self.object_hook.clone()),
|
||||
vm.unwrap_or_none(self.object_pairs_hook.clone()),
|
||||
],
|
||||
);
|
||||
}
|
||||
@@ -134,7 +132,7 @@ mod _json {
|
||||
};
|
||||
}
|
||||
|
||||
parse_const!("null", vm.get_none());
|
||||
parse_const!("null", vm.ctx.none());
|
||||
parse_const!("true", vm.ctx.new_bool(true));
|
||||
parse_const!("false", vm.ctx.new_bool(false));
|
||||
|
||||
|
||||
+3
-6
@@ -23,8 +23,8 @@ use crate::obj::objstr::{PyString, PyStringRef};
|
||||
use crate::obj::objtuple::PyTupleRef;
|
||||
use crate::obj::objtype::PyClassRef;
|
||||
use crate::pyobject::{
|
||||
BorrowValue, Either, ItemProtocol, PyClassImpl, PyObjectRef, PyRef, PyResult, PyStructSequence,
|
||||
PyValue, TryFromObject, TypeProtocol,
|
||||
BorrowValue, Either, IntoPyObject, ItemProtocol, PyClassImpl, PyObjectRef, PyRef, PyResult,
|
||||
PyStructSequence, PyValue, TryFromObject, TypeProtocol,
|
||||
};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
@@ -155,10 +155,7 @@ impl IntoPyException for io::Error {
|
||||
},
|
||||
};
|
||||
let os_error = vm.new_exception_msg(exc_type, self.to_string());
|
||||
let errno = match self.raw_os_error() {
|
||||
Some(errno) => vm.ctx.new_int(errno),
|
||||
None => vm.get_none(),
|
||||
};
|
||||
let errno = self.raw_os_error().into_pyobject(vm);
|
||||
vm.set_attr(os_error.as_object(), "errno", errno).unwrap();
|
||||
os_error
|
||||
}
|
||||
|
||||
+10
-10
@@ -172,14 +172,14 @@ fn do_match(vm: &VirtualMachine, pattern: &PyPattern, search_text: PyStringRef)
|
||||
let regex = Regex::new(®ex).unwrap();
|
||||
|
||||
match regex.captures(search_text.borrow_value().as_bytes()) {
|
||||
None => Ok(vm.get_none()),
|
||||
None => Ok(vm.ctx.none()),
|
||||
Some(captures) => Ok(create_match(vm, search_text.clone(), captures)),
|
||||
}
|
||||
}
|
||||
|
||||
fn do_search(vm: &VirtualMachine, regex: &PyPattern, search_text: PyStringRef) -> PyResult {
|
||||
match regex.regex.captures(search_text.borrow_value().as_bytes()) {
|
||||
None => Ok(vm.get_none()),
|
||||
None => Ok(vm.ctx.none()),
|
||||
Some(captures) => Ok(create_match(vm, search_text.clone(), captures)),
|
||||
}
|
||||
}
|
||||
@@ -255,8 +255,7 @@ fn do_split(
|
||||
let split = output
|
||||
.into_iter()
|
||||
.map(|v| {
|
||||
v.map(|v| vm.ctx.new_str(String::from_utf8_lossy(v).into_owned()))
|
||||
.unwrap_or_else(|| vm.get_none())
|
||||
vm.unwrap_or_none(v.map(|v| vm.ctx.new_str(String::from_utf8_lossy(v).into_owned())))
|
||||
})
|
||||
.collect();
|
||||
Ok(vm.ctx.new_list(split))
|
||||
@@ -407,7 +406,7 @@ impl PyMatch {
|
||||
let bounds = self.get_bounds(id, vm)?;
|
||||
let group = match bounds {
|
||||
Some(bounds) => self.subgroup(bounds, vm),
|
||||
None => vm.get_none(),
|
||||
None => vm.ctx.none(),
|
||||
};
|
||||
Ok(group)
|
||||
}
|
||||
@@ -435,11 +434,12 @@ impl PyMatch {
|
||||
.captures
|
||||
.iter()
|
||||
.map(|capture| {
|
||||
capture
|
||||
.as_ref()
|
||||
.map(|bounds| self.subgroup(bounds.clone(), vm))
|
||||
.or_else(|| default.clone())
|
||||
.unwrap_or_else(|| vm.get_none())
|
||||
vm.unwrap_or_none(
|
||||
capture
|
||||
.as_ref()
|
||||
.map(|bounds| self.subgroup(bounds.clone(), vm))
|
||||
.or_else(|| default.clone()),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
vm.ctx.new_tuple(groups)
|
||||
|
||||
@@ -112,7 +112,7 @@ pub fn check_signals(vm: &VirtualMachine) -> PyResult<()> {
|
||||
if triggerd {
|
||||
let handler = &signal_handlers[signum];
|
||||
if vm.is_callable(handler) {
|
||||
vm.invoke(handler, vec![vm.ctx.new_int(signum), vm.get_none()])?;
|
||||
vm.invoke(handler, vec![vm.ctx.new_int(signum), vm.ctx.none()])?;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -156,7 +156,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
} else if handler == SIG_IGN {
|
||||
sig_ign.clone()
|
||||
} else {
|
||||
vm.get_none()
|
||||
vm.ctx.none()
|
||||
};
|
||||
vm.signal_handlers.as_ref().unwrap().borrow_mut()[signum] = py_handler;
|
||||
}
|
||||
|
||||
@@ -545,10 +545,7 @@ fn socket_getaddrinfo(opts: GAIOptions, vm: &VirtualMachine) -> PyResult {
|
||||
vm.ctx.new_int(ai.address),
|
||||
vm.ctx.new_int(ai.socktype),
|
||||
vm.ctx.new_int(ai.protocol),
|
||||
match ai.canonname {
|
||||
Some(s) => vm.ctx.new_str(s),
|
||||
None => vm.get_none(),
|
||||
},
|
||||
ai.canonname.into_pyobject(vm),
|
||||
get_addr_tuple(ai.sockaddr).into_pyobject(vm),
|
||||
])
|
||||
})
|
||||
@@ -786,7 +783,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
"htons" => ctx.new_function(u16::to_be),
|
||||
"ntohl" => ctx.new_function(u32::from_be),
|
||||
"ntohs" => ctx.new_function(u16::from_be),
|
||||
"getdefaulttimeout" => ctx.new_function(|vm: &VirtualMachine| vm.get_none()),
|
||||
"getdefaulttimeout" => ctx.new_function(|vm: &VirtualMachine| vm.ctx.none()),
|
||||
"has_ipv6" => ctx.new_bool(false),
|
||||
"inet_pton" => ctx.new_function(socket_inet_pton),
|
||||
"inet_ntop" => ctx.new_function(socket_inet_ntop),
|
||||
|
||||
@@ -668,10 +668,7 @@ fn cert_to_py(vm: &VirtualMachine, cert: &X509Ref, binary: bool) -> PyResult {
|
||||
let name_to_py = |name: &x509::X509NameRef| {
|
||||
name.entries()
|
||||
.map(|entry| {
|
||||
let txt = match obj2txt(entry.object(), false) {
|
||||
Some(s) => vm.ctx.new_str(s),
|
||||
None => vm.get_none(),
|
||||
};
|
||||
let txt = obj2txt(entry.object(), false).into_pyobject(vm);
|
||||
let data = vm.ctx.new_str(entry.data().as_utf8()?.to_owned());
|
||||
Ok(vm.ctx.new_tuple(vec![vm.ctx.new_tuple(vec![txt, data])]))
|
||||
})
|
||||
|
||||
@@ -239,7 +239,7 @@ fn reg_to_py(value: RegValue, vm: &VirtualMachine) -> PyResult {
|
||||
}
|
||||
RegType::REG_BINARY | _ => {
|
||||
if value.bytes.is_empty() {
|
||||
Ok(vm.get_none())
|
||||
Ok(vm.ctx.none())
|
||||
} else {
|
||||
Ok(vm.ctx.new_bytes(value.bytes))
|
||||
}
|
||||
|
||||
+4
-4
@@ -215,7 +215,7 @@ fn sys_intern(value: PyStringRef) -> PyStringRef {
|
||||
fn sys_exc_info(vm: &VirtualMachine) -> PyObjectRef {
|
||||
let (ty, val, tb) = match vm.current_exception() {
|
||||
Some(exception) => exceptions::split(exception, vm),
|
||||
None => (vm.get_none(), vm.get_none(), vm.get_none()),
|
||||
None => (vm.ctx.none(), vm.ctx.none(), vm.ctx.none()),
|
||||
};
|
||||
vm.ctx.new_tuple(vec![ty, val, tb])
|
||||
}
|
||||
@@ -229,7 +229,7 @@ fn sys_git_info(vm: &VirtualMachine) -> PyObjectRef {
|
||||
}
|
||||
|
||||
fn sys_exit(code: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult {
|
||||
let code = code.unwrap_or_else(|| vm.get_none());
|
||||
let code = code.unwrap_or_none(vm);
|
||||
Err(vm.new_exception(vm.ctx.exceptions.system_exit.clone(), vec![code]))
|
||||
}
|
||||
|
||||
@@ -243,7 +243,7 @@ fn sys_displayhook(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
|
||||
return Ok(());
|
||||
}
|
||||
// set to none to avoid recursion while printing
|
||||
vm.set_attr(&vm.builtins, "_", vm.get_none())?;
|
||||
vm.set_attr(&vm.builtins, "_", vm.ctx.none())?;
|
||||
// TODO: catch encoding errors
|
||||
let repr = vm.to_repr(&obj)?.into_object();
|
||||
builtins::builtin_print(Args::new(vec![repr]), Default::default(), vm)?;
|
||||
@@ -651,7 +651,7 @@ settrace() -- set the global debug tracing function
|
||||
"meta_path" => ctx.new_list(vec![]),
|
||||
"path_hooks" => ctx.new_list(vec![]),
|
||||
"path_importer_cache" => ctx.new_dict(),
|
||||
"pycache_prefix" => vm.get_none(),
|
||||
"pycache_prefix" => vm.ctx.none(),
|
||||
"dont_write_bytecode" => vm.ctx.new_bool(vm.state.settings.dont_write_bytecode),
|
||||
"setprofile" => ctx.new_function(sys_setprofile),
|
||||
"setrecursionlimit" => ctx.new_function(sys_setrecursionlimit),
|
||||
|
||||
+12
-21
@@ -268,9 +268,9 @@ impl VirtualMachine {
|
||||
&vm,
|
||||
&builtins_dict,
|
||||
vm.ctx.new_str("builtins"),
|
||||
vm.get_none(),
|
||||
vm.ctx.none(),
|
||||
);
|
||||
objmodule::init_module_dict(&vm, &sysmod_dict, vm.ctx.new_str("sys"), vm.get_none());
|
||||
objmodule::init_module_dict(&vm, &sysmod_dict, vm.ctx.new_str("sys"), vm.ctx.none());
|
||||
vm
|
||||
}
|
||||
|
||||
@@ -335,8 +335,8 @@ impl VirtualMachine {
|
||||
wasm_id: self.wasm_id.clone(),
|
||||
exceptions: RefCell::new(vec![]),
|
||||
import_func: self.import_func.clone(),
|
||||
profile_func: RefCell::new(self.get_none()),
|
||||
trace_func: RefCell::new(self.get_none()),
|
||||
profile_func: RefCell::new(self.ctx.none()),
|
||||
trace_func: RefCell::new(self.ctx.none()),
|
||||
use_tracing: Cell::new(false),
|
||||
recursion_limit: self.recursion_limit.clone(),
|
||||
signal_handlers: None,
|
||||
@@ -446,7 +446,7 @@ impl VirtualMachine {
|
||||
self,
|
||||
&dict,
|
||||
self.new_pyobj(name.to_owned()),
|
||||
self.get_none(),
|
||||
self.ctx.none(),
|
||||
);
|
||||
PyObject::new(PyModule {}, self.ctx.types.module_type.clone(), Some(dict))
|
||||
}
|
||||
@@ -663,10 +663,6 @@ impl VirtualMachine {
|
||||
Scope::with_builtins(None, self.ctx.new_dict(), self)
|
||||
}
|
||||
|
||||
pub fn get_none(&self) -> PyObjectRef {
|
||||
self.ctx.none()
|
||||
}
|
||||
|
||||
/// Test whether a python object is `None`.
|
||||
pub fn is_none(&self, obj: &PyObjectRef) -> bool {
|
||||
obj.is(&self.ctx.none)
|
||||
@@ -678,6 +674,9 @@ impl VirtualMachine {
|
||||
Some(obj)
|
||||
}
|
||||
}
|
||||
pub fn unwrap_or_none(&self, obj: Option<PyObjectRef>) -> PyObjectRef {
|
||||
obj.unwrap_or_else(|| self.ctx.none())
|
||||
}
|
||||
|
||||
pub fn get_locals(&self) -> PyDictRef {
|
||||
self.current_scope().get_locals()
|
||||
@@ -767,7 +766,7 @@ impl VirtualMachine {
|
||||
frame.scope.globals.clone().into_object(),
|
||||
)
|
||||
} else {
|
||||
(self.get_none(), self.get_none())
|
||||
(self.ctx.none(), self.ctx.none())
|
||||
};
|
||||
let from_list = self
|
||||
.ctx
|
||||
@@ -824,11 +823,7 @@ impl VirtualMachine {
|
||||
} else if let Some(ref descriptor) = descr_class.get_attr("__get__") {
|
||||
Some(self.invoke(
|
||||
descriptor,
|
||||
vec![
|
||||
descr,
|
||||
obj.unwrap_or_else(|| self.get_none()),
|
||||
cls.unwrap_or_else(|| self.get_none()),
|
||||
],
|
||||
vec![descr, self.unwrap_or_none(obj), self.unwrap_or_none(cls)],
|
||||
))
|
||||
} else {
|
||||
None
|
||||
@@ -910,8 +905,7 @@ impl VirtualMachine {
|
||||
|
||||
let frame = frame_ref.unwrap().as_object().clone();
|
||||
let event = self.ctx.new_str(event.to_string());
|
||||
let arg = self.get_none();
|
||||
let args = vec![frame, event, arg];
|
||||
let args = vec![frame, event, self.ctx.none()];
|
||||
|
||||
// temporarily disable tracing, during the call to the
|
||||
// tracing function itself.
|
||||
@@ -1164,10 +1158,7 @@ impl VirtualMachine {
|
||||
) -> PyResult {
|
||||
let codecsmodule = self.import("_codecs", &[], 0)?;
|
||||
let func = self.get_attribute(codecsmodule, func)?;
|
||||
let mut args = vec![
|
||||
obj,
|
||||
encoding.map_or_else(|| self.get_none(), |s| s.into_object()),
|
||||
];
|
||||
let mut args = vec![obj, encoding.into_pyobject(self)];
|
||||
if let Some(errors) = errors {
|
||||
args.push(errors.into_object());
|
||||
}
|
||||
|
||||
@@ -9,7 +9,8 @@ use rustpython_vm::function::{OptionalArg, PyFuncArgs};
|
||||
use rustpython_vm::import::import_file;
|
||||
use rustpython_vm::obj::{objdict::PyDictRef, objstr::PyStringRef, objtype::PyClassRef};
|
||||
use rustpython_vm::pyobject::{
|
||||
BorrowValue, PyCallable, PyClassImpl, PyObject, PyObjectRef, PyRef, PyResult, PyValue,
|
||||
BorrowValue, IntoPyObject, PyCallable, PyClassImpl, PyObject, PyObjectRef, PyRef, PyResult,
|
||||
PyValue,
|
||||
};
|
||||
use rustpython_vm::VirtualMachine;
|
||||
|
||||
@@ -146,12 +147,12 @@ fn browser_request_animation_frame(func: PyCallable, vm: &VirtualMachine) -> PyR
|
||||
Ok(vm.ctx.new_int(id))
|
||||
}
|
||||
|
||||
fn browser_cancel_animation_frame(id: i32, vm: &VirtualMachine) -> PyResult {
|
||||
fn browser_cancel_animation_frame(id: i32, vm: &VirtualMachine) -> PyResult<()> {
|
||||
window()
|
||||
.cancel_animation_frame(id)
|
||||
.map_err(|err| convert::js_py_typeerror(vm, err))?;
|
||||
|
||||
Ok(vm.get_none())
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[pyclass(module = "browser", name = "Promise")]
|
||||
@@ -270,11 +271,9 @@ impl Document {
|
||||
let elem = self
|
||||
.doc
|
||||
.query_selector(query.borrow_value())
|
||||
.map_err(|err| convert::js_py_typeerror(vm, err))?;
|
||||
let elem = match elem {
|
||||
Some(elem) => Element { elem }.into_object(vm),
|
||||
None => vm.get_none(),
|
||||
};
|
||||
.map_err(|err| convert::js_py_typeerror(vm, err))?
|
||||
.map(|elem| Element { elem })
|
||||
.into_pyobject(vm);
|
||||
Ok(elem)
|
||||
}
|
||||
}
|
||||
@@ -302,7 +301,7 @@ impl Element {
|
||||
) -> PyObjectRef {
|
||||
match self.elem.get_attribute(attr.borrow_value()) {
|
||||
Some(s) => vm.ctx.new_str(s),
|
||||
None => default.into_option().unwrap_or_else(|| vm.get_none()),
|
||||
None => default.unwrap_or_none(vm),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -216,10 +216,10 @@ pub fn js_to_py(vm: &VirtualMachine, js_val: JsValue) -> PyObjectRef {
|
||||
js_err_to_py_err(vm, err).into_object()
|
||||
} else if js_val.is_undefined() {
|
||||
// Because `JSON.stringify(undefined)` returns undefined
|
||||
vm.get_none()
|
||||
vm.ctx.none()
|
||||
} else {
|
||||
py_serde::deserialize(vm, serde_wasm_bindgen::Deserializer::from(js_val))
|
||||
.unwrap_or_else(|_| vm.get_none())
|
||||
.unwrap_or_else(|_| vm.ctx.none())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user