/* 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
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.
{
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);
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
(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 ();
|| 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);
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 */
}