+/* 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;
+};
+
+/* Called before entering the Python interpreter to install the
+ current language and architecture to be used for Python values.
+ Also set the active extension language for GDB so that SIGINT's
+ are directed our way, and if necessary install the right SIGINT
+ handler. */
+class gdbpy_enter
+{
+ public:
+
+ /* Set the ambient Python architecture to GDBARCH and the language
+ to LANGUAGE. If GDBARCH is nullptr, then the architecture will
+ be computed, when needed, using get_current_arch; see the
+ get_gdbarch method. If LANGUAGE is not nullptr, then the current
+ language at time of construction will be saved (to be restored on
+ destruction), and the current language will be set to
+ LANGUAGE. */
+ explicit gdbpy_enter (struct gdbarch *gdbarch = nullptr,
+ const struct language_defn *language = nullptr);
+
+ ~gdbpy_enter ();
+
+ DISABLE_COPY_AND_ASSIGN (gdbpy_enter);
+
+ /* Return the current gdbarch, as known to the Python layer. This
+ is either python_gdbarch (which comes from the most recent call
+ to the gdbpy_enter constructor), or, if that is nullptr, the
+ result of get_current_arch. */
+ static struct gdbarch *get_gdbarch ();
+
+ /* Called only during gdb shutdown. This sets python_gdbarch to an
+ acceptable value. */
+ static void finalize ();
+
+ private:
+
+ /* The current gdbarch, according to Python. This can be
+ nullptr. */
+ static struct gdbarch *python_gdbarch;
+
+ struct active_ext_lang_state *m_previous_active;
+ PyGILState_STATE m_state;
+ struct gdbarch *m_gdbarch;
+ const struct language_defn *m_language;
+
+ /* An optional is used here because we don't want to call
+ PyErr_Fetch too early. */
+ gdb::optional<gdbpy_err_fetch> m_error;
+};
+
+/* Like gdbpy_enter, but takes a varobj. This is a subclass just to
+ make constructor delegation a little nicer. */
+class gdbpy_enter_varobj : public gdbpy_enter
+{
+ public:
+
+ /* This is defined in varobj.c, where it can access varobj
+ internals. */
+ gdbpy_enter_varobj (const struct varobj *var);
+
+};
+
+/* The opposite of gdb_enter: this releases the GIL around a region,
+ allowing other Python threads to run. No Python APIs may be used
+ while this is active. */
+class gdbpy_allow_threads
+{
+public:
+
+ gdbpy_allow_threads ()
+ : m_save (PyEval_SaveThread ())
+ {
+ gdb_assert (m_save != nullptr);
+ }