mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-12 01:10:34 +00:00
Add a method to check Python exception types (#772)
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.
This commit is contained in:
committed by
Dean Moldovan
parent
37ef74c584
commit
83a8a977a7
@@ -597,6 +597,9 @@ public:
|
||||
/// Clear the held Python error state (the C++ `what()` message remains intact)
|
||||
void clear() { restore(); PyErr_Clear(); }
|
||||
|
||||
/// Check if the trapped exception matches a given Python exception class
|
||||
bool matches(PyObject *ex) const { return PyErr_GivenExceptionMatches(ex, type); }
|
||||
|
||||
private:
|
||||
PyObject *type, *value, *trace;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user