From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 128497 invoked by alias); 15 Jan 2017 13:43:21 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 128421 invoked by uid 89); 15 Jan 2017 13:43:21 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.3 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE,RCVD_IN_SORBS_SPAM,SPF_PASS autolearn=no version=3.3.2 spammy=4077, silence, H*RU:10.0.90.82, Hx-spam-relays-external:CMOut01 X-HELO: gproxy2-pub.mail.unifiedlayer.com Received: from gproxy2-pub.mail.unifiedlayer.com (HELO gproxy2-pub.mail.unifiedlayer.com) (69.89.18.3) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with SMTP; Sun, 15 Jan 2017 13:43:04 +0000 Received: (qmail 1451 invoked by uid 0); 15 Jan 2017 13:43:02 -0000 Received: from unknown (HELO CMOut01) (10.0.90.82) by gproxy2.mail.unifiedlayer.com with SMTP; 15 Jan 2017 13:43:02 -0000 Received: from box522.bluehost.com ([74.220.219.122]) by CMOut01 with id Ydiy1u00Z2f2jeq01dj11b; Sun, 15 Jan 2017 06:43:01 -0700 X-Authority-Analysis: v=2.1 cv=H75InYoi c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IgFoBzBjUZAA:10 a=zstS-IiYAAAA:8 a=zZZVtEAcxdeiLj70LpMA:9 a=4G6NA9xxw8l3yy4pmD5M:22 Received: from 174-16-146-181.hlrn.qwest.net ([174.16.146.181]:40602 helo=bapiya.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87) (envelope-from ) id 1cSl5H-0002Lj-JC; Sun, 15 Jan 2017 06:42:59 -0700 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [RFA 5/5] Remove some gotos from Python Date: Sun, 15 Jan 2017 13:43:00 -0000 Message-Id: <20170115134253.24018-6-tom@tromey.com> In-Reply-To: <20170115134253.24018-1-tom@tromey.com> References: <20170115134253.24018-1-tom@tromey.com> X-BWhitelist: no X-Exim-ID: 1cSl5H-0002Lj-JC X-Source-Sender: 174-16-146-181.hlrn.qwest.net (bapiya.Home) [174.16.146.181]:40602 X-Source-Auth: tom+tromey.com X-Email-Count: 6 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTIyLmJsdWVob3N0LmNvbQ== X-SW-Source: 2017-01/txt/msg00278.txt.bz2 This patch slightly refactors a couple of spots in the Python code to avoid some gotos. 2017-01-15 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 c32192d..7eaeb6c 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,13 @@ 2017-01-15 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-01-15 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 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