public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [RFA 5/5] Remove some gotos from Python
Date: Sun, 15 Jan 2017 13:43:00 -0000	[thread overview]
Message-ID: <20170115134253.24018-6-tom@tromey.com> (raw)
In-Reply-To: <20170115134253.24018-1-tom@tromey.com>

This patch slightly refactors a couple of spots in the Python code to
avoid some gotos.

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

	* 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 c32192d..7eaeb6c 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,13 @@
 2017-01-15  Tom Tromey  <tom@tromey.com>
 
+	* 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-01-15  Tom Tromey  <tom@tromey.com>
+
 	* 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 1f73ff7..5fe352d 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 ab5a6a4..69851c3 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1524,8 +1524,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
@@ -1535,77 +1537,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.
@@ -1624,14 +1555,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);
@@ -1656,7 +1587,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
@@ -1664,31 +1595,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 ();
 
@@ -1729,26 +1660,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);
@@ -1756,14 +1687,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.9.3

  parent reply	other threads:[~2017-01-15 13:43 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-15 13:43 [RFA 0/5] more cleanup removal in Python Tom Tromey
2017-01-15 13:43 ` [RFA 4/5] Change one more spot to use gdbpy_ref Tom Tromey
2017-02-09 12:52   ` Pedro Alves
2017-01-15 13:43 ` [RFA 3/5] Introduce gdbpy_subclass and use it to simplify some logic Tom Tromey
2017-01-24 20:21   ` Simon Marchi
2017-02-09 11:44     ` Pedro Alves
2017-02-09 18:52       ` Tom Tromey
2017-02-09 13:00   ` Pedro Alves
2017-01-15 13:43 ` [RFA 1/5] Remove some ui_out-related cleanups from Python Tom Tromey
2017-01-15 21:52   ` Simon Marchi
2017-01-16 16:13     ` Tom Tromey
2017-01-16 11:19   ` Trevor Saunders
2017-02-08 17:28     ` Pedro Alves
2017-02-08 22:27       ` Pedro Alves
2017-02-08 23:05       ` Tom Tromey
2017-02-08 23:52         ` Pedro Alves
2017-02-09  4:34           ` Matt Rice
2017-02-09 12:48             ` Pedro Alves
2017-02-09 12:51               ` Pedro Alves
2017-02-09 15:46                 ` Matt Rice
2017-02-09 16:04                   ` Simon Marchi
2017-02-10  6:47       ` Trevor Saunders
2017-01-15 13:43 ` [RFA 2/5] Introduce ui_file_up and use it to remove cleanups Tom Tromey
2017-01-16  9:59   ` Trevor Saunders
2017-01-16 17:58   ` Pedro Alves
2017-01-16 19:08     ` Tom Tromey
2017-01-17  1:40       ` Pedro Alves
2017-01-17 19:05         ` Tom Tromey
2017-01-15 13:43 ` Tom Tromey [this message]
2017-02-09 13:03   ` [RFA 5/5] Remove some gotos from Python Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170115134253.24018-6-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).