Skip to content

Handling errors inside BOOST_PYTHON_MODULE() #280

Description

@db4

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.

Activity

  1. stefanseefeld commented on Dec 9, 2019

    @stefanseefeld
    Member

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

  2. db4 commented on Dec 9, 2019

    @db4
    ContributorAuthor

    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.

  3. stefanseefeld commented on Dec 9, 2019

    @stefanseefeld
    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 ?

  4. db4 commented on Dec 9, 2019

    @db4
    ContributorAuthor

    Are you sure that's a Python problem? Maybe

    handle_exception(init_function);

    should be replaced with

    if (handle_exception(init_function)) return NULL;

    ?

  5. stefanseefeld commented on Dec 9, 2019

    @stefanseefeld
    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 !

  6. db4 commented on Dec 9, 2019

    @db4
    ContributorAuthor

    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...

  7. stefanseefeld commented on Dec 9, 2019

    @stefanseefeld
    Member

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

  8. db4 commented on Jun 29, 2020

    @db4
    ContributorAuthor

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

  9. tdelame commented on Nov 20, 2020

    @tdelame

    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.

  10. twvd commented on Feb 2, 2021

    @twvd

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

  11. db4 commented on Feb 3, 2021

    @db4
    ContributorAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions