From 2bb8f231957e2beecfb689a896252b8d9fb67e23 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Thu, 12 Jan 2017 08:59:26 -0700 Subject: [PATCH] Remove some gotos from Python This patch slightly refactors a couple of spots in the Python code to avoid some gotos. gdb/ChangeLog 2017-02-10 Tom Tromey * python/python.c (do_start_initialization): New function, from _initialize_python. (_initialize_python): Call do_start_initialization. * python/py-linetable.c (ltpy_iternext): Use explicit returns, not goto. --- gdb/ChangeLog | 8 ++ gdb/python/py-linetable.c | 14 ++-- gdb/python/python.c | 192 ++++++++++++++++++++++++---------------------- 3 files changed, 115 insertions(+), 99 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 6b449f0..ddf4de0 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,13 @@ 2017-02-10 Tom Tromey + * python/python.c (do_start_initialization): New function, from + _initialize_python. + (_initialize_python): Call do_start_initialization. + * python/py-linetable.c (ltpy_iternext): Use explicit returns, not + goto. + +2017-02-10 Tom Tromey + * python/py-prettyprint.c (pretty_print_one_value): Use gdbpy_ref. diff --git a/gdb/python/py-linetable.c b/gdb/python/py-linetable.c index d8597e4..a5e57b0 100644 --- a/gdb/python/py-linetable.c +++ b/gdb/python/py-linetable.c @@ -407,7 +407,10 @@ ltpy_iternext (PyObject *self) LTPY_REQUIRE_VALID (iter_obj->source, symtab); if (iter_obj->current_index >= SYMTAB_LINETABLE (symtab)->nitems) - goto stop_iteration; + { + PyErr_SetNone (PyExc_StopIteration); + return NULL; + } item = &(SYMTAB_LINETABLE (symtab)->item[iter_obj->current_index]); @@ -419,7 +422,10 @@ ltpy_iternext (PyObject *self) /* Exit if the internal value is the last item in the line table. */ if (iter_obj->current_index >= SYMTAB_LINETABLE (symtab)->nitems) - goto stop_iteration; + { + PyErr_SetNone (PyExc_StopIteration); + return NULL; + } item = &(SYMTAB_LINETABLE (symtab)->item[iter_obj->current_index]); } @@ -427,10 +433,6 @@ ltpy_iternext (PyObject *self) iter_obj->current_index++; return obj; - - stop_iteration: - PyErr_SetNone (PyExc_StopIteration); - return NULL; } /* Implementation of gdb.LineTableIterator.is_valid (self) -> Boolean. diff --git a/gdb/python/python.c b/gdb/python/python.c index 74a6f42..1f5ab42 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1525,8 +1525,10 @@ finalize_python (void *ignore) /* Provide a prototype to silence -Wmissing-prototypes. */ extern initialize_file_ftype _initialize_python; -void -_initialize_python (void) +#ifdef HAVE_PYTHON + +static bool +do_start_initialization () { char *progname; #ifdef IS_PY3K @@ -1536,77 +1538,6 @@ _initialize_python (void) wchar_t *progname_copy; #endif - add_com ("python-interactive", class_obscure, - python_interactive_command, -#ifdef HAVE_PYTHON - _("\ -Start an interactive Python prompt.\n\ -\n\ -To return to GDB, type the EOF character (e.g., Ctrl-D on an empty\n\ -prompt).\n\ -\n\ -Alternatively, a single-line Python command can be given as an\n\ -argument, and if the command is an expression, the result will be\n\ -printed. For example:\n\ -\n\ - (gdb) python-interactive 2 + 3\n\ - 5\n\ -") -#else /* HAVE_PYTHON */ - _("\ -Start a Python interactive prompt.\n\ -\n\ -Python scripting is not supported in this copy of GDB.\n\ -This command is only a placeholder.") -#endif /* HAVE_PYTHON */ - ); - add_com_alias ("pi", "python-interactive", class_obscure, 1); - - add_com ("python", class_obscure, python_command, -#ifdef HAVE_PYTHON - _("\ -Evaluate a Python command.\n\ -\n\ -The command can be given as an argument, for instance:\n\ -\n\ - python print 23\n\ -\n\ -If no argument is given, the following lines are read and used\n\ -as the Python commands. Type a line containing \"end\" to indicate\n\ -the end of the command.") -#else /* HAVE_PYTHON */ - _("\ -Evaluate a Python command.\n\ -\n\ -Python scripting is not supported in this copy of GDB.\n\ -This command is only a placeholder.") -#endif /* HAVE_PYTHON */ - ); - add_com_alias ("py", "python", class_obscure, 1); - - /* Add set/show python print-stack. */ - add_prefix_cmd ("python", no_class, user_show_python, - _("Prefix command for python preference settings."), - &user_show_python_list, "show python ", 0, - &showlist); - - add_prefix_cmd ("python", no_class, user_set_python, - _("Prefix command for python preference settings."), - &user_set_python_list, "set python ", 0, - &setlist); - - add_setshow_enum_cmd ("print-stack", no_class, python_excp_enums, - &gdbpy_should_print_stack, _("\ -Set mode for Python stack dump on error."), _("\ -Show the mode of Python stack printing on error."), _("\ -none == no stack or message will be printed.\n\ -full == a message and a stack will be printed.\n\ -message == an error message without a stack will be printed."), - NULL, NULL, - &user_set_python_list, - &user_show_python_list); - -#ifdef HAVE_PYTHON #ifdef WITH_PYTHON_PATH /* Work around problem where python gets confused about where it is, and then can't find its libraries, etc. @@ -1625,14 +1556,14 @@ message == an error message without a stack will be printed."), { xfree (oldloc); fprintf (stderr, "out of memory\n"); - return; + return false; } count = mbstowcs (progname_copy, progname, progsize + 1); if (count == (size_t) -1) { xfree (oldloc); fprintf (stderr, "Could not convert python path to string\n"); - return; + return false; } setlocale (LC_ALL, oldloc); xfree (oldloc); @@ -1657,7 +1588,7 @@ message == an error message without a stack will be printed."), gdb_module = Py_InitModule ("_gdb", python_GdbMethods); #endif if (gdb_module == NULL) - goto fail; + return false; /* The casts to (char*) are for python 2.4. */ if (PyModule_AddStringConstant (gdb_module, "VERSION", (char*) version) < 0 @@ -1665,31 +1596,31 @@ message == an error message without a stack will be printed."), (char*) host_name) < 0 || PyModule_AddStringConstant (gdb_module, "TARGET_CONFIG", (char*) target_name) < 0) - goto fail; + return false; /* Add stream constants. */ if (PyModule_AddIntConstant (gdb_module, "STDOUT", 0) < 0 || PyModule_AddIntConstant (gdb_module, "STDERR", 1) < 0 || PyModule_AddIntConstant (gdb_module, "STDLOG", 2) < 0) - goto fail; + return false; gdbpy_gdb_error = PyErr_NewException ("gdb.error", PyExc_RuntimeError, NULL); if (gdbpy_gdb_error == NULL || gdb_pymodule_addobject (gdb_module, "error", gdbpy_gdb_error) < 0) - goto fail; + return false; gdbpy_gdb_memory_error = PyErr_NewException ("gdb.MemoryError", gdbpy_gdb_error, NULL); if (gdbpy_gdb_memory_error == NULL || gdb_pymodule_addobject (gdb_module, "MemoryError", gdbpy_gdb_memory_error) < 0) - goto fail; + return false; gdbpy_gdberror_exc = PyErr_NewException ("gdb.GdbError", NULL, NULL); if (gdbpy_gdberror_exc == NULL || gdb_pymodule_addobject (gdb_module, "GdbError", gdbpy_gdberror_exc) < 0) - goto fail; + return false; gdbpy_initialize_gdb_readline (); @@ -1730,26 +1661,26 @@ message == an error message without a stack will be printed."), || gdbpy_initialize_arch () < 0 || gdbpy_initialize_xmethods () < 0 || gdbpy_initialize_unwind () < 0) - goto fail; + return false; gdbpy_to_string_cst = PyString_FromString ("to_string"); if (gdbpy_to_string_cst == NULL) - goto fail; + return false; gdbpy_children_cst = PyString_FromString ("children"); if (gdbpy_children_cst == NULL) - goto fail; + return false; gdbpy_display_hint_cst = PyString_FromString ("display_hint"); if (gdbpy_display_hint_cst == NULL) - goto fail; + return false; gdbpy_doc_cst = PyString_FromString ("__doc__"); if (gdbpy_doc_cst == NULL) - goto fail; + return false; gdbpy_enabled_cst = PyString_FromString ("enabled"); if (gdbpy_enabled_cst == NULL) - goto fail; + return false; gdbpy_value_cst = PyString_FromString ("value"); if (gdbpy_value_cst == NULL) - goto fail; + return false; /* Release the GIL while gdb runs. */ PyThreadState_Swap (NULL); @@ -1757,14 +1688,89 @@ message == an error message without a stack will be printed."), make_final_cleanup (finalize_python, NULL); + /* Only set this when initialization has succeeded. */ gdb_python_initialized = 1; - return; + return true; +} + +#endif /* HAVE_PYTHON */ + +void +_initialize_python (void) +{ + add_com ("python-interactive", class_obscure, + python_interactive_command, +#ifdef HAVE_PYTHON + _("\ +Start an interactive Python prompt.\n\ +\n\ +To return to GDB, type the EOF character (e.g., Ctrl-D on an empty\n\ +prompt).\n\ +\n\ +Alternatively, a single-line Python command can be given as an\n\ +argument, and if the command is an expression, the result will be\n\ +printed. For example:\n\ +\n\ + (gdb) python-interactive 2 + 3\n\ + 5\n\ +") +#else /* HAVE_PYTHON */ + _("\ +Start a Python interactive prompt.\n\ +\n\ +Python scripting is not supported in this copy of GDB.\n\ +This command is only a placeholder.") +#endif /* HAVE_PYTHON */ + ); + add_com_alias ("pi", "python-interactive", class_obscure, 1); + + add_com ("python", class_obscure, python_command, +#ifdef HAVE_PYTHON + _("\ +Evaluate a Python command.\n\ +\n\ +The command can be given as an argument, for instance:\n\ +\n\ + python print 23\n\ +\n\ +If no argument is given, the following lines are read and used\n\ +as the Python commands. Type a line containing \"end\" to indicate\n\ +the end of the command.") +#else /* HAVE_PYTHON */ + _("\ +Evaluate a Python command.\n\ +\n\ +Python scripting is not supported in this copy of GDB.\n\ +This command is only a placeholder.") +#endif /* HAVE_PYTHON */ + ); + add_com_alias ("py", "python", class_obscure, 1); + + /* Add set/show python print-stack. */ + add_prefix_cmd ("python", no_class, user_show_python, + _("Prefix command for python preference settings."), + &user_show_python_list, "show python ", 0, + &showlist); - fail: - gdbpy_print_stack (); - /* Do not set 'gdb_python_initialized'. */ - return; + add_prefix_cmd ("python", no_class, user_set_python, + _("Prefix command for python preference settings."), + &user_set_python_list, "set python ", 0, + &setlist); + add_setshow_enum_cmd ("print-stack", no_class, python_excp_enums, + &gdbpy_should_print_stack, _("\ +Set mode for Python stack dump on error."), _("\ +Show the mode of Python stack printing on error."), _("\ +none == no stack or message will be printed.\n\ +full == a message and a stack will be printed.\n\ +message == an error message without a stack will be printed."), + NULL, NULL, + &user_set_python_list, + &user_show_python_list); + +#ifdef HAVE_PYTHON + if (!do_start_initialization () && PyErr_Occurred ()) + gdbpy_print_stack (); #endif /* HAVE_PYTHON */ } -- 2.7.4