+/* A wrapper for PyErr_Fetch that handles reference counting for the
+ caller. */
+class gdbpy_err_fetch
+{
+public:
+
+ gdbpy_err_fetch ()
+ {
+ PyErr_Fetch (&m_error_type, &m_error_value, &m_error_traceback);
+ }
+
+ ~gdbpy_err_fetch ()
+ {
+ Py_XDECREF (m_error_type);
+ Py_XDECREF (m_error_value);
+ Py_XDECREF (m_error_traceback);
+ }
+
+ /* Call PyErr_Restore using the values stashed in this object.
+ After this call, this object is invalid and neither the to_string
+ nor restore methods may be used again. */
+
+ void restore ()
+ {
+ PyErr_Restore (m_error_type, m_error_value, m_error_traceback);
+ m_error_type = nullptr;
+ m_error_value = nullptr;
+ m_error_traceback = nullptr;
+ }
+
+ /* Return the string representation of the exception represented by
+ this object. If the result is NULL a python error occurred, the
+ caller must clear it. */
+
+ gdb::unique_xmalloc_ptr<char> to_string () const;
+
+ /* Return the string representation of the type of the exception
+ represented by this object. If the result is NULL a python error
+ occurred, the caller must clear it. */
+
+ gdb::unique_xmalloc_ptr<char> type_to_string () const;
+
+ /* Return true if the stored type matches TYPE, false otherwise. */
+
+ bool type_matches (PyObject *type) const
+ {
+ return PyErr_GivenExceptionMatches (m_error_type, type);
+ }
+
+private:
+
+ PyObject *m_error_type, *m_error_value, *m_error_traceback;
+};
+