Use gdbpy_enter in python.c

This changes the last functions in python.c to use gdbpy_enter.  I
split gdbpy_finish_initialization into two functions in order to avoid
some "goto"s.

2017-01-10  Tom Tromey  <tom@tromey.com>

	* python/python.c (python_command): Use gdbpy_enter, gdbpy_ref.
	(do_finish_initialization): New function.  Use gdbpy_ref.
	(gdbpy_finish_initialization): Use gdbpy_enter.  Call
	do_finish_initialization.
This commit is contained in:
Tom Tromey 2016-11-12 11:32:05 -07:00
parent 2865bfce38
commit a7785f8c79
2 changed files with 44 additions and 45 deletions

View File

@ -1,3 +1,10 @@
2017-01-10 Tom Tromey <tom@tromey.com>
* python/python.c (python_command): Use gdbpy_enter, gdbpy_ref.
(do_finish_initialization): New function. Use gdbpy_ref.
(gdbpy_finish_initialization): Use gdbpy_enter. Call
do_finish_initialization.
2017-01-10 Tom Tromey <tom@tromey.com> 2017-01-10 Tom Tromey <tom@tromey.com>
* python/py-param.c (get_set_value, get_show_value): Use * python/py-param.c (get_set_value, get_show_value): Use

View File

@ -452,9 +452,7 @@ gdbpy_eval_from_control_command (const struct extension_language_defn *extlang,
static void static void
python_command (char *arg, int from_tty) python_command (char *arg, int from_tty)
{ {
struct cleanup *cleanup; gdbpy_enter enter_py (get_current_arch (), current_language);
cleanup = ensure_python_env (get_current_arch (), current_language);
scoped_restore save_async = make_scoped_restore (&current_ui->async, 0); scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
@ -467,12 +465,11 @@ python_command (char *arg, int from_tty)
else else
{ {
struct command_line *l = get_command_line (python_control, ""); struct command_line *l = get_command_line (python_control, "");
struct cleanup *cleanup = make_cleanup_free_command_lines (&l);
make_cleanup_free_command_lines (&l);
execute_control_command_untraced (l); execute_control_command_untraced (l);
do_cleanups (cleanup);
} }
do_cleanups (cleanup);
} }
@ -1819,26 +1816,20 @@ message == an error message without a stack will be printed."),
#ifdef HAVE_PYTHON #ifdef HAVE_PYTHON
/* Perform the remaining python initializations. /* Helper function for gdbpy_finish_initialization. This does the
These must be done after GDB is at least mostly initialized. work and then returns false if an error has occurred and must be
E.g., The "info pretty-printer" command needs the "info" prefix displayed, or true on success. */
command installed.
This is the extension_language_ops.finish_initialization "method". */
static void static bool
gdbpy_finish_initialization (const struct extension_language_defn *extlang) do_finish_initialization (const struct extension_language_defn *extlang)
{ {
PyObject *m; PyObject *m;
char *gdb_pythondir;
PyObject *sys_path; PyObject *sys_path;
struct cleanup *cleanup;
cleanup = ensure_python_env (get_current_arch (), current_language);
/* Add the initial data-directory to sys.path. */ /* Add the initial data-directory to sys.path. */
gdb_pythondir = concat (gdb_datadir, SLASH_STRING, "python", (char *) NULL); std::string gdb_pythondir = (std::string (gdb_datadir) + SLASH_STRING
make_cleanup (xfree, gdb_pythondir); + "python");
sys_path = PySys_GetObject ("path"); sys_path = PySys_GetObject ("path");
@ -1854,27 +1845,21 @@ gdbpy_finish_initialization (const struct extension_language_defn *extlang)
} }
if (sys_path && PyList_Check (sys_path)) if (sys_path && PyList_Check (sys_path))
{ {
PyObject *pythondir; gdbpy_ref pythondir (PyString_FromString (gdb_pythondir.c_str ()));
int err; if (pythondir == NULL || PyList_Insert (sys_path, 0, pythondir.get ()))
return false;
pythondir = PyString_FromString (gdb_pythondir);
if (pythondir == NULL)
goto fail;
err = PyList_Insert (sys_path, 0, pythondir);
Py_DECREF (pythondir);
if (err)
goto fail;
} }
else else
goto fail; return false;
/* Import the gdb module to finish the initialization, and /* Import the gdb module to finish the initialization, and
add it to __main__ for convenience. */ add it to __main__ for convenience. */
m = PyImport_AddModule ("__main__"); m = PyImport_AddModule ("__main__");
if (m == NULL) if (m == NULL)
goto fail; return false;
/* Keep the reference to gdb_python_module since it is in a global
variable. */
gdb_python_module = PyImport_ImportModule ("gdb"); gdb_python_module = PyImport_ImportModule ("gdb");
if (gdb_python_module == NULL) if (gdb_python_module == NULL)
{ {
@ -1885,24 +1870,31 @@ gdbpy_finish_initialization (const struct extension_language_defn *extlang)
"Could not load the Python gdb module from `%s'.\n" "Could not load the Python gdb module from `%s'.\n"
"Limited Python support is available from the _gdb module.\n" "Limited Python support is available from the _gdb module.\n"
"Suggest passing --data-directory=/path/to/gdb/data-directory.\n"), "Suggest passing --data-directory=/path/to/gdb/data-directory.\n"),
gdb_pythondir); gdb_pythondir.c_str ());
do_cleanups (cleanup); /* We return "success" here as we've already emitted the
return; warning. */
return true;
} }
if (gdb_pymodule_addobject (m, "gdb", gdb_python_module) < 0) return gdb_pymodule_addobject (m, "gdb", gdb_python_module) >= 0;
goto fail; }
/* Keep the reference to gdb_python_module since it is in a global /* Perform the remaining python initializations.
variable. */ These must be done after GDB is at least mostly initialized.
E.g., The "info pretty-printer" command needs the "info" prefix
command installed.
This is the extension_language_ops.finish_initialization "method". */
do_cleanups (cleanup); static void
return; gdbpy_finish_initialization (const struct extension_language_defn *extlang)
{
gdbpy_enter enter_py (get_current_arch (), current_language);
fail: if (!do_finish_initialization (extlang))
gdbpy_print_stack (); {
warning (_("internal error: Unhandled Python exception")); gdbpy_print_stack ();
do_cleanups (cleanup); warning (_("internal error: Unhandled Python exception"));
}
} }
/* Return non-zero if Python has successfully initialized. /* Return non-zero if Python has successfully initialized.