Skip to content

Remove importlib frames from traceback #1104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions vm/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use crate::bytecode::CodeObject;
use crate::frame::Scope;
use crate::obj::objcode;
use crate::pyobject::{ItemProtocol, PyResult, PyValue};
use crate::obj::{objcode, objsequence, objstr, objtype};
use crate::pyobject::{ItemProtocol, PyObjectRef, PyResult, PyValue};
use crate::vm::VirtualMachine;
#[cfg(feature = "rustpython-compiler")]
use rustpython_compiler::compile;
Expand Down Expand Up @@ -82,3 +82,41 @@ pub fn import_codeobj(
)?;
Ok(module)
}

// TODO: This function should do nothing on verbose mode.
pub fn remove_importlib_frames(vm: &VirtualMachine, exc: &PyObjectRef) -> PyObjectRef {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure you want to add this? This might be a good opportunity to keep the rustpython interpreter transparent to users, and show them what is actually going on. What is your opinion on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it gives too much information. I will add a TODO that when running on verbose this function will do nothing.

let always_trim = objtype::isinstance(exc, &vm.ctx.exceptions.import_error);

if let Ok(tb) = vm.get_attribute(exc.clone(), "__traceback__") {
if objtype::isinstance(&tb, &vm.ctx.list_type()) {
let tb_entries = objsequence::get_elements_list(&tb).to_vec();
let mut in_importlib = false;
let new_tb = tb_entries
.iter()
.filter(|tb_entry| {
let location_attrs = objsequence::get_elements_tuple(&tb_entry);
let file_name = objstr::get_value(&location_attrs[0]);
if file_name == "_frozen_importlib" || file_name == "_frozen_importlib_external"
{
let run_obj_name = objstr::get_value(&location_attrs[2]);
if run_obj_name == "_call_with_frames_removed" {
in_importlib = true;
}
if always_trim || in_importlib {
false
} else {
true
}
} else {
in_importlib = false;
true
}
})
.map(|x| x.clone())
.collect();
vm.set_attr(exc, "__traceback__", vm.ctx.new_list(new_tb))
.unwrap();
}
}
exc.clone()
}
50 changes: 27 additions & 23 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::bytecode;
use crate::frame::{ExecutionResult, Frame, FrameRef, Scope};
use crate::frozen;
use crate::function::PyFuncArgs;
use crate::import;
use crate::obj::objbool;
use crate::obj::objbuiltinfunc::PyBuiltinFunction;
use crate::obj::objcode::{PyCode, PyCodeRef};
Expand Down Expand Up @@ -305,30 +306,33 @@ impl VirtualMachine {

pub fn import(&self, module: &str, from_list: &PyObjectRef, level: usize) -> PyResult {
let sys_modules = self.get_attribute(self.sys_module.clone(), "modules")?;
sys_modules.get_item(module.to_string(), self).or_else(|_| {
let import_func = self
.get_attribute(self.builtins.clone(), "__import__")
.map_err(|_| self.new_import_error("__import__ not found".to_string()))?;

let (locals, globals) = if let Some(frame) = self.current_frame() {
(
frame.scope.get_locals().into_object(),
frame.scope.globals.clone().into_object(),
sys_modules
.get_item(module.to_string(), self)
.or_else(|_| {
let import_func = self
.get_attribute(self.builtins.clone(), "__import__")
.map_err(|_| self.new_import_error("__import__ not found".to_string()))?;

let (locals, globals) = if let Some(frame) = self.current_frame() {
(
frame.scope.get_locals().into_object(),
frame.scope.globals.clone().into_object(),
)
} else {
(self.get_none(), self.get_none())
};
self.invoke(
import_func,
vec![
self.ctx.new_str(module.to_string()),
globals,
locals,
from_list.clone(),
self.ctx.new_int(level),
],
)
} else {
(self.get_none(), self.get_none())
};
self.invoke(
import_func,
vec![
self.ctx.new_str(module.to_string()),
globals,
locals,
from_list.clone(),
self.ctx.new_int(level),
],
)
})
})
.map_err(|exc| import::remove_importlib_frames(self, &exc))
}

/// Determines if `obj` is an instance of `cls`, either directly, indirectly or virtually via
Expand Down