Reduce direct usage of vm.ctx.none · RustPython/RustPython@b876629 · GitHub
Skip to content

Commit b876629

Browse files
committed
Reduce direct usage of vm.ctx.none
1 parent 90b3f7a commit b876629

7 files changed

Lines changed: 67 additions & 90 deletions

File tree

vm/src/exceptions.rs

Lines changed: 5 additions & 6 deletions

vm/src/obj/objobject.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ impl PyBaseObject {
140140
}
141141

142142
#[pyclassmethod(magic)]
143-
fn init_subclass(_cls: PyClassRef, vm: &VirtualMachine) -> PyResult {
144-
Ok(vm.ctx.none())
145-
}
143+
fn init_subclass(_cls: PyClassRef) {}
146144

147145
#[pymethod(magic)]
148146
pub fn dir(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyList> {
@@ -176,9 +174,7 @@ impl PyBaseObject {
176174
}
177175

178176
#[pymethod(magic)]
179-
fn init(_args: PyFuncArgs, vm: &VirtualMachine) -> PyResult {
180-
Ok(vm.ctx.none())
181-
}
177+
fn init(_args: PyFuncArgs) {}
182178

183179
#[pyproperty(name = "__class__")]
184180
fn get_class(obj: PyObjectRef) -> PyObjectRef {

vm/src/obj/objrange.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,21 +369,21 @@ impl PyRange {
369369
fn hash(zelf: PyRef<Self>, vm: &VirtualMachine) -> PyResult<PyHash> {
370370
let length = zelf.length();
371371
let elements = if length.is_zero() {
372-
vec![vm.ctx.new_int(length), vm.ctx.none(), vm.ctx.none()]
372+
[vm.ctx.new_int(length), vm.ctx.none(), vm.ctx.none()]
373373
} else if length.is_one() {
374-
vec![
374+
[
375375
vm.ctx.new_int(length),
376376
zelf.start().into_object(),
377377
vm.ctx.none(),
378378
]
379379
} else {
380-
vec![
380+
[
381381
vm.ctx.new_int(length),
382382
zelf.start().into_object(),
383383
zelf.step().into_object(),
384384
]
385385
};
386-
pyobject::hash_iter(&elements, vm)
386+
pyobject::hash_iter(elements.iter(), vm)
387387
}
388388

389389
#[pyslot]

vm/src/obj/objset.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -556,15 +556,15 @@ impl PySet {
556556
}
557557

558558
#[pymethod]
559-
fn update(&self, others: Args<PyIterable>, vm: &VirtualMachine) -> PyResult {
559+
fn update(&self, others: Args<PyIterable>, vm: &VirtualMachine) -> PyResult<()> {
560560
self.inner.update(others, vm)?;
561-
Ok(vm.ctx.none())
561+
Ok(())
562562
}
563563

564564
#[pymethod]
565-
fn intersection_update(&self, others: Args<PyIterable>, vm: &VirtualMachine) -> PyResult {
565+
fn intersection_update(&self, others: Args<PyIterable>, vm: &VirtualMachine) -> PyResult<()> {
566566
self.inner.intersection_update(others, vm)?;
567-
Ok(vm.ctx.none())
567+
Ok(())
568568
}
569569

570570
#[pymethod(name = "__iand__")]
@@ -574,9 +574,9 @@ impl PySet {
574574
}
575575

576576
#[pymethod]
577-
fn difference_update(&self, others: Args<PyIterable>, vm: &VirtualMachine) -> PyResult {
577+
fn difference_update(&self, others: Args<PyIterable>, vm: &VirtualMachine) -> PyResult<()> {
578578
self.inner.difference_update(others, vm)?;
579-
Ok(vm.ctx.none())
579+
Ok(())
580580
}
581581

582582
#[pymethod(name = "__isub__")]
@@ -590,9 +590,9 @@ impl PySet {
590590
&self,
591591
others: Args<PyIterable>,
592592
vm: &VirtualMachine,
593-
) -> PyResult {
593+
) -> PyResult<()> {
594594
self.inner.symmetric_difference_update(others, vm)?;
595-
Ok(vm.ctx.none())
595+
Ok(())
596596
}
597597

598598
#[pymethod(name = "__ixor__")]

vm/src/stdlib/ast.rs

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustpython_parser::{ast, mode::Mode, parser};
1111

1212
use crate::obj::objlist::PyListRef;
1313
use crate::obj::objtype::PyClassRef;
14-
use crate::pyobject::{PyObjectRef, PyRef, PyResult, PyValue};
14+
use crate::pyobject::{IntoPyObject, PyObjectRef, PyRef, PyResult, PyValue};
1515
use crate::slots::PyTpFlags;
1616
use crate::vm::VirtualMachine;
1717

@@ -204,7 +204,7 @@ fn statement_to_ast(vm: &VirtualMachine, statement: &ast::Statement) -> PyResult
204204
names,
205205
} => node!(vm, ImportFrom, {
206206
level => vm.ctx.new_int(*level),
207-
module => optional_string_to_py_obj(vm, module),
207+
module => module.as_ref().into_pyobject(vm),
208208
names => map_ast(alias_to_ast, vm, names)?
209209
}),
210210
Nonlocal { names } => node!(vm, Nonlocal, {
@@ -247,7 +247,7 @@ fn statement_to_ast(vm: &VirtualMachine, statement: &ast::Statement) -> PyResult
247247
fn alias_to_ast(vm: &VirtualMachine, alias: &ast::ImportSymbol) -> PyResult<AstNodeRef> {
248248
Ok(node!(vm, alias, {
249249
name => vm.ctx.new_str(&alias.symbol),
250-
asname => optional_string_to_py_obj(vm, &alias.alias)
250+
asname => alias.alias.as_ref().into_pyobject(vm),
251251
}))
252252
}
253253

@@ -274,7 +274,7 @@ fn with_item_to_ast(vm: &VirtualMachine, with_item: &ast::WithItem) -> PyResult<
274274
fn handler_to_ast(vm: &VirtualMachine, handler: &ast::ExceptHandler) -> PyResult<AstNodeRef> {
275275
let node = node!(vm, ExceptHandler, {
276276
typ => optional_expression_to_ast(vm, &handler.typ)?,
277-
name => optional_string_to_py_obj(vm, &handler.name),
277+
name => handler.name.as_ref().into_pyobject(vm),
278278
body => statements_to_ast(vm, &handler.body)?,
279279
});
280280
Ok(node)
@@ -297,12 +297,11 @@ fn optional_expressions_to_ast(
297297
}
298298

299299
fn optional_expression_to_ast(vm: &VirtualMachine, value: &Option<ast::Expression>) -> PyResult {
300-
let value = if let Some(value) = value {
301-
expression_to_ast(vm, value)?.into_object()
302-
} else {
303-
vm.ctx.none()
304-
};
305-
Ok(value)
300+
let ast = value
301+
.as_ref()
302+
.map(|expr| expression_to_ast(vm, expr))
303+
.transpose()?;
304+
Ok(ast.into_pyobject(vm))
306305
}
307306

308307
fn expressions_to_ast(vm: &VirtualMachine, expressions: &[ast::Expression]) -> PyResult<PyListRef> {
@@ -439,11 +438,8 @@ fn expression_to_ast(vm: &VirtualMachine, expression: &ast::Expression) -> PyRes
439438
let mut keys = Vec::new();
440439
let mut values = Vec::new();
441440
for (k, v) in elements {
442-
if let Some(k) = k {
443-
keys.push(expression_to_ast(vm, k)?.into_object());
444-
} else {
445-
keys.push(vm.ctx.none());
446-
}
441+
let k = k.as_ref().map(|k| expression_to_ast(vm, k)).transpose()?;
442+
keys.push(k.into_pyobject(vm));
447443
values.push(expression_to_ast(vm, v)?.into_object());
448444
}
449445

@@ -484,13 +480,12 @@ fn expression_to_ast(vm: &VirtualMachine, expression: &ast::Expression) -> PyRes
484480
})
485481
}
486482
Yield { value } => {
487-
let py_value = if let Some(value) = value {
488-
expression_to_ast(vm, value)?.into_object()
489-
} else {
490-
vm.ctx.none()
491-
};
483+
let py_value = value
484+
.as_ref()
485+
.map(|v| expression_to_ast(vm, v))
486+
.transpose()?;
492487
node!(vm, Yield, {
493-
value => py_value
488+
value => py_value.into_pyobject(vm)
494489
})
495490
}
496491
YieldFrom { value } => {
@@ -567,12 +562,12 @@ fn vararg_to_ast(vm: &VirtualMachine, vararg: &ast::Varargs) -> PyResult {
567562
}
568563

569564
fn parameter_to_ast(vm: &VirtualMachine, parameter: &ast::Parameter) -> PyResult<AstNodeRef> {
570-
let py_annotation = if let Some(annotation) = &parameter.annotation {
571-
expression_to_ast(vm, annotation)?.into_object()
572-
} else {
573-
vm.ctx.none()
574-
};
575-
565+
let py_annotation = parameter
566+
.annotation
567+
.as_ref()
568+
.map(|expr| expression_to_ast(vm, expr))
569+
.transpose()?
570+
.into_pyobject(vm);
576571
let py_node = node!(vm, arg, {
577572
arg => vm.ctx.new_str(&parameter.arg),
578573
annotation => py_annotation
@@ -584,17 +579,9 @@ fn parameter_to_ast(vm: &VirtualMachine, parameter: &ast::Parameter) -> PyResult
584579
Ok(py_node)
585580
}
586581

587-
fn optional_string_to_py_obj(vm: &VirtualMachine, name: &Option<String>) -> PyObjectRef {
588-
if let Some(name) = name {
589-
vm.ctx.new_str(name)
590-
} else {
591-
vm.ctx.none()
592-
}
593-
}
594-
595582
fn keyword_to_ast(vm: &VirtualMachine, keyword: &ast::Keyword) -> PyResult<AstNodeRef> {
596583
Ok(node!(vm, keyword, {
597-
arg => optional_string_to_py_obj(vm, &keyword.name),
584+
arg => keyword.name.as_ref().into_pyobject(vm),
598585
value => expression_to_ast(vm, &keyword.value)?
599586
}))
600587
}

vm/src/stdlib/dis.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,30 @@ mod decl {
1111
use rustpython_compiler::compile;
1212

1313
#[pyfunction]
14-
fn dis(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
15-
// Method or function:
16-
if let Ok(co) = vm.get_attribute(obj.clone(), "__code__") {
17-
return disassemble(co, vm);
18-
}
19-
20-
// String:
21-
if let Ok(co_str) = PyStringRef::try_from_object(vm, obj.clone()) {
22-
let code = vm
23-
.compile(
24-
co_str.borrow_value(),
25-
compile::Mode::Exec,
26-
"<string>".to_owned(),
27-
)
28-
.map_err(|err| vm.new_syntax_error(&err))?
29-
.into_object();
30-
return disassemble(code, vm);
31-
}
32-
33-
disassemble(obj, vm)
14+
fn dis(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
15+
let co = if let Ok(co) = vm.get_attribute(obj.clone(), "__code__") {
16+
// Method or function:
17+
co
18+
} else if let Ok(co_str) = PyStringRef::try_from_object(vm, obj.clone()) {
19+
// String:
20+
vm.compile(
21+
co_str.borrow_value(),
22+
compile::Mode::Exec,
23+
"<string>".to_owned(),
24+
)
25+
.map_err(|err| vm.new_syntax_error(&err))?
26+
.into_object()
27+
} else {
28+
obj
29+
};
30+
disassemble(co, vm)
3431
}
3532

3633
#[pyfunction]
37-
fn disassemble(co: PyObjectRef, vm: &VirtualMachine) -> PyResult {
34+
fn disassemble(co: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
3835
let code = &PyCodeRef::try_from_object(vm, co)?.code;
3936
print!("{}", code);
40-
Ok(vm.ctx.none())
37+
Ok(())
4138
}
4239

4340
#[pyattr(name = "COMPILER_FLAG_NAMES")]

vm/src/sysmodule.rs

Lines changed: 6 additions & 8 deletions

0 commit comments

Comments
 (0)