* [python] [patch] PR python/13329
@ 2011-11-09 15:36 Phil Muldoon
2011-11-09 15:49 ` Kevin Pouget
2011-11-17 20:55 ` Tom Tromey
0 siblings, 2 replies; 13+ messages in thread
From: Phil Muldoon @ 2011-11-09 15:36 UTC (permalink / raw)
To: gdb-patches
This patch changes the behavior of 'python stack-print' to three modes:
full, message or none. The default has been set to "message".
full - will print the exception message and the stack.
message - will print the exception type and message only.
none - nothing will be printed.
This patch also fixes a bug where we were calling gdbpy_print_stack
after a few PyRun_SimpleString calls. This is not necessary as that
API deals with the exception internally and prints its own stack.
This is backwards compatible with the deprecated maint set python print-sack.
on - will set the mode to "full".
off - will set the mode to "none".
Tested with no regressions.
Cheers,
Phil
--
2011-11-09 Phil Muldoon <pmuldoon@redhat.com>
PR python/13329
* python/python.c: Declare python_excp_none, python_excp_full,
python_excp_message, python_excp_enums
(set_python_excp_mode): New function.
(_initialize_python): Set 'print-stack' command to an enum
accepting command.
(gdbpy_print_stack): Rewrite.
(eval_python_from_control_command): Do not call gdbpy_print_stack.
(python_command): Ditto.
* NEWS: Update 'print-stack' text.
2011-11-09 Phil Muldoon <pmuldoon@redhat.com>
PR python/13329
* gdb.texinfo (Python Commands): Update 'print-stack' to reflect
the three values it can now take.
2011-11-09 Phil Muldoon <pmuldoon@redhat.com>
PR python/13329
* gdb.python/py-function.exp: Set python print-stack "on" to "full.
* gdb.python/python.exp: Ditto.
--
diff --git a/gdb/NEWS b/gdb/NEWS
index 1713049..ecf6849 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -23,9 +23,10 @@
existing one.
** The "maint set python print-stack on|off" command has been
- deprecated, and a new command: "set python print-stack on|off" has
- replaced it. Additionally, the default for "print-stack" is now
- "off".
+ deprecated, and a new command: "set python print-stack
+ none|full|message" has replaced it. Additionally, the default
+ for "print-stack" is now "message", which just prints the error
+ message without the stack trace.
** A prompt substitution hook (prompt_hook) is now available to the
Python API.
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index d2bdefa..dc5daa9 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -21348,11 +21348,12 @@ print-stack}
@kindex set python print-stack
@item set python print-stack
-By default, @value{GDBN} will not print a stack trace when an error
-occurs in a Python script. This can be controlled using @code{set
-python print-stack}: if @code{on}, then Python stack printing is
-enabled; if @code{off}, the default, then Python stack printing is
-disabled.
+By default, @value{GDBN} will print only the message component of a
+Python exception when an error occurs in a Python script. This can be
+controlled using @code{set python print-stack}: if @code{full}, then
+full Python stack printing is enabled; if @code{none}, then Python stack
+and message printing is disabled; if @code{message}, the default, only
+the message component of the error is printed.
@end table
It is also possible to execute a Python script from the @value{GDBN}
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 108e542..93a80c6 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -35,9 +35,29 @@
#include <ctype.h>
-/* True if we should print the stack when catching a Python error,
- false otherwise. */
-static int gdbpy_should_print_stack = 0;
+/* Declared constants and enum for python stack printing. */
+static const char python_excp_none[] = "none";
+static const char python_excp_full[] = "full";
+static const char python_excp_message[] = "message";
+
+/* set python print-stack choices. */
+static const char *python_excp_enums[] = {
+ python_excp_none,
+ python_excp_full,
+ python_excp_message,
+ NULL
+};
+
+/* 0 if we should not print the stack when catching a Python error,
+ 1 to print the full stack, 2 to just print the message. */
+static int gdbpy_should_print_stack = 2;
+
+/* The extended exception printing variable. 'full' if we want to
+ print the error message and stack, 'none' if we want to print
+ nothing, and 'message' if we only want to print the error
+ message. */
+static const char *gdbpy_should_print_stack_extended =
+ python_excp_message;
#ifdef HAVE_PYTHON
@@ -233,10 +253,7 @@ eval_python_from_control_command (struct command_line *cmd)
ret = PyRun_SimpleString (script);
xfree (script);
if (ret)
- {
- gdbpy_print_stack ();
- error (_("Error while executing Python code."));
- }
+ error (_("Error while executing Python code."));
do_cleanups (cleanup);
}
@@ -258,10 +275,7 @@ python_command (char *arg, int from_tty)
if (arg && *arg)
{
if (PyRun_SimpleString (arg))
- {
- gdbpy_print_stack ();
- error (_("Error while executing Python code."));
- }
+ error (_("Error while executing Python code."));
}
else
{
@@ -881,22 +895,57 @@ gdbpy_flush (PyObject *self, PyObject *args, PyObject *kw)
Py_RETURN_NONE;
}
-/* Print a python exception trace, or print nothing and clear the
- python exception, depending on gdbpy_should_print_stack. Only call
- this if a python exception is set. */
+/* Print a python exception trace, print just a message, or print
+ nothing and clear the python exception, depending on
+ gdbpy_should_print_stack. Only call this if a python exception is
+ set. */
void
gdbpy_print_stack (void)
{
- if (gdbpy_should_print_stack)
+ switch (gdbpy_should_print_stack)
{
+ PyObject *ptype, *pvalue, *ptraceback;
+ char *msg = NULL, *type = NULL;
+
+ case 0:
+ /* Print "none", just clear exception. */
+ PyErr_Clear ();
+ break;
+
+ case 1:
+ /* Print "full" message and backtrace. */
PyErr_Print ();
/* PyErr_Print doesn't necessarily end output with a newline.
This works because Python's stdout/stderr is fed through
printf_filtered. */
begin_line ();
+ break;
+
+ case 2:
+ /* Print "message", just error print message. */
+ PyErr_Fetch (&ptype, &pvalue, &ptraceback);
+
+ /* Fetch the error message contained within ptype, pvalue. */
+ msg = gdbpy_exception_to_string (ptype, pvalue);
+ type = gdbpy_obj_to_string (ptype);
+ if (msg == NULL)
+ {
+ /* An error occurred computing the string representation of the
+ error message. */
+ fprintf_filtered (gdb_stderr,
+ _("Error occurred computing Python error" \
+ "message.\n"));
+ }
+ else
+ fprintf_filtered (gdb_stderr, "Python Exception %s %s: \n",
+ type, msg);
+
+ Py_XDECREF (ptype);
+ Py_XDECREF (pvalue);
+ Py_XDECREF (ptraceback);
+ xfree (msg);
+ break;
}
- else
- PyErr_Clear ();
}
\f
@@ -1106,6 +1155,21 @@ user_show_python (char *args, int from_tty)
cmd_show_list (user_show_python_list, from_tty, "");
}
+/* When setting the 'python print-stack' with an enum, set the
+ gdbpy_should_print_stack correspondingly. */
+static void
+set_python_excp_mode (char *args, int i, struct cmd_list_element *c)
+{
+ const char *user_arg = *(const char **) c->var;
+
+ if (strncmp (user_arg, python_excp_none, 4) == 0)
+ gdbpy_should_print_stack = 0;
+ else if (strncmp (user_arg, python_excp_full, 3) == 0)
+ gdbpy_should_print_stack = 1;
+ else if (strncmp (user_arg, python_excp_message, 7) == 0)
+ gdbpy_should_print_stack = 2;
+}
+
/* Initialize the Python code. */
/* Provide a prototype to silence -Wmissing-prototypes. */
@@ -1176,14 +1240,16 @@ Enables or disables printing of Python stack traces."),
&user_set_python_list, "set python ", 0,
&setlist);
- add_setshow_boolean_cmd ("print-stack", no_class,
- &gdbpy_should_print_stack, _("\
-Enable or disable printing of Python stack dump on error."), _("\
-Show whether Python stack will be printed on error."), _("\
-Enables or disables printing of Python stack traces."),
- NULL, NULL,
- &user_set_python_list,
- &user_show_python_list);
+ add_setshow_enum_cmd ("print-stack", no_class, python_excp_enums,
+ &gdbpy_should_print_stack_extended, _("\
+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."),
+ set_python_excp_mode, NULL,
+ &user_set_python_list,
+ &user_show_python_list);
#ifdef HAVE_PYTHON
#ifdef WITH_PYTHON_PATH
diff --git a/gdb/testsuite/gdb.python/py-function.exp b/gdb/testsuite/gdb.python/py-function.exp
index d579435..f670d52 100644
--- a/gdb/testsuite/gdb.python/py-function.exp
+++ b/gdb/testsuite/gdb.python/py-function.exp
@@ -93,7 +93,7 @@ gdb_py_test_multiple "Test Normal Error" \
"NormalError ()" "" \
"end" ""
-gdb_test_no_output "set python print-stack on"
+gdb_test_no_output "set python print-stack full"
gdb_test "print \$normalerror()" "Traceback.*File.*line 5.*in invoke.*RuntimeError.*This is a Normal Error.*" \
"Test a Runtime error. There should be a stack trace."
diff --git a/gdb/testsuite/gdb.python/py-prettyprint.exp b/gdb/testsuite/gdb.python/py-prettyprint.exp
index b0e7d62..9786d71 100644
--- a/gdb/testsuite/gdb.python/py-prettyprint.exp
+++ b/gdb/testsuite/gdb.python/py-prettyprint.exp
@@ -97,7 +97,7 @@ proc run_lang_tests {exefile lang} {
gdb_test_no_output "python pp_ls_encoding = 'UTF-8'"
gdb_test "print estring2" "\"embedded \", <incomplete sequence \\\\302>"
- gdb_test_no_output "set python print-stack on"
+ gdb_test_no_output "set python print-stack full"
gdb_test "print hint_error" "Exception: hint failed\r\nhint_error_val"
gdb_test "print c" " = container \"container\" with 2 elements = {$nl *.0. = 23,$nl *.1. = 72$nl}"
diff --git a/gdb/testsuite/gdb.python/python.exp b/gdb/testsuite/gdb.python/python.exp
index 94d6d0d..2062d00 100644
--- a/gdb/testsuite/gdb.python/python.exp
+++ b/gdb/testsuite/gdb.python/python.exp
@@ -203,12 +203,13 @@ gdb_test "maint set python print-stack off" \
"Warning: command 'maintenance set python print-stack' is deprecated.*Use 'set python print-stack'.*" \
"Test deprecation maint set warning."
gdb_test "show python print-stack" \
- "Whether Python stack will be printed on error is off.*" \
+ "The mode of Python stack printing on error is \"message\".*" \
"Test print-backtrace show setting. Default off."
-gdb_py_test_silent_cmd "set python print-stack on" \
+gdb_py_test_silent_cmd "set python print-stack full" \
"Test print-backtrace set setting" 1
gdb_test "show python print-stack" \
- "Whether Python stack will be printed on error is on.*" \
+ "The mode of Python stack printing on error is \"full\".*" \
+ "Test print-backtrace show setting to full."
# Test prompt substituion
@@ -313,7 +314,7 @@ gdb_test_multiple "set extended-prompt \\w " \
gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \
"set extended prompt parameter" {
- -re "\[\r\n\]some param True $" {
+ -re "\[\r\n\]some param full $" {
pass "set extended prompt parameter"
}
}
^ permalink raw reply [flat|nested] 13+ messages in thread* Re: [python] [patch] PR python/13329 2011-11-09 15:36 [python] [patch] PR python/13329 Phil Muldoon @ 2011-11-09 15:49 ` Kevin Pouget 2011-11-09 20:25 ` Phil Muldoon 2011-11-17 20:55 ` Tom Tromey 1 sibling, 1 reply; 13+ messages in thread From: Kevin Pouget @ 2011-11-09 15:49 UTC (permalink / raw) To: pmuldoon; +Cc: gdb-patches On Wed, Nov 9, 2011 at 4:35 PM, Phil Muldoon <pmuldoon@redhat.com> wrote: > > This patch changes the behavior of 'python stack-print' to three modes: > full, message or none. The default has been set to "message". > > full - will print the exception message and the stack. > message - will print the exception type and message only. > none - nothing will be printed. > > This patch also fixes a bug where we were calling gdbpy_print_stack > after a few PyRun_SimpleString calls. This is not necessary as that > API deals with the exception internally and prints its own stack. > > This is backwards compatible with the deprecated maint set python print-sack. > > on - will set the mode to "full". > off - will set the mode to "none". > > Tested with no regressions. > > Cheers, > > Phil > Thanks Phil, that's exactly what I was looking for :) just a remark, + fprintf_filtered (gdb_stderr, + _("Error occurred computing Python error" \ + "message.\n")); this error message sounds strange to me, is it correct? Cheers, Kevin ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [python] [patch] PR python/13329 2011-11-09 15:49 ` Kevin Pouget @ 2011-11-09 20:25 ` Phil Muldoon 0 siblings, 0 replies; 13+ messages in thread From: Phil Muldoon @ 2011-11-09 20:25 UTC (permalink / raw) To: Kevin Pouget; +Cc: gdb-patches Kevin Pouget <kevin.pouget@gmail.com> writes: > Thanks Phil, that's exactly what I was looking for :) > > just a remark, > > + fprintf_filtered (gdb_stderr, > + _("Error occurred computing Python error" \ > + "message.\n")); > > this error message sounds strange to me, is it correct? Yes, this happens when we cannot get the exception message, but an exception has been set. I don't ever expect this error to actually be seen, but it can happen depending on the state of the interpreter. Looking at it again, I do see I did miss a space between 'error' and 'message' though! Cheers, Phil ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [python] [patch] PR python/13329 2011-11-09 15:36 [python] [patch] PR python/13329 Phil Muldoon 2011-11-09 15:49 ` Kevin Pouget @ 2011-11-17 20:55 ` Tom Tromey 2011-11-29 13:57 ` Phil Muldoon 1 sibling, 1 reply; 13+ messages in thread From: Tom Tromey @ 2011-11-17 20:55 UTC (permalink / raw) To: pmuldoon; +Cc: gdb-patches >>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes: Phil> 2011-11-09 Phil Muldoon <pmuldoon@redhat.com> Phil> PR python/13329 Phil> +/* set python print-stack choices. */ Capital "s". Phil> +static const char *python_excp_enums[] = { I usually put the "{" on a new line here. It seems more GNU-ish to me. Phil> +/* 0 if we should not print the stack when catching a Python error, Phil> + 1 to print the full stack, 2 to just print the message. */ Phil> +static int gdbpy_should_print_stack = 2; It is more normal, though not exclusively the case (I think), to just use the string constants here. Because they are constant strings you can just compare them with ==. That is, you can just remove this variable. Phil> +/* The extended exception printing variable. 'full' if we want to Phil> + print the error message and stack, 'none' if we want to print Phil> + nothing, and 'message' if we only want to print the error Phil> + message. */ Phil> +static const char *gdbpy_should_print_stack_extended = Phil> + python_excp_message; ... and rename this to gdbpy_should_print_stack and use it directly. Phil> +/* When setting the 'python print-stack' with an enum, set the Phil> + gdbpy_should_print_stack correspondingly. */ Phil> +static void Phil> +set_python_excp_mode (char *args, int i, struct cmd_list_element *c) With the above change you won't need this function. Phil> + add_setshow_enum_cmd ("print-stack", no_class, python_excp_enums, Phil> + &gdbpy_should_print_stack_extended, _("\ Phil> +Set mode for Python stack dump on error."), _("\ Phil> +Show the mode of Python stack printing on error."), _("\ Phil> +none == no stack or message will be printed.\n\ Phil> +full == a message and a stack will be printed.\n\ Phil> +message == an error message without a stack will be printed."), Phil> + set_python_excp_mode, NULL, Phil> + &user_set_python_list, Phil> + &user_show_python_list); I'm surprised this is backward compatible. Phil> -gdb_test_no_output "set python print-stack on" Phil> +gdb_test_no_output "set python print-stack full" You should leave in some compatibility tests for the old values. I didn't look but you should try the old code to see exactly what values it accepted. I thought the boolean set/shows took more than just on/off. Tom ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [python] [patch] PR python/13329 2011-11-17 20:55 ` Tom Tromey @ 2011-11-29 13:57 ` Phil Muldoon 2011-12-08 13:08 ` Phil Muldoon 2011-12-13 19:40 ` Tom Tromey 0 siblings, 2 replies; 13+ messages in thread From: Phil Muldoon @ 2011-11-29 13:57 UTC (permalink / raw) To: Tom Tromey; +Cc: gdb-patches Tom Tromey <tromey@redhat.com> writes: > Phil> +/* 0 if we should not print the stack when catching a Python error, > Phil> + 1 to print the full stack, 2 to just print the message. */ > Phil> +static int gdbpy_should_print_stack = 2; > > It is more normal, though not exclusively the case (I think), to just > use the string constants here. Because they are constant strings you > can just compare them with ==. > > That is, you can just remove this variable. > > Phil> +/* The extended exception printing variable. 'full' if we want to > Phil> + print the error message and stack, 'none' if we want to print > Phil> + nothing, and 'message' if we only want to print the error > Phil> + message. */ > Phil> +static const char *gdbpy_should_print_stack_extended = > Phil> + python_excp_message; > > ... and rename this to gdbpy_should_print_stack and use it directly. > > Phil> +/* When setting the 'python print-stack' with an enum, set the > Phil> + gdbpy_should_print_stack correspondingly. */ > Phil> +static void > Phil> +set_python_excp_mode (char *args, int i, struct cmd_list_element *c) I have to keep/add these functions around to be compatible with "maint set python print-stack" which is a boolean parameter. So it was my view we still operate on the gdbpy_should_print_stack variable in gdbpy_print_stack function, and have the "set function" just set gdbpy_should_print_stack from the enum command. I can do what you wish, but I would have to write a "set" function to set the enum constant instead. > With the above change you won't need this function. > > Phil> + add_setshow_enum_cmd ("print-stack", no_class, python_excp_enums, > Phil> + &gdbpy_should_print_stack_extended, _("\ > Phil> +Set mode for Python stack dump on error."), _("\ > Phil> +Show the mode of Python stack printing on error."), _("\ > Phil> +none == no stack or message will be printed.\n\ > Phil> +full == a message and a stack will be printed.\n\ > Phil> +message == an error message without a stack will be printed."), > Phil> + set_python_excp_mode, NULL, > Phil> + &user_set_python_list, > Phil> + &user_show_python_list); > > I'm surprised this is backward compatible. The user command "set python print-stack" has not been published in a release so it is not compatible; I believe it is within our API pledge to change whatever we like until that feature is released. "maint set python print-stack" still works, but is deprecated. For the variable gdbpy_should_print_stack, 0 is still off, 1 is full, 2 is message only. The "maint set python print-stack" will only ever set off or full. > Phil> -gdb_test_no_output "set python print-stack on" > Phil> +gdb_test_no_output "set python print-stack full" > > You should leave in some compatibility tests for the old values. Do you mean use "maint set python print-stack" here? > I didn't look but you should try the old code to see exactly what values > it accepted. I thought the boolean set/shows took more than just on/off. I will, but the result remains the same. 0 for off, > 0 for on. Cheers Phil ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [python] [patch] PR python/13329 2011-11-29 13:57 ` Phil Muldoon @ 2011-12-08 13:08 ` Phil Muldoon 2011-12-13 19:40 ` Tom Tromey 1 sibling, 0 replies; 13+ messages in thread From: Phil Muldoon @ 2011-12-08 13:08 UTC (permalink / raw) To: Tom Tromey; +Cc: gdb-patches Phil Muldoon <pmuldoon@redhat.com> writes: Ping. I want to target this for 7.4. Cheers, Phil > Tom Tromey <tromey@redhat.com> writes: > >> Phil> +/* 0 if we should not print the stack when catching a Python error, >> Phil> + 1 to print the full stack, 2 to just print the message. */ >> Phil> +static int gdbpy_should_print_stack = 2; >> >> It is more normal, though not exclusively the case (I think), to just >> use the string constants here. Because they are constant strings you >> can just compare them with ==. >> >> That is, you can just remove this variable. >> >> Phil> +/* The extended exception printing variable. 'full' if we want to >> Phil> + print the error message and stack, 'none' if we want to print >> Phil> + nothing, and 'message' if we only want to print the error >> Phil> + message. */ >> Phil> +static const char *gdbpy_should_print_stack_extended = >> Phil> + python_excp_message; >> >> ... and rename this to gdbpy_should_print_stack and use it directly. >> >> Phil> +/* When setting the 'python print-stack' with an enum, set the >> Phil> + gdbpy_should_print_stack correspondingly. */ >> Phil> +static void >> Phil> +set_python_excp_mode (char *args, int i, struct cmd_list_element *c) > > I have to keep/add these functions around to be compatible with "maint > set python print-stack" which is a boolean parameter. So it was my view > we still operate on the gdbpy_should_print_stack variable in > gdbpy_print_stack function, and have the "set function" just set > gdbpy_should_print_stack from the enum command. I can do what you wish, > but I would have to write a "set" function to set the enum constant > instead. > >> With the above change you won't need this function. >> >> Phil> + add_setshow_enum_cmd ("print-stack", no_class, python_excp_enums, >> Phil> + &gdbpy_should_print_stack_extended, _("\ >> Phil> +Set mode for Python stack dump on error."), _("\ >> Phil> +Show the mode of Python stack printing on error."), _("\ >> Phil> +none == no stack or message will be printed.\n\ >> Phil> +full == a message and a stack will be printed.\n\ >> Phil> +message == an error message without a stack will be printed."), >> Phil> + set_python_excp_mode, NULL, >> Phil> + &user_set_python_list, >> Phil> + &user_show_python_list); >> >> I'm surprised this is backward compatible. > > The user command "set python print-stack" has not been published in a > release so it is not compatible; I believe it is within our API pledge > to change whatever we like until that feature is released. "maint set > python print-stack" still works, but is deprecated. For the variable > gdbpy_should_print_stack, 0 is still off, 1 is full, 2 is message only. > The "maint set python print-stack" will only ever set off or full. > >> Phil> -gdb_test_no_output "set python print-stack on" >> Phil> +gdb_test_no_output "set python print-stack full" >> >> You should leave in some compatibility tests for the old values. > > Do you mean use "maint set python print-stack" here? > >> I didn't look but you should try the old code to see exactly what values >> it accepted. I thought the boolean set/shows took more than just on/off. > > I will, but the result remains the same. 0 for off, > 0 for on. > > Cheers > > Phil ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [python] [patch] PR python/13329 2011-11-29 13:57 ` Phil Muldoon 2011-12-08 13:08 ` Phil Muldoon @ 2011-12-13 19:40 ` Tom Tromey 2011-12-15 18:15 ` Phil Muldoon 1 sibling, 1 reply; 13+ messages in thread From: Tom Tromey @ 2011-12-13 19:40 UTC (permalink / raw) To: pmuldoon; +Cc: gdb-patches >>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes: Phil> The user command "set python print-stack" has not been published in a Phil> release so it is not compatible Ok, I see. I didn't remember that. I think you should just change the 'maint' one to follow. If you want you can add a compatibility mode, but it is up to you. I don't think we should promise any kind of compatibility around "maint" commands. Tom> You should leave in some compatibility tests for the old values. Phil> Do you mean use "maint set python print-stack" here? I did, but don't bother with that now. Tom ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [python] [patch] PR python/13329 2011-12-13 19:40 ` Tom Tromey @ 2011-12-15 18:15 ` Phil Muldoon 2011-12-15 21:27 ` Eli Zaretskii 2011-12-16 0:51 ` Tom Tromey 0 siblings, 2 replies; 13+ messages in thread From: Phil Muldoon @ 2011-12-15 18:15 UTC (permalink / raw) To: Tom Tromey; +Cc: gdb-patches, eli Tom Tromey <tromey@redhat.com> writes: >>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes: > > Phil> The user command "set python print-stack" has not been published in a > Phil> release so it is not compatible > > Ok, I see. I didn't remember that. > > I think you should just change the 'maint' one to follow. > If you want you can add a compatibility mode, but it is up to you. > I don't think we should promise any kind of compatibility around "maint" > commands. > > Tom> You should leave in some compatibility tests for the old values. > > Phil> Do you mean use "maint set python print-stack" here? > > I did, but don't bother with that now. I updated the patch. This patch removes the "maint" command entirely. Needs a doc/code review. OK? Cheers, Phil -- 2011-12-15 Phil Muldoon <pmuldoon@redhat.com> * testsuite/gdb.python/py-function.exp: Change "on" to "full" for python print-stack. Add set/show python print-stack off|full|message tests. 2011-12-15 Phil Muldoon <pmuldoon@redhat.com> * python/python.c: Define python_excp_enums. (eval_python_from_control_command): Do not call gdbpy_print_stack. (python_command): Ditto. (gdbpy_print_stack): Rewrite to use new enum constants. (maint_set_python): Remove function. (maint_show_python): Ditto. (_initialize_python): Do not add "maint" commands. Add "set/show python print-stack commands". * NEWS: Update to reflect removal for "maint set/show print-stack" 2011-12-15 Phil Muldoon <pmuldoon@redhat.com> * doc/gdb.texinfo (Python Commands): Remove "maint set/show print stack". Add documentation for "set/show python print-stack". -- diff --git a/gdb/NEWS b/gdb/NEWS index 02f73a5..43345a9 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -31,9 +31,10 @@ existing one. ** The "maint set python print-stack on|off" command has been - deprecated, and a new command: "set python print-stack on|off" has - replaced it. Additionally, the default for "print-stack" is now - "off". + removed. A new command: "set python print-stack + none|full|message" has replaced it. Additionally, the default + for "print-stack" is now "message", which just prints the error + message without the stack trace. ** A prompt substitution hook (prompt_hook) is now available to the Python API. diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 925e66a..4787b82 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -21447,18 +21447,14 @@ End with a line saying just "end". 23 @end smallexample -@kindex maint set python print-stack -@item maint set python print-stack -This command is now deprecated. Instead use @code{set python -print-stack} - @kindex set python print-stack @item set python print-stack -By default, @value{GDBN} will not print a stack trace when an error -occurs in a Python script. This can be controlled using @code{set -python print-stack}: if @code{on}, then Python stack printing is -enabled; if @code{off}, the default, then Python stack printing is -disabled. +By default, @value{GDBN} will print only the message component of a +Python exception when an error occurs in a Python script. This can be +controlled using @code{set python print-stack}: if @code{full}, then +full Python stack printing is enabled; if @code{none}, then Python stack +and message printing is disabled; if @code{message}, the default, only +the message component of the error is printed. @end table It is also possible to execute a Python script from the @value{GDBN} diff --git a/gdb/python/python.c b/gdb/python/python.c index b0b9a9c..9b00cdc 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -35,9 +35,25 @@ #include <ctype.h> -/* True if we should print the stack when catching a Python error, - false otherwise. */ -static int gdbpy_should_print_stack = 0; +/* Declared constants and enum for python stack printing. */ +static const char python_excp_none[] = "none"; +static const char python_excp_full[] = "full"; +static const char python_excp_message[] = "message"; + +/* "set python print-stack" choices. */ +static const char *python_excp_enums[] = + { + python_excp_none, + python_excp_full, + python_excp_message, + NULL + }; + +/* The exception printing variable. 'full' if we want to print the + error message and stack, 'none' if we want to print nothing, and + 'message' if we only want to print the error message. 'message' is + the default. */ +static const char *gdbpy_should_print_stack = python_excp_message; #ifdef HAVE_PYTHON @@ -233,10 +249,7 @@ eval_python_from_control_command (struct command_line *cmd) ret = PyRun_SimpleString (script); xfree (script); if (ret) - { - gdbpy_print_stack (); - error (_("Error while executing Python code.")); - } + error (_("Error while executing Python code.")); do_cleanups (cleanup); } @@ -258,10 +271,7 @@ python_command (char *arg, int from_tty) if (arg && *arg) { if (PyRun_SimpleString (arg)) - { - gdbpy_print_stack (); - error (_("Error while executing Python code.")); - } + error (_("Error while executing Python code.")); } else { @@ -881,13 +891,20 @@ gdbpy_flush (PyObject *self, PyObject *args, PyObject *kw) Py_RETURN_NONE; } -/* Print a python exception trace, or print nothing and clear the - python exception, depending on gdbpy_should_print_stack. Only call - this if a python exception is set. */ +/* Print a python exception trace, print just a message, or print + nothing and clear the python exception, depending on + gdbpy_should_print_stack. Only call this if a python exception is + set. */ void gdbpy_print_stack (void) { - if (gdbpy_should_print_stack) + /* Print "none", just clear exception. */ + if (gdbpy_should_print_stack == python_excp_none) + { + PyErr_Clear (); + } + /* Print "full" message and backtrace. */ + else if (gdbpy_should_print_stack == python_excp_full) { PyErr_Print (); /* PyErr_Print doesn't necessarily end output with a newline. @@ -895,8 +912,34 @@ gdbpy_print_stack (void) printf_filtered. */ begin_line (); } + /* Print "message", just error print message. */ else - PyErr_Clear (); + { + PyObject *ptype, *pvalue, *ptraceback; + char *msg = NULL, *type = NULL; + + PyErr_Fetch (&ptype, &pvalue, &ptraceback); + + /* Fetch the error message contained within ptype, pvalue. */ + msg = gdbpy_exception_to_string (ptype, pvalue); + type = gdbpy_obj_to_string (ptype); + if (msg == NULL) + { + /* An error occurred computing the string representation of the + error message. */ + fprintf_filtered (gdb_stderr, + _("Error occurred computing Python error" \ + "message.\n")); + } + else + fprintf_filtered (gdb_stderr, "Python Exception %s %s: \n", + type, msg); + + Py_XDECREF (ptype); + Py_XDECREF (pvalue); + Py_XDECREF (ptraceback); + xfree (msg); + } } \f @@ -1062,33 +1105,11 @@ gdbpy_breakpoint_has_py_cond (struct breakpoint_object *bp_obj) \f -/* Lists for 'maint set python' commands. */ - -static struct cmd_list_element *maint_set_python_list; -static struct cmd_list_element *maint_show_python_list; - /* Lists for 'set python' commands. */ static struct cmd_list_element *user_set_python_list; static struct cmd_list_element *user_show_python_list; -/* Function for use by 'maint set python' prefix command. */ - -static void -maint_set_python (char *args, int from_tty) -{ - help_list (maint_set_python_list, "maintenance set python ", - class_deprecated, gdb_stdout); -} - -/* Function for use by 'maint show python' prefix command. */ - -static void -maint_show_python (char *args, int from_tty) -{ - cmd_show_list (maint_show_python_list, from_tty, ""); -} - /* Function for use by 'set python' prefix command. */ static void @@ -1138,33 +1159,6 @@ This command is only a placeholder.") #endif /* HAVE_PYTHON */ ); - add_prefix_cmd ("python", no_class, maint_show_python, - _("Prefix command for python maintenance settings."), - &maint_show_python_list, "maintenance show python ", 0, - &maintenance_show_cmdlist); - add_prefix_cmd ("python", no_class, maint_set_python, - _("Prefix command for python maintenance settings."), - &maint_set_python_list, "maintenance set python ", 0, - &maintenance_set_cmdlist); - - add_setshow_boolean_cmd ("print-stack", class_maintenance, - &gdbpy_should_print_stack, _("\ -Enable or disable printing of Python stack dump on error."), _("\ -Show whether Python stack will be printed on error."), _("\ -Enables or disables printing of Python stack traces."), - NULL, NULL, - &maint_set_python_list, - &maint_show_python_list); - - /* Deprecate maint set/show python print-stack in favour of - non-maintenance alternatives. */ - cmd_name = "print-stack"; - cmd = lookup_cmd (&cmd_name, maint_set_python_list, "", -1, 0); - deprecate_cmd (cmd, "set python print-stack"); - cmd_name = "print-stack"; /* Reset name. */ - cmd = lookup_cmd (&cmd_name, maint_show_python_list, "", -1, 0); - deprecate_cmd (cmd, "show python print-stack"); - /* Add set/show python print-stack. */ add_prefix_cmd ("python", no_class, user_show_python, _("Prefix command for python preference settings."), @@ -1176,14 +1170,16 @@ Enables or disables printing of Python stack traces."), &user_set_python_list, "set python ", 0, &setlist); - add_setshow_boolean_cmd ("print-stack", no_class, - &gdbpy_should_print_stack, _("\ -Enable or disable printing of Python stack dump on error."), _("\ -Show whether Python stack will be printed on error."), _("\ -Enables or disables printing of Python stack traces."), - NULL, NULL, - &user_set_python_list, - &user_show_python_list); + 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 diff --git a/gdb/testsuite/gdb.python/py-function.exp b/gdb/testsuite/gdb.python/py-function.exp index d579435..f670d52 100644 --- a/gdb/testsuite/gdb.python/py-function.exp +++ b/gdb/testsuite/gdb.python/py-function.exp @@ -93,7 +93,7 @@ gdb_py_test_multiple "Test Normal Error" \ "NormalError ()" "" \ "end" "" -gdb_test_no_output "set python print-stack on" +gdb_test_no_output "set python print-stack full" gdb_test "print \$normalerror()" "Traceback.*File.*line 5.*in invoke.*RuntimeError.*This is a Normal Error.*" \ "Test a Runtime error. There should be a stack trace." diff --git a/gdb/testsuite/gdb.python/py-prettyprint.exp b/gdb/testsuite/gdb.python/py-prettyprint.exp index b0e7d62..9786d71 100644 --- a/gdb/testsuite/gdb.python/py-prettyprint.exp +++ b/gdb/testsuite/gdb.python/py-prettyprint.exp @@ -97,7 +97,7 @@ proc run_lang_tests {exefile lang} { gdb_test_no_output "python pp_ls_encoding = 'UTF-8'" gdb_test "print estring2" "\"embedded \", <incomplete sequence \\\\302>" - gdb_test_no_output "set python print-stack on" + gdb_test_no_output "set python print-stack full" gdb_test "print hint_error" "Exception: hint failed\r\nhint_error_val" gdb_test "print c" " = container \"container\" with 2 elements = {$nl *.0. = 23,$nl *.1. = 72$nl}" diff --git a/gdb/testsuite/gdb.python/python.exp b/gdb/testsuite/gdb.python/python.exp index 94d6d0d..290a083 100644 --- a/gdb/testsuite/gdb.python/python.exp +++ b/gdb/testsuite/gdb.python/python.exp @@ -195,20 +195,15 @@ gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error strea gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "Test stdout write" gdb_test "python gdb.write(\"Log stream\\n\", stream=gdb.STDLOG)" "Log stream" "Test stdlog write" -# Deprecate maint set/show python print-stack -gdb_test "maint show python print-stack" \ - "Warning: command 'maintenance show python print-stack' is deprecated.*Use 'show python print-stack'.*" \ - "Test deprecation maint show warning." -gdb_test "maint set python print-stack off" \ - "Warning: command 'maintenance set python print-stack' is deprecated.*Use 'set python print-stack'.*" \ - "Test deprecation maint set warning." +# print-stack gdb_test "show python print-stack" \ - "Whether Python stack will be printed on error is off.*" \ - "Test print-backtrace show setting. Default off." -gdb_py_test_silent_cmd "set python print-stack on" \ + "The mode of Python stack printing on error is \"message\".*" \ + "Test print-backtrace show setting. Default is message." +gdb_py_test_silent_cmd "set python print-stack full" \ "Test print-backtrace set setting" 1 gdb_test "show python print-stack" \ - "Whether Python stack will be printed on error is on.*" \ + "The mode of Python stack printing on error is \"full\".*" \ + "Test print-backtrace show setting to full." # Test prompt substituion @@ -313,7 +308,63 @@ gdb_test_multiple "set extended-prompt \\w " \ gdb_test_multiple "set extended-prompt some param \\p{python print-stack} " \ "set extended prompt parameter" { - -re "\[\r\n\]some param True $" { + -re "\[\r\n\]some param full $" { pass "set extended prompt parameter" } } + +# Start with a fresh gdb. +clean_restart ${testfile} + +# The following tests require execution. + +if ![runto_main] then { + fail "Can't run to main" + return 0 +} + +# print-stack settings +gdb_test "show python print-stack" \ + "The mode of Python stack printing on error is \"message\".*" \ + "Test print-backtrace show setting. Default is message." +gdb_py_test_silent_cmd "set python print-stack full" \ + "Test print-backtrace set setting" 1 +gdb_test "show python print-stack" \ + "The mode of Python stack printing on error is \"full\".*" \ + "Test print-backtrace show setting to full." +gdb_py_test_silent_cmd "set python print-stack none" \ + "Test print-backtrace set setting" 1 +gdb_test "show python print-stack" \ + "The mode of Python stack printing on error is \"none\".*" \ + "Test print-backtrace show setting to none." + +gdb_py_test_silent_cmd "set python print-stack message" \ + "Test print-backtrace set setting" 1 + +gdb_py_test_multiple "prompt substitution readline" \ + "python" "" \ + "pCounter = 0" "" \ + "def error_prompt(current):" "" \ + " raise RuntimeError(\"Python exception called\")" "" \ + "end" "" + +gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" { + -re "Python Exception <type 'exceptions.RuntimeError'> Python exception called.*" { + pass "set hook" + } +} + +gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \ + "set the hook to default" 1 + +gdb_py_test_silent_cmd "set python print-stack full" \ + "Test print-backtrace set setting" 1 + +gdb_test_multiple "python gdb.prompt_hook = error_prompt" "set the hook" { + -re "Traceback.*File.*line.*RuntimeError.*Python exception called.*" { + pass "set hook" + } +} + +gdb_py_test_silent_cmd "python gdb.prompt_hook = None" \ + "set the hook to default" 1 ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [python] [patch] PR python/13329 2011-12-15 18:15 ` Phil Muldoon @ 2011-12-15 21:27 ` Eli Zaretskii 2011-12-16 0:51 ` Tom Tromey 1 sibling, 0 replies; 13+ messages in thread From: Eli Zaretskii @ 2011-12-15 21:27 UTC (permalink / raw) To: pmuldoon; +Cc: tromey, gdb-patches, eli > From: Phil Muldoon <pmuldoon@redhat.com> > Cc: gdb-patches@sourceware.org, eli@gnu.org > Date: Thu, 15 Dec 2011 17:42:44 +0000 > > I updated the patch. This patch removes the "maint" command entirely. > > Needs a doc/code review. The documentation part is okay. Thanks. ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [python] [patch] PR python/13329 2011-12-15 18:15 ` Phil Muldoon 2011-12-15 21:27 ` Eli Zaretskii @ 2011-12-16 0:51 ` Tom Tromey 2011-12-16 15:15 ` Phil Muldoon 1 sibling, 1 reply; 13+ messages in thread From: Tom Tromey @ 2011-12-16 0:51 UTC (permalink / raw) To: pmuldoon; +Cc: gdb-patches, eli >>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes: Phil> I updated the patch. This patch removes the "maint" command entirely. Phil> Needs a doc/code review. Phil> OK? Ok. Thanks. Tom ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [python] [patch] PR python/13329 2011-12-16 0:51 ` Tom Tromey @ 2011-12-16 15:15 ` Phil Muldoon 2011-12-16 15:17 ` Tom Tromey 0 siblings, 1 reply; 13+ messages in thread From: Phil Muldoon @ 2011-12-16 15:15 UTC (permalink / raw) To: Tom Tromey; +Cc: gdb-patches, eli Tom Tromey <tromey@redhat.com> writes: >>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes: > > Phil> I updated the patch. This patch removes the "maint" command entirely. > Phil> Needs a doc/code review. > > Phil> OK? > > Ok. Thanks. In the current 7.4 release tarball we have set python print-stack on|off. Beyond that pre-release, it has never been released before. As 7.4 is unreleased as yet, we can change it to this new functionality. Ok for 7.4 branch too (to avoid API breakage)? Cheers, Phil ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [python] [patch] PR python/13329 2011-12-16 15:15 ` Phil Muldoon @ 2011-12-16 15:17 ` Tom Tromey 2011-12-16 17:16 ` Phil Muldoon 0 siblings, 1 reply; 13+ messages in thread From: Tom Tromey @ 2011-12-16 15:17 UTC (permalink / raw) To: pmuldoon; +Cc: gdb-patches, eli Phil> In the current 7.4 release tarball we have set python print-stack Phil> on|off. Beyond that pre-release, it has never been released before. As Phil> 7.4 is unreleased as yet, we can change it to this new functionality. Phil> Ok for 7.4 branch too (to avoid API breakage)? Yes please. Thanks. Tom ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [python] [patch] PR python/13329 2011-12-16 15:17 ` Tom Tromey @ 2011-12-16 17:16 ` Phil Muldoon 0 siblings, 0 replies; 13+ messages in thread From: Phil Muldoon @ 2011-12-16 17:16 UTC (permalink / raw) To: Tom Tromey; +Cc: gdb-patches, eli Tom Tromey <tromey@redhat.com> writes: > Phil> In the current 7.4 release tarball we have set python print-stack > Phil> on|off. Beyond that pre-release, it has never been released before. As > Phil> 7.4 is unreleased as yet, we can change it to this new functionality. > Phil> Ok for 7.4 branch too (to avoid API breakage)? > > Yes please. Thanks. So committed, to head and branch. Also, an additional fix, an obvious fix to HEAD where there were path typos in ChangeLog doc/ChangeLog Cheers, Phil ^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2011-12-16 16:17 UTC | newest] Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2011-11-09 15:36 [python] [patch] PR python/13329 Phil Muldoon 2011-11-09 15:49 ` Kevin Pouget 2011-11-09 20:25 ` Phil Muldoon 2011-11-17 20:55 ` Tom Tromey 2011-11-29 13:57 ` Phil Muldoon 2011-12-08 13:08 ` Phil Muldoon 2011-12-13 19:40 ` Tom Tromey 2011-12-15 18:15 ` Phil Muldoon 2011-12-15 21:27 ` Eli Zaretskii 2011-12-16 0:51 ` Tom Tromey 2011-12-16 15:15 ` Phil Muldoon 2011-12-16 15:17 ` Tom Tromey 2011-12-16 17:16 ` Phil Muldoon
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox