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

Add a method to check Python exception types #772

Merged
merged 2 commits into from
Apr 2, 2017
Merged

Add a method to check Python exception types #772

merged 2 commits into from
Apr 2, 2017

Conversation

romanvm
Copy link
Contributor

@romanvm romanvm commented Mar 31, 2017

This commit adds error_already_set::matches() convenience method to
check if the exception trapped by error_already_set matches a given
Python exception type. This will address #700 by providing a less
verbose way to check exceptions:

py::dict val;
try {
  val = d["key"];
} catch (py::error_already_set& exc) {
  if (exc.matches(PyExc_KeyError)) {
    exc.clear();
    return;
  } else {
    throw;
  }
}

This commit adds `error_already_set::matches()` convenience method to
check if the exception trapped by `error_already_set` matches a given
Python exception type. This will address #700 by providing a less
verbose way to check exceptions.
@dean0x7d
Copy link
Member

Looks pretty convenient! The only suggestion I have is to turn the example code into a test and add it to tests/test_exceptions.cpp.

@romanvm
Copy link
Contributor Author

romanvm commented Apr 1, 2017

Done.

@wjakob
Copy link
Member

wjakob commented Apr 1, 2017

LGTM

@dean0x7d dean0x7d merged commit 83a8a97 into pybind:master Apr 2, 2017
@dean0x7d
Copy link
Member

dean0x7d commented Apr 2, 2017

Merged. Thanks @romanvm!

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