Skip to content

Commit 1949239

Browse files
committed
cleanups, type fixes and renames
1 parent 849c6eb commit 1949239

File tree

5 files changed

+33
-36
lines changed

5 files changed

+33
-36
lines changed

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> Result<(), Com
159159
Ok(value) => {
160160
// Save non-None values as "_"
161161

162-
use rustpython_vm::pyobject::{IdProtocol, IntoPyObject, ItemProtocol};
162+
use rustpython_vm::pyobject::{IdProtocol, IntoPyObject};
163163

164164
if !value.is(&vm.get_none()) {
165165
let key = objstr::PyString::from("_").into_pyobject(vm);

vm/src/frame.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,15 @@ impl Scope {
124124
self.locals.iter().next().cloned()
125125
}
126126

127-
pub fn child_scope_with_locals(&self, locals: PyDictRef) -> Scope {
127+
pub fn new_child_scope_with_locals(&self, locals: PyDictRef) -> Scope {
128128
Scope {
129129
locals: self.locals.clone().insert(locals),
130130
globals: self.globals.clone(),
131131
}
132132
}
133133

134-
pub fn child_scope(&self, ctx: &PyContext) -> Scope {
135-
self.child_scope_with_locals(ctx.new_dict())
134+
pub fn new_child_scope(&self, ctx: &PyContext) -> Scope {
135+
self.new_child_scope_with_locals(ctx.new_dict())
136136
}
137137
}
138138

@@ -651,22 +651,22 @@ impl Frame {
651651
// pop argc arguments
652652
// argument: name, args, globals
653653
let scope = self.scope.clone();
654-
let obj = vm
654+
let func_obj = vm
655655
.ctx
656656
.new_function(code_obj, scope, defaults, kw_only_defaults);
657657

658658
let name = qualified_name.value.split('.').next_back().unwrap();
659-
vm.set_attr(&obj, "__name__", vm.new_str(name.to_string()))?;
660-
vm.set_attr(&obj, "__qualname__", qualified_name)?;
659+
vm.set_attr(&func_obj, "__name__", vm.new_str(name.to_string()))?;
660+
vm.set_attr(&func_obj, "__qualname__", qualified_name)?;
661661
let module = self
662662
.scope
663663
.globals
664664
.get_item_option("__name__", vm)?
665665
.unwrap_or_else(|| vm.get_none());
666-
vm.set_attr(&obj, "__module__", module)?;
667-
vm.set_attr(&obj, "__annotations__", annotations)?;
666+
vm.set_attr(&func_obj, "__module__", module)?;
667+
vm.set_attr(&func_obj, "__annotations__", annotations)?;
668668

669-
self.push_value(obj);
669+
self.push_value(func_obj);
670670
Ok(None)
671671
}
672672
bytecode::Instruction::CallFunction { typ } => {
@@ -1318,14 +1318,14 @@ impl Frame {
13181318
|| objtype::isinstance(&exception, &vm.ctx.exceptions.base_exception_type)
13191319
{
13201320
Ok(exception)
1321-
} else if let Ok(exception) = PyClassRef::try_from_object(vm, exception) {
1322-
if objtype::issubclass(&exception, &vm.ctx.exceptions.base_exception_type) {
1323-
let exception = vm.new_empty_exception(exception)?;
1321+
} else if let Ok(exc_type) = PyClassRef::try_from_object(vm, exception) {
1322+
if objtype::issubclass(&exc_type, &vm.ctx.exceptions.base_exception_type) {
1323+
let exception = vm.new_empty_exception(exc_type)?;
13241324
Ok(exception)
13251325
} else {
13261326
let msg = format!(
13271327
"Can only raise BaseException derived types, not {}",
1328-
exception
1328+
exc_type
13291329
);
13301330
Err(vm.new_type_error(msg))
13311331
}

vm/src/stdlib/binascii.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn binascii_crc32(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
6868
);
6969

7070
let bytes = objbytes::get_value(data);
71-
let mut crc = match value {
71+
let crc = match value {
7272
None => 0u32,
7373
Some(value) => objint::get_value(&value).to_u32().unwrap(),
7474
};

vm/src/symboltable.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ pub fn make_symbol_table(program: &ast::Program) -> Result<SymbolScope, SymbolTa
1616
let mut builder = SymbolTableBuilder::new();
1717
builder.enter_scope();
1818
builder.scan_program(program)?;
19-
assert!(builder.scopes.len() == 1);
19+
assert_eq!(builder.scopes.len(), 1);
20+
2021
let symbol_table = builder.scopes.pop().unwrap();
2122
analyze_symbol_table(&symbol_table, None)?;
2223
Ok(symbol_table)
@@ -28,7 +29,8 @@ pub fn statements_to_symbol_table(
2829
let mut builder = SymbolTableBuilder::new();
2930
builder.enter_scope();
3031
builder.scan_statements(statements)?;
31-
assert!(builder.scopes.len() == 1);
32+
assert_eq!(builder.scopes.len(), 1);
33+
3234
let symbol_table = builder.scopes.pop().unwrap();
3335
analyze_symbol_table(&symbol_table, None)?;
3436
Ok(symbol_table)
@@ -42,8 +44,7 @@ pub enum SymbolRole {
4244
Assigned,
4345
}
4446

45-
/// Symbolscope captures all symbols in the current scope, and
46-
/// has a list of subscopes in this scope.
47+
/// Captures all symbols in the current scope, and has a list of subscopes in this scope.
4748
pub struct SymbolScope {
4849
/// A set of symbols present on this scope level.
4950
pub symbols: HashMap<String, SymbolRole>,
@@ -101,7 +102,6 @@ fn analyze_symbol_table(
101102
symbol_scope: &SymbolScope,
102103
parent_symbol_scope: Option<&SymbolScope>,
103104
) -> SymbolTableResult {
104-
// println!("Analyzing {:?}, parent={:?} symbols={:?}", symbol_scope, parent_symbol_scope, symbol_scope.symbols);
105105
// Analyze sub scopes:
106106
for sub_scope in &symbol_scope.sub_scopes {
107107
analyze_symbol_table(&sub_scope, Some(symbol_scope))?;
@@ -157,7 +157,6 @@ impl SymbolTableBuilder {
157157
}
158158

159159
pub fn enter_scope(&mut self) {
160-
// Create new scope and push into scope stack.
161160
let scope = SymbolScope::new();
162161
self.scopes.push(scope);
163162
}
@@ -494,7 +493,7 @@ impl SymbolTableBuilder {
494493
}
495494

496495
fn enter_function(&mut self, args: &ast::Parameters) -> SymbolTableResult {
497-
// Evaulate eventual default parameters:
496+
// Evaluate eventual default parameters:
498497
self.scan_expressions(&args.defaults)?;
499498
for kw_default in &args.kw_defaults {
500499
if let Some(expression) = kw_default {
@@ -545,9 +544,8 @@ impl SymbolTableBuilder {
545544
let scope_depth = self.scopes.len();
546545
let current_scope = self.scopes.last_mut().unwrap();
547546
let location = Default::default();
548-
if let Some(_old_role) = current_scope.symbols.get(name) {
547+
if current_scope.symbols.contains_key(name) {
549548
// Role already set..
550-
// debug!("TODO: {:?}", old_role);
551549
match role {
552550
SymbolRole::Global => {
553551
return Err(SymbolTableError {

vm/src/vm.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,13 @@ impl VirtualMachine {
171171
self.invoke(exc_type.into_object(), args)
172172
}
173173

174+
/// Create Python instance of `exc_type` with message
174175
pub fn new_exception(&self, exc_type: PyClassRef, msg: String) -> PyObjectRef {
175176
// TODO: exc_type may be user-defined exception, so we should return PyResult
176177
// TODO: maybe there is a clearer way to create an instance:
177178
info!("New exception created: {}", msg);
178179
let pymsg = self.new_str(msg);
179180
let args: Vec<PyObjectRef> = vec![pymsg];
180-
181-
// Call function:
182181
self.invoke(exc_type.into_object(), args).unwrap()
183182
}
184183

@@ -402,13 +401,13 @@ impl VirtualMachine {
402401
scope: &Scope,
403402
defaults: &Option<PyTupleRef>,
404403
kw_only_defaults: &Option<PyDictRef>,
405-
args: PyFuncArgs,
404+
func_args: PyFuncArgs,
406405
) -> PyResult {
407-
let scope = scope.child_scope(&self.ctx);
406+
let scope = scope.new_child_scope(&self.ctx);
408407
self.fill_locals_from_args(
409408
&code.code,
410409
&scope.get_locals(),
411-
args,
410+
func_args,
412411
defaults,
413412
kw_only_defaults,
414413
)?;
@@ -432,8 +431,8 @@ impl VirtualMachine {
432431
) -> PyResult {
433432
if let Some(PyFunction { code, scope, .. }) = &function.payload() {
434433
let scope = scope
435-
.child_scope_with_locals(cells)
436-
.child_scope_with_locals(locals);
434+
.new_child_scope_with_locals(cells)
435+
.new_child_scope_with_locals(locals);
437436
let frame = Frame::new(code.clone(), scope).into_ref(self);
438437
return self.run_frame_full(frame);
439438
}
@@ -447,11 +446,11 @@ impl VirtualMachine {
447446
&self,
448447
code_object: &bytecode::CodeObject,
449448
locals: &PyDictRef,
450-
args: PyFuncArgs,
449+
func_args: PyFuncArgs,
451450
defaults: &Option<PyTupleRef>,
452451
kw_only_defaults: &Option<PyDictRef>,
453452
) -> PyResult<()> {
454-
let nargs = args.args.len();
453+
let nargs = func_args.args.len();
455454
let nexpected_args = code_object.arg_names.len();
456455

457456
// This parses the arguments from args and kwargs into
@@ -469,7 +468,7 @@ impl VirtualMachine {
469468
// Copy positional arguments into local variables
470469
for i in 0..n {
471470
let arg_name = &code_object.arg_names[i];
472-
let arg = &args.args[i];
471+
let arg = &func_args.args[i];
473472
locals.set_item(arg_name, arg.clone(), self)?;
474473
}
475474

@@ -478,7 +477,7 @@ impl VirtualMachine {
478477
bytecode::Varargs::Named(ref vararg_name) => {
479478
let mut last_args = vec![];
480479
for i in n..nargs {
481-
let arg = &args.args[i];
480+
let arg = &func_args.args[i];
482481
last_args.push(arg.clone());
483482
}
484483
let vararg_value = self.ctx.new_tuple(last_args);
@@ -508,7 +507,7 @@ impl VirtualMachine {
508507
};
509508

510509
// Handle keyword arguments
511-
for (name, value) in args.kwargs {
510+
for (name, value) in func_args.kwargs {
512511
// Check if we have a parameter with this name:
513512
if code_object.arg_names.contains(&name) || code_object.kwonlyarg_names.contains(&name)
514513
{

0 commit comments

Comments
 (0)