Skip to content

Commit 96ee1cc

Browse files
committed
Add filename and row to traceback
1 parent 71896fd commit 96ee1cc

4 files changed

Lines changed: 30 additions & 10 deletions

File tree

parser/src/lexer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub enum LexicalError {
2020

2121
#[derive(Clone, Debug, Default, PartialEq)]
2222
pub struct Location {
23-
row: usize,
24-
column: usize,
23+
pub row: usize,
24+
pub column: usize,
2525
}
2626

2727
impl Location {

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ fn _run_string(source: &String, source_path: Option<String>) {
5858
match vm.run_code_obj(code_obj, vars) {
5959
Ok(_value) => {}
6060
Err(exc) => {
61+
// println!("X: {:?}", exc.get_attr("__traceback__"));
6162
panic!("Exception: {:?}", exc);
6263
}
6364
}

vm/src/objlist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn set_item(
2424
}
2525
}
2626

27-
fn append(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
27+
pub fn append(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
2828
trace!("list.append called with: {:?}", args);
2929
if args.args.len() == 2 {
3030
let l = args.args[0].clone();

vm/src/vm.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,21 @@ impl VirtualMachine {
219219
} else if scope.has_parent() {
220220
scope = scope.get_parent();
221221
} else {
222-
let name_error = self.context().new_instance(
223-
self.context().new_dict(),
224-
self.context().exceptions.name_error.clone(),
225-
);
222+
let name_error_type = self.ctx.exceptions.name_error.clone();
223+
let msg = format!("Has not attribute '{}'", name);
224+
let name_error = self.new_exception(name_error_type, msg);
226225
break Some(Err(name_error));
227226
}
228227
}
229228
}
230229

231230
fn run_frame(&mut self, frame: Frame) -> PyResult {
232231
self.frames.push(frame);
232+
let filename = if let Some(source_path) = &self.current_frame().code.source_path {
233+
source_path.to_string()
234+
} else {
235+
"<unknown>".to_string()
236+
};
233237

234238
// Execute until return or exception:
235239
let value = loop {
@@ -243,9 +247,24 @@ impl VirtualMachine {
243247
Some(Err(exception)) => {
244248
// unwind block stack on exception and find any handlers.
245249
// Add an entry in the traceback:
246-
let traceback =
247-
self.get_attribute(exception.clone(), &"__traceback__".to_string());
250+
assert!(objtype::isinstance(
251+
exception.clone(),
252+
self.ctx.exceptions.base_exception_type.clone()
253+
));
254+
let traceback = self
255+
.get_attribute(exception.clone(), &"__traceback__".to_string())
256+
.unwrap();
248257
trace!("Adding to traceback: {:?} {:?}", traceback, lineno);
258+
let pos = self.ctx.new_tuple(vec![
259+
self.ctx.new_str(filename.clone()),
260+
self.ctx.new_int(lineno.row as i32),
261+
]);
262+
objlist::append(
263+
self,
264+
PyFuncArgs {
265+
args: vec![traceback, pos],
266+
},
267+
).unwrap();
249268
// exception.__trace
250269
match self.unwind_exception(exception) {
251270
None => {}
@@ -901,7 +920,7 @@ impl VirtualMachine {
901920
current_frame.lasti = target_pc;
902921
}
903922

904-
fn get_lineno(&mut self) -> ast::Location {
923+
fn get_lineno(&self) -> ast::Location {
905924
self.current_frame().get_lineno()
906925
}
907926
}

0 commit comments

Comments
 (0)