remove vm.get_none and clean up its use cases · RustPython/RustPython@0f7ed21 · GitHub
Skip to content

Commit 0f7ed21

Browse files
committed
remove vm.get_none and clean up its use cases
1 parent 3d908dc commit 0f7ed21

42 files changed

Lines changed: 194 additions & 255 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

vm/src/exceptions.rs

Lines changed: 11 additions & 24 deletions

vm/src/frame.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl Frame {
170170
stack: Vec::new(),
171171
blocks: Vec::new(),
172172
}),
173-
trace: PyMutex::new(vm.get_none()),
173+
trace: PyMutex::new(vm.ctx.none()),
174174
}
175175
}
176176
}
@@ -540,14 +540,11 @@ impl ExecutingFrame<'_> {
540540
let exit = self.pop_value();
541541

542542
let args = if let Some(exc) = exc {
543-
let exc_type = exc.class().into_object();
544-
let exc_val = exc.clone();
545-
let exc_tb = exc.traceback().map_or(vm.get_none(), |tb| tb.into_object());
546-
vec![exc_type, exc_val.into_object(), exc_tb]
543+
exceptions::split(exc, vm)
547544
} else {
548-
vec![vm.ctx.none(), vm.ctx.none(), vm.ctx.none()]
545+
(vm.ctx.none(), vm.ctx.none(), vm.ctx.none())
549546
};
550-
let exit_res = vm.invoke(&exit, args)?;
547+
let exit_res = vm.invoke(&exit, vec![args.0, args.1, args.2])?;
551548
self.push_value(exit_res);
552549

553550
Ok(None)
@@ -1287,7 +1284,7 @@ impl ExecutingFrame<'_> {
12871284
.ctx
12881285
.new_pyfunction(code_obj, scope, defaults, kw_only_defaults);
12891286

1290-
vm.set_attr(&func_obj, "__doc__", vm.get_none())?;
1287+
vm.set_attr(&func_obj, "__doc__", vm.ctx.none())?;
12911288

12921289
let name = qualified_name
12931290
.borrow_value()
@@ -1296,11 +1293,7 @@ impl ExecutingFrame<'_> {
12961293
.unwrap();
12971294
vm.set_attr(&func_obj, "__name__", vm.ctx.new_str(name))?;
12981295
vm.set_attr(&func_obj, "__qualname__", qualified_name)?;
1299-
let module = self
1300-
.scope
1301-
.globals
1302-
.get_item_option("__name__", vm)?
1303-
.unwrap_or_else(|| vm.get_none());
1296+
let module = vm.unwrap_or_none(self.scope.globals.get_item_option("__name__", vm)?);
13041297
vm.set_attr(&func_obj, "__module__", module)?;
13051298
vm.set_attr(&func_obj, "__annotations__", annotations)?;
13061299

vm/src/function.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,12 @@ pub enum OptionalArg<T = PyObjectRef> {
394394

395395
impl_option_like!(OptionalArg, Present, Missing);
396396

397+
impl OptionalArg<PyObjectRef> {
398+
pub fn unwrap_or_none(self, vm: &VirtualMachine) -> PyObjectRef {
399+
self.unwrap_or_else(|| vm.ctx.none())
400+
}
401+
}
402+
397403
pub type OptionalOption<T> = OptionalArg<Option<T>>;
398404

399405
impl<T> OptionalOption<T> {

vm/src/obj/objasyncgenerator.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ impl PyAsyncGen {
3939

4040
// TODO: fix function names situation
4141
#[pyproperty(magic)]
42-
fn name(&self, vm: &VirtualMachine) -> PyObjectRef {
43-
vm.get_none()
44-
}
42+
fn name(&self) {}
4543

4644
#[pymethod(name = "__aiter__")]
4745
fn aiter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
@@ -50,7 +48,7 @@ impl PyAsyncGen {
5048

5149
#[pymethod(name = "__anext__")]
5250
fn anext(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyAsyncGenASend {
53-
Self::asend(zelf, vm.get_none(), vm)
51+
Self::asend(zelf, vm.ctx.none(), vm)
5452
}
5553

5654
#[pymethod]
@@ -76,8 +74,8 @@ impl PyAsyncGen {
7674
state: AtomicCell::new(AwaitableState::Init),
7775
value: (
7876
exc_type,
79-
exc_val.unwrap_or_else(|| vm.get_none()),
80-
exc_tb.unwrap_or_else(|| vm.get_none()),
77+
exc_val.unwrap_or_none(vm),
78+
exc_tb.unwrap_or_none(vm),
8179
),
8280
}
8381
}
@@ -90,8 +88,8 @@ impl PyAsyncGen {
9088
state: AtomicCell::new(AwaitableState::Init),
9189
value: (
9290
vm.ctx.exceptions.generator_exit.clone().into_object(),
93-
vm.get_none(),
94-
vm.get_none(),
91+
vm.ctx.none(),
92+
vm.ctx.none(),
9593
),
9694
}
9795
}
@@ -185,7 +183,7 @@ impl PyAsyncGenASend {
185183

186184
#[pymethod(name = "__next__")]
187185
fn next(&self, vm: &VirtualMachine) -> PyResult {
188-
self.send(vm.get_none(), vm)
186+
self.send(vm.ctx.none(), vm)
189187
}
190188

191189
#[pymethod]
@@ -236,8 +234,8 @@ impl PyAsyncGenASend {
236234

237235
let res = self.ag.inner.throw(
238236
exc_type,
239-
exc_val.unwrap_or_else(|| vm.get_none()),
240-
exc_tb.unwrap_or_else(|| vm.get_none()),
237+
exc_val.unwrap_or_none(vm),
238+
exc_tb.unwrap_or_none(vm),
241239
vm,
242240
);
243241
let res = PyAsyncGenWrappedValue::unbox(&self.ag, res, vm);
@@ -281,7 +279,7 @@ impl PyAsyncGenAThrow {
281279

282280
#[pymethod(name = "__next__")]
283281
fn next(&self, vm: &VirtualMachine) -> PyResult {
284-
self.send(vm.get_none(), vm)
282+
self.send(vm.ctx.none(), vm)
285283
}
286284

287285
#[pymethod]
@@ -353,8 +351,8 @@ impl PyAsyncGenAThrow {
353351
) -> PyResult {
354352
let ret = self.ag.inner.throw(
355353
exc_type,
356-
exc_val.unwrap_or_else(|| vm.get_none()),
357-
exc_tb.unwrap_or_else(|| vm.get_none()),
354+
exc_val.unwrap_or_none(vm),
355+
exc_tb.unwrap_or_none(vm),
358356
vm,
359357
);
360358
let res = if self.aclose {

vm/src/obj/objbuiltinfunc.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use std::fmt;
33
use crate::function::{OptionalArg, PyFuncArgs, PyNativeFunc};
44
use crate::obj::objstr::PyStringRef;
55
use crate::obj::objtype::PyClassRef;
6-
use crate::pyobject::{
7-
IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyResult, PyValue, TypeProtocol,
8-
};
6+
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyResult, PyValue, TypeProtocol};
97
use crate::slots::{SlotCall, SlotDescriptor};
108
use crate::vm::VirtualMachine;
119

@@ -118,7 +116,7 @@ impl SlotDescriptor for PyBuiltinMethod {
118116
Ok(obj) => obj,
119117
Err(result) => return result,
120118
};
121-
if obj.is(&vm.get_none()) && !Self::_cls_is(&cls, &obj.class()) {
119+
if vm.is_none(&obj) && !Self::_cls_is(&cls, &obj.class()) {
122120
Ok(zelf.into_object())
123121
} else {
124122
Ok(vm.ctx.new_bound_method(zelf.into_object(), obj))

vm/src/obj/objcoroinner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ impl Coro {
139139
f.gen_throw(
140140
vm,
141141
vm.ctx.exceptions.generator_exit.clone().into_object(),
142-
vm.get_none(),
143-
vm.get_none(),
142+
vm.ctx.none(),
143+
vm.ctx.none(),
144144
)
145145
});
146146
self.closed.store(true);

vm/src/obj/objcoroutine.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl PyCoroutine {
3737
// TODO: fix function names situation
3838
#[pyproperty(magic)]
3939
fn name(&self, vm: &VirtualMachine) -> PyObjectRef {
40-
vm.get_none()
40+
vm.ctx.none()
4141
}
4242

4343
#[pymethod]
@@ -55,8 +55,8 @@ impl PyCoroutine {
5555
) -> PyResult {
5656
self.inner.throw(
5757
exc_type,
58-
exc_val.unwrap_or_else(|| vm.get_none()),
59-
exc_tb.unwrap_or_else(|| vm.get_none()),
58+
exc_val.unwrap_or_none(vm),
59+
exc_tb.unwrap_or_none(vm),
6060
vm,
6161
)
6262
}
@@ -116,7 +116,7 @@ impl PyCoroutineWrapper {
116116

117117
#[pymethod(name = "__next__")]
118118
fn next(&self, vm: &VirtualMachine) -> PyResult {
119-
self.coro.send(vm.get_none(), vm)
119+
self.coro.send(vm.ctx.none(), vm)
120120
}
121121

122122
#[pymethod]

vm/src/obj/objdict.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl PyDict {
136136
vm: &VirtualMachine,
137137
) -> PyResult<PyRef<Self>> {
138138
let dict = DictContentType::default();
139-
let value = value.unwrap_or_else(|| vm.ctx.none());
139+
let value = value.unwrap_or_none(vm);
140140
for elem in iterable.iter(vm)? {
141141
let elem = elem?;
142142
dict.insert(vm, elem, value.clone())?;
@@ -314,7 +314,7 @@ impl PyDict {
314314
) -> PyResult {
315315
match self.entries.get(vm, &key)? {
316316
Some(value) => Ok(value),
317-
None => Ok(default.unwrap_or_else(|| vm.ctx.none())),
317+
None => Ok(default.unwrap_or_none(vm)),
318318
}
319319
}
320320

@@ -328,7 +328,7 @@ impl PyDict {
328328
match self.entries.get(vm, &key)? {
329329
Some(value) => Ok(value),
330330
None => {
331-
let set_value = default.unwrap_or_else(|| vm.ctx.none());
331+
let set_value = default.unwrap_or_none(vm);
332332
self.entries.insert(vm, key, set_value.clone())?;
333333
Ok(set_value)
334334
}

vm/src/obj/objfilter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::objbool;
22
use super::objiter;
33
use super::objtype::PyClassRef;
4-
use crate::pyobject::{IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
4+
use crate::pyobject::{PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue};
55
use crate::vm::VirtualMachine;
66

77
pub type PyFilterRef = PyRef<PyFilter>;
@@ -47,7 +47,7 @@ impl PyFilter {
4747
let iterator = &self.iterator;
4848
loop {
4949
let next_obj = objiter::call_next(vm, iterator)?;
50-
let predicate_value = if predicate.is(&vm.get_none()) {
50+
let predicate_value = if vm.is_none(predicate) {
5151
next_obj.clone()
5252
} else {
5353
// the predicate itself can raise StopIteration which does stop the filter

vm/src/obj/objframe.rs

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)