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

Handling errors inside BOOST_PYTHON_MODULE() #280

Closed
db4 opened this issue Dec 9, 2019 · 11 comments · Fixed by #351
Closed

Handling errors inside BOOST_PYTHON_MODULE() #280

db4 opened this issue Dec 9, 2019 · 11 comments · Fixed by #351

Comments

@db4
Copy link
Contributor

db4 commented Dec 9, 2019

I have a module that may fail to initialize due to missing dependencies. So I do

BOOST_PYTHON_MODULE(mymodule)
{
...
    if (missing_dependency) {
        throw std::exception("Missing dependency");
    }
...
}

The exception is correctly caught and PyErr_SetString(PyExc_RuntimeError, x.what()) is called, but python reports

    import mymodule
SystemError: initialization of mymodule raised unreported exception

Why unreported exception? My platform is Windows 10/Python 3.7.4 x64/MSVC 2017 if that matters. python_boost version is 1.69.0.

@stefanseefeld
Copy link
Member

What happens if you replace std::exception by std::runtime_error or some other more specific exception type ?

@db4
Copy link
Contributor Author

db4 commented Dec 9, 2019

No, switching to std::runtime_error does not help, I still have unreported exception. And looks like the exception type has nothing to do with the issue: I set a debug breakpoint in

python/src/errors.cpp

Lines 48 to 51 in ac62db1

catch(const std::exception& x)
{
PyErr_SetString(PyExc_RuntimeError, x.what());
}

and verified that this catch case was executed.

@stefanseefeld
Copy link
Member

OK, I see. So the Python error has correctly been set by Boost.Python, but Python's own runtime then flags this as an "unreported exception". In other words, you could strip your code off all Boost.Python references, only using Python's own C API, and you'd still observe the issue.
Can you report this to the Python bug tracker ?

@db4
Copy link
Contributor Author

db4 commented Dec 9, 2019

Are you sure that's a Python problem? Maybe

handle_exception(init_function);

should be replaced with

if (handle_exception(init_function)) return NULL;

?

@stefanseefeld
Copy link
Member

Oh, indeed, that looks sensible. Can you write a (minimal) test case to demonstrate the problem as well as that your proposed change fixes it ? That would make a great PR !

@db4
Copy link
Contributor Author

db4 commented Dec 9, 2019

I just tried to fix it locally this way and it solved the problem. Well, I could try to create a test case and PR, but I'm quite new to boost libs testing infrastructure...

@stefanseefeld
Copy link
Member

OK, great. I'll see whether I can write a quick test.

@db4
Copy link
Contributor Author

db4 commented Jun 29, 2020

Hi @stefanseefeld
Any news on the issue? Are you still planning to create a test and finally commit the fix?

@tdelame
Copy link

tdelame commented Nov 20, 2020

Had the same issue, boost::python was hiding the real exception with raised unreported exception. I applied the patch if (handle_exception(init_function)) return NULL; mentioned by @db4 and now the real exception is thrown.

@twvd
Copy link

twvd commented Feb 2, 2021

Also just ran into this problem.. Just bumping this issue to show it's still relevant and affects users.

db4 added a commit to db4/python that referenced this issue Feb 3, 2021
db4 added a commit to db4/python that referenced this issue Feb 3, 2021
@db4
Copy link
Contributor Author

db4 commented Feb 3, 2021

Also just ran into this problem.. Just bumping this issue to show it's still relevant and affects users.

I finally decided to stop waiting for something and create PR myself

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 a pull request may close this issue.

4 participants