Skip to content

Add name, path, msg attributes to ImportError #1103

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 1 commit into from
Jul 3, 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
20 changes: 20 additions & 0 deletions tests/snippets/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# KeyError
empty_exc = KeyError()
assert str(empty_exc) == ''
assert repr(empty_exc) == 'KeyError()'
Expand All @@ -23,3 +24,22 @@ def __str__(self):
exc = KeyError(A())
assert str(exc) == 'repr'
assert repr(exc) == 'KeyError(repr,)'

# ImportError / ModuleNotFoundError
exc = ImportError()
assert exc.name is None
assert exc.path is None
assert exc.msg is None
assert exc.args == ()

exc = ImportError('hello')
assert exc.name is None
assert exc.path is None
assert exc.msg == 'hello'
assert exc.args == ('hello',)

exc = ImportError('hello', name='name', path='path')
assert exc.name == 'name'
assert exc.path == 'path'
assert exc.msg == 'hello'
assert exc.args == ('hello',)
5 changes: 5 additions & 0 deletions tests/snippets/import.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
except ImportError:
pass

try:
import mymodule
except ModuleNotFoundError as exc:
assert exc.name == 'mymodule'


test = __import__("import_target")
assert test.X == import_target.X
Expand Down
34 changes: 34 additions & 0 deletions vm/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,35 @@ impl ExceptionZoo {
}
}

fn import_error_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
// TODO: call super().__init__(*args) instead
exception_init(vm, args.clone())?;

let exc_self = args.args[0].clone();
vm.set_attr(
&exc_self,
"name",
args.kwargs
.get("name")
.cloned()
.unwrap_or_else(|| vm.get_none()),
)?;
vm.set_attr(
&exc_self,
"path",
args.kwargs
.get("path")
.cloned()
.unwrap_or_else(|| vm.get_none()),
)?;
vm.set_attr(
&exc_self,
"msg",
args.args.get(1).cloned().unwrap_or_else(|| vm.get_none()),
)?;
Ok(vm.get_none())
}

pub fn init(context: &PyContext) {
let base_exception_type = &context.exceptions.base_exception_type;
extend_class!(context, base_exception_type, {
Expand All @@ -323,4 +352,9 @@ pub fn init(context: &PyContext) {
"__str__" => context.new_rustfunc(exception_str),
"__repr__" => context.new_rustfunc(exception_repr),
});

let import_error_type = &context.exceptions.import_error;
extend_class!(context, import_error_type, {
"__init__" => context.new_rustfunc(import_error_init)
});
}