Skip to content

Commit 3478251

Browse files
Merge remote-tracking branch 'origin/master' into joey/fun-with-functions
Conflicts: vm/src/lib.rs vm/src/pyobject.rs wasm/lib/src/vm_class.rs
2 parents a70f251 + 7bb6f8f commit 3478251

29 files changed

+562
-530
lines changed

parser/src/fstring.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl<'a> FStringParser<'a> {
121121
}
122122
}
123123

124-
return Err(UnclosedLbrace);
124+
Err(UnclosedLbrace)
125125
}
126126

127127
fn parse(mut self) -> Result<StringGroup, FStringError> {

src/main.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ use rustpython_parser::error::ParseError;
1212
use rustpython_vm::{
1313
compile,
1414
error::CompileError,
15+
frame::ScopeRef,
1516
import,
1617
obj::objstr,
1718
print_exception,
18-
pyobject::{AttributeProtocol, PyObjectRef, PyResult},
19+
pyobject::{AttributeProtocol, PyResult},
1920
util, VirtualMachine,
2021
};
2122
use rustyline::{error::ReadlineError, Editor};
@@ -120,11 +121,7 @@ fn run_script(vm: &mut VirtualMachine, script_file: &str) -> PyResult {
120121
}
121122
}
122123

123-
fn shell_exec(
124-
vm: &mut VirtualMachine,
125-
source: &str,
126-
scope: PyObjectRef,
127-
) -> Result<(), CompileError> {
124+
fn shell_exec(vm: &mut VirtualMachine, source: &str, scope: ScopeRef) -> Result<(), CompileError> {
128125
match compile::compile(
129126
source,
130127
&compile::Mode::Single,

tests/snippets/try_exceptions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88
# print(ex.__traceback__)
99
# print(type(ex.__traceback__))
1010

11+
try:
12+
raise ZeroDivisionError
13+
except ZeroDivisionError as ex:
14+
pass
15+
16+
class E(Exception):
17+
def __init__(self):
18+
asdf
19+
20+
try:
21+
raise E
22+
except NameError as ex:
23+
pass
1124

1225
l = []
1326
try:

vm/src/builtins.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! Implements functions listed here: https://docs.python.org/3/library/builtins.html
44
55
// use std::ops::Deref;
6-
use std::cell::RefCell;
76
use std::char;
87
use std::io::{self, Write};
98

@@ -15,10 +14,11 @@ use crate::obj::objiter;
1514
use crate::obj::objstr;
1615
use crate::obj::objtype;
1716

17+
use crate::frame::{Scope, ScopeRef};
1818
use crate::pyobject::{
19-
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObject, PyObjectPayload, PyObjectRef,
20-
PyResult, Scope, TypeProtocol,
19+
AttributeProtocol, IdProtocol, PyContext, PyFuncArgs, PyObjectRef, PyResult, TypeProtocol,
2120
};
21+
use std::rc::Rc;
2222

2323
#[cfg(not(target_arch = "wasm32"))]
2424
use crate::stdlib::io::io_open;
@@ -258,7 +258,7 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
258258
vm.run_code_obj(code_obj, scope)
259259
}
260260

261-
fn make_scope(vm: &mut VirtualMachine, locals: Option<&PyObjectRef>) -> PyObjectRef {
261+
fn make_scope(vm: &mut VirtualMachine, locals: Option<&PyObjectRef>) -> ScopeRef {
262262
// handle optional global and locals
263263
let locals = if let Some(locals) = locals {
264264
locals.clone()
@@ -268,18 +268,10 @@ fn make_scope(vm: &mut VirtualMachine, locals: Option<&PyObjectRef>) -> PyObject
268268

269269
// TODO: handle optional globals
270270
// Construct new scope:
271-
let scope_inner = Scope {
271+
Rc::new(Scope {
272272
locals,
273273
parent: None,
274-
};
275-
276-
PyObject {
277-
payload: PyObjectPayload::Scope {
278-
scope: RefCell::new(scope_inner),
279-
},
280-
typ: None,
281-
}
282-
.into_ref()
274+
})
283275
}
284276

285277
fn builtin_format(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
@@ -842,13 +834,7 @@ pub fn builtin_build_class_(vm: &mut VirtualMachine, mut args: PyFuncArgs) -> Py
842834
},
843835
)?;
844836

845-
vm.invoke(
846-
function,
847-
PyFuncArgs {
848-
args: vec![namespace.clone()],
849-
kwargs: vec![],
850-
},
851-
)?;
837+
vm.invoke_with_locals(function, namespace.clone())?;
852838

853839
vm.call_method(&metaclass, "__call__", vec![name_arg, bases, namespace])
854840
}

vm/src/bytecode.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ pub enum Instruction {
159159
},
160160
PrintExpr,
161161
LoadBuildClass,
162-
StoreLocals,
163162
UnpackSequence {
164163
size: usize,
165164
},
@@ -191,7 +190,7 @@ pub enum Constant {
191190
Boolean { value: bool },
192191
String { value: String },
193192
Bytes { value: Vec<u8> },
194-
Code { code: CodeObject },
193+
Code { code: Box<CodeObject> },
195194
Tuple { elements: Vec<Constant> },
196195
None,
197196
}
@@ -269,7 +268,7 @@ impl CodeObject {
269268
}
270269
}
271270

272-
pub fn get_constants<'a>(&'a self) -> impl Iterator<Item = &'a Constant> {
271+
pub fn get_constants(&self) -> impl Iterator<Item = &Constant> {
273272
self.instructions.iter().filter_map(|x| {
274273
if let Instruction::LoadConst { value } = x {
275274
Some(value)
@@ -358,14 +357,10 @@ impl Instruction {
358357
MapAdd { i } => w!(MapAdd, i),
359358
PrintExpr => w!(PrintExpr),
360359
LoadBuildClass => w!(LoadBuildClass),
361-
StoreLocals => w!(StoreLocals),
362360
UnpackSequence { size } => w!(UnpackSequence, size),
363361
UnpackEx { before, after } => w!(UnpackEx, before, after),
364362
Unpack => w!(Unpack),
365-
FormatValue {
366-
conversion: _,
367-
spec,
368-
} => w!(FormatValue, spec), // TODO: write conversion
363+
FormatValue { spec, .. } => w!(FormatValue, spec), // TODO: write conversion
369364
}
370365
}
371366
}

0 commit comments

Comments
 (0)