Introduce gdbpy_enter
This introduces gdbpy_enter, a class that can be used to acquire and release the Python GIL, and also set other Python-related globals used by gdb. ensure_python_env is rewritten in terms of this new class. 2017-01-10 Tom Tromey <tom@tromey.com> * python/python.c (gdbpy_enter): New constructor. (~gdbpy_enter): New destructor. (restore_python_env, ensure_python_env): Rewrite. * python/python-internal.h (gdbpy_enter): New class.
This commit is contained in:
parent
37fce74fb4
commit
4ecee2c47d
3 changed files with 64 additions and 37 deletions
|
@ -1,3 +1,10 @@
|
||||||
|
2017-01-10 Tom Tromey <tom@tromey.com>
|
||||||
|
|
||||||
|
* python/python.c (gdbpy_enter): New constructor.
|
||||||
|
(~gdbpy_enter): New destructor.
|
||||||
|
(restore_python_env, ensure_python_env): Rewrite.
|
||||||
|
* python/python-internal.h (gdbpy_enter): New class.
|
||||||
|
|
||||||
2017-01-10 Tom Tromey <tom@tromey.com>
|
2017-01-10 Tom Tromey <tom@tromey.com>
|
||||||
|
|
||||||
* python/py-symbol.c (gdbpy_lookup_symbol): Use gdbpy_ref.
|
* python/py-symbol.c (gdbpy_lookup_symbol): Use gdbpy_ref.
|
||||||
|
|
|
@ -501,6 +501,31 @@ int gdbpy_initialize_unwind (void)
|
||||||
struct cleanup *make_cleanup_py_decref (PyObject *py);
|
struct cleanup *make_cleanup_py_decref (PyObject *py);
|
||||||
struct cleanup *make_cleanup_py_xdecref (PyObject *py);
|
struct cleanup *make_cleanup_py_xdecref (PyObject *py);
|
||||||
|
|
||||||
|
/* 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:
|
||||||
|
|
||||||
|
gdbpy_enter (struct gdbarch *gdbarch, const struct language_defn *language);
|
||||||
|
|
||||||
|
~gdbpy_enter ();
|
||||||
|
|
||||||
|
gdbpy_enter (const gdbpy_enter &) = delete;
|
||||||
|
gdbpy_enter &operator= (const gdbpy_enter &) = delete;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
struct active_ext_lang_state *m_previous_active;
|
||||||
|
PyGILState_STATE m_state;
|
||||||
|
struct gdbarch *m_gdbarch;
|
||||||
|
const struct language_defn *m_language;
|
||||||
|
PyObject *m_error_type, *m_error_value, *m_error_traceback;
|
||||||
|
};
|
||||||
|
|
||||||
struct cleanup *ensure_python_env (struct gdbarch *gdbarch,
|
struct cleanup *ensure_python_env (struct gdbarch *gdbarch,
|
||||||
const struct language_defn *language);
|
const struct language_defn *language);
|
||||||
|
|
||||||
|
|
|
@ -202,23 +202,28 @@ const struct extension_language_ops python_extension_ops =
|
||||||
struct gdbarch *python_gdbarch;
|
struct gdbarch *python_gdbarch;
|
||||||
const struct language_defn *python_language;
|
const struct language_defn *python_language;
|
||||||
|
|
||||||
/* Restore global language and architecture and Python GIL state
|
gdbpy_enter::gdbpy_enter (struct gdbarch *gdbarch,
|
||||||
when leaving the Python interpreter. */
|
const struct language_defn *language)
|
||||||
|
: m_gdbarch (python_gdbarch),
|
||||||
struct python_env
|
m_language (python_language)
|
||||||
{
|
{
|
||||||
struct active_ext_lang_state *previous_active;
|
/* We should not ever enter Python unless initialized. */
|
||||||
PyGILState_STATE state;
|
if (!gdb_python_initialized)
|
||||||
struct gdbarch *gdbarch;
|
error (_("Python not initialized"));
|
||||||
const struct language_defn *language;
|
|
||||||
PyObject *error_type, *error_value, *error_traceback;
|
|
||||||
};
|
|
||||||
|
|
||||||
static void
|
m_previous_active = set_active_ext_lang (&extension_language_python);
|
||||||
restore_python_env (void *p)
|
|
||||||
|
m_state = PyGILState_Ensure ();
|
||||||
|
|
||||||
|
python_gdbarch = gdbarch;
|
||||||
|
python_language = language;
|
||||||
|
|
||||||
|
/* Save it and ensure ! PyErr_Occurred () afterwards. */
|
||||||
|
PyErr_Fetch (&m_error_type, &m_error_value, &m_error_traceback);
|
||||||
|
}
|
||||||
|
|
||||||
|
gdbpy_enter::~gdbpy_enter ()
|
||||||
{
|
{
|
||||||
struct python_env *env = (struct python_env *)p;
|
|
||||||
|
|
||||||
/* Leftover Python error is forbidden by Python Exception Handling. */
|
/* Leftover Python error is forbidden by Python Exception Handling. */
|
||||||
if (PyErr_Occurred ())
|
if (PyErr_Occurred ())
|
||||||
{
|
{
|
||||||
|
@ -227,15 +232,21 @@ restore_python_env (void *p)
|
||||||
warning (_("internal error: Unhandled Python exception"));
|
warning (_("internal error: Unhandled Python exception"));
|
||||||
}
|
}
|
||||||
|
|
||||||
PyErr_Restore (env->error_type, env->error_value, env->error_traceback);
|
PyErr_Restore (m_error_type, m_error_value, m_error_traceback);
|
||||||
|
|
||||||
PyGILState_Release (env->state);
|
PyGILState_Release (m_state);
|
||||||
python_gdbarch = env->gdbarch;
|
python_gdbarch = m_gdbarch;
|
||||||
python_language = env->language;
|
python_language = m_language;
|
||||||
|
|
||||||
restore_active_ext_lang (env->previous_active);
|
restore_active_ext_lang (m_previous_active);
|
||||||
|
}
|
||||||
|
|
||||||
xfree (env);
|
static void
|
||||||
|
restore_python_env (void *p)
|
||||||
|
{
|
||||||
|
gdbpy_enter *env = (gdbpy_enter *) p;
|
||||||
|
|
||||||
|
delete env;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Called before entering the Python interpreter to install the
|
/* Called before entering the Python interpreter to install the
|
||||||
|
@ -248,23 +259,7 @@ struct cleanup *
|
||||||
ensure_python_env (struct gdbarch *gdbarch,
|
ensure_python_env (struct gdbarch *gdbarch,
|
||||||
const struct language_defn *language)
|
const struct language_defn *language)
|
||||||
{
|
{
|
||||||
struct python_env *env = XNEW (struct python_env);
|
gdbpy_enter *env = new gdbpy_enter (gdbarch, language);
|
||||||
|
|
||||||
/* We should not ever enter Python unless initialized. */
|
|
||||||
if (!gdb_python_initialized)
|
|
||||||
error (_("Python not initialized"));
|
|
||||||
|
|
||||||
env->previous_active = set_active_ext_lang (&extension_language_python);
|
|
||||||
|
|
||||||
env->state = PyGILState_Ensure ();
|
|
||||||
env->gdbarch = python_gdbarch;
|
|
||||||
env->language = python_language;
|
|
||||||
|
|
||||||
python_gdbarch = gdbarch;
|
|
||||||
python_language = language;
|
|
||||||
|
|
||||||
/* Save it and ensure ! PyErr_Occurred () afterwards. */
|
|
||||||
PyErr_Fetch (&env->error_type, &env->error_value, &env->error_traceback);
|
|
||||||
|
|
||||||
return make_cleanup (restore_python_env, env);
|
return make_cleanup (restore_python_env, env);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue