Skip to content
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

print traceback on failed import #537

Merged
merged 1 commit into from
Dec 1, 2016
Merged

Conversation

esquires
Copy link
Contributor

This PR prints a traceback when a python import fails due to an error in the imported file. The existing implementation will print out a message about the module not being found. Here is an example demonstrating the issue:

// example.cpp
#include <Python.h>
#include <pybind11/pybind11.h>

int main(int argc, char *argv[]) {
    Py_Initialize();
    pybind11::module::import(argv[1]);
    Py_Finalize();
    return 0;
}
# example.py
raise Exception('exception raised on import')

Before this PR:

build: ./ex example                                                                     
terminate called after throwing an instance of 'pybind11::import_error'
  what():  Module "example" not found!
[1]    3744 abort (core dumped)  ./ex example
build: ./ex module_that_doesnt_exist                                                   
terminate called after throwing an instance of 'pybind11::import_error'
  what():  Module "module_that_doesnt_exist" not found!
[1]    3766 abort (core dumped)  ./ex module_that_doesnt_exist

After this PR:

build: ./ex example                                                                 
terminate called after throwing an instance of 'pybind11::error_already_set'
  what():  Exception: exception raised on import

At:
  /home/esquires3/Documents/pybind/example.py(1): <module>

[1]    3650 abort (core dumped)  ./ex example
build: ./ex module_that_doesnt_exist                                                      
terminate called after throwing an instance of 'pybind11::error_already_set'
  what():  ImportError: No module named module_that_doesnt_exist
[1]    3664 abort (core dumped)  ./ex module_that_doesnt_exist

@wjakob
Copy link
Member

wjakob commented Dec 1, 2016

That looks good to me -- thanks!

@wjakob wjakob merged commit 67a68f1 into pybind:master Dec 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants