Skip to content

[BUGFIX] Fixing pybind11::error_already_set.matches to also work with exception subclasses #1715

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 2 commits into from
May 12, 2019

Conversation

YannickJadoul
Copy link
Collaborator

The error_already_set.matches implementation (introduced in #772) forwards the two exception types in the wrong order as arguments to PyErr_GivenExceptionMatches. This PR fixes that (and renames the parameter/argument from ex to exc to explicitly match the Python documentation).

The reason this bug didn't cause a lot of harm and wasn't notice before is that the problem is only noticeable when matching to an exception base class (otherwise, the order of the arguments does not end up mattering).

This demonstrates the error, as ModuleNotFound error is a subclass of ImportError in Python >= 3.6:

try {
    return py::module::import("nonexistent");
}
catch (py::error_already_set &e) {
    py::print(e.matches(PyExc_ModuleNotFoundError)); // == True
    py::print(e.matches(PyExc_ImportError)); // == False
}

@bstaletic
Copy link
Collaborator

Can you make that example that shows the error a test case, so that we have a test that fails without this PR, but works with it?

@YannickJadoul
Copy link
Collaborator Author

Good point; will do!

@YannickJadoul YannickJadoul force-pushed the fix-error_already_set-matches branch from 5664010 to b3e190c Compare May 12, 2019 13:01
@YannickJadoul
Copy link
Collaborator Author

@bstaletic I already rebased. Any idea why that the docs fail to build? Something seems wrong with sphinx/doxygen/breathe?

@bstaletic
Copy link
Collaborator

Exception occurred:
  File "/home/travis/.local/lib/python3.6/site-packages/breathe/renderer/sphinxrenderer.py", line 47, in parse_definition
    ast = parser.parse_declaration("class", "class")
TypeError: parse_declaration() takes 2 positional arguments but 3 were given

I'm guessing some python library got updated and broke things...

@bstaletic
Copy link
Collaborator

Aha! breathe-doc/breathe#411 broke the build because pybind requires "sphinx<2.0", but allows for the latest breathe.

Two options:

  • Try with the latest sphinx
  • Limit the version of breathe

@YannickJadoul
Copy link
Collaborator Author

Great; thanks for figuring that out. But then it's got nothing to do with this PR, luckily :-)

  • Try with the latest sphinx

I think that should work, since .travis.yml currently says # Breathe does not yet support Sphinx 2. Should we (or you, since you figured it out) turn that into a small PR to fix 'master'?

@YannickJadoul YannickJadoul force-pushed the fix-error_already_set-matches branch from b3e190c to 1e0ec0e Compare May 12, 2019 18:19
@YannickJadoul
Copy link
Collaborator Author

Done, I think; let's see if all tests now work.

I also had to fix the old test on error_already_set::matches because it never actually entered the catch block where the functionality was tested.

@YannickJadoul
Copy link
Collaborator Author

All green! Thanks for the CI investigation and fix, @bstaletic!

@wjakob, if you!d be thinking about a new patch release, this one might be interesting?

@wjakob
Copy link
Member

wjakob commented May 12, 2019

Nice catch -- merged!

@wjakob wjakob merged commit 97784da into pybind:master May 12, 2019
@YannickJadoul YannickJadoul deleted the fix-error_already_set-matches branch May 12, 2019 22:34
tree-copybara pushed a commit to google-deepmind/tree that referenced this pull request Jan 31, 2020
ImportError is caused by a bug in an older pybind11 version which does
not allow exception subclasses in error_already_set::matches.
See pybind/pybind11#1715.

PiperOrigin-RevId: 292511528
Change-Id: I494fd029728b304ab1add94713e01544c20db6fe
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.

3 participants