* [PATCH 1/4]: Make "python" start a standard Python prompt
@ 2012-07-16 20:47 Khoo Yit Phang
2012-07-24 21:19 ` Khoo Yit Phang
2012-07-25 6:45 ` Phil Muldoon
0 siblings, 2 replies; 26+ messages in thread
From: Khoo Yit Phang @ 2012-07-16 20:47 UTC (permalink / raw)
To: gdb-patches; +Cc: Khoo Yit Phang
[-- Attachment #1: Type: text/plain, Size: 574 bytes --]
Hi,
I'd like to resubmit my patches to make the "python" command start a standard Python prompt (I've completed the copyright assignment). As discussed back in January-February (http://permalink.gmane.org/gmane.comp.gdb.patches/71971), I've updated my patch to move the old "python" to "python-block".
This is the first of four patches. I'll post more patches that will: 1) update the testsuite to use "python-block"; 2) make Ctrl-C work under Python by raising KeyboardInterrupt; and 3) enable readline under the Python prompt.
Thank you,
Yit
July 16, 2012
[-- Attachment #2: python-interactive --]
[-- Type: application/octet-stream, Size: 6927 bytes --]
# HG changeset patch
# Parent 721a6806846d0e943d1728645c5536fb9cf9a079
Make the "python" command resemble the standard Python interpreter, and move the old behavior to "python-block".
- Use Py_single_input mode to interpret "python" with arguments, so that results of expressions are printed.
- Use Python's built-in interactive loop for "python" without arguments.
gdb/ChangeLog:
2012-07-16 Khoo Yit Phang <khooyp@cs.umd.edu>
Make the "python" command resemble the standard Python interpreter.
* doc/gdb.texinfo (Python Commands): Document the new "python"
command and rename the old "python" to "python-block".
* python/python.c (eval_python_command): New function.
(python_command): For "python" with arguments, call
eval_python_command. For "python" without arguments, call
PyRun_InteractiveLoop.
(python_block_command): New function, copied from the old
python_command.
(_initialize_python): Add "python" command with "py" as alias, and
rename the old "python" to "python-block".
diff --git a/gdb/cli/cli-script.c b/gdb/cli/cli-script.c
--- a/gdb/cli/cli-script.c
+++ b/gdb/cli/cli-script.c
@@ -243,7 +243,7 @@
if (list->control_type == python_control)
{
- ui_out_field_string (uiout, NULL, "python");
+ ui_out_field_string (uiout, NULL, "python-block");
ui_out_text (uiout, "\n");
/* Don't indent python code at all. */
print_command_lines (uiout, *list->body_list, 0);
@@ -1002,7 +1002,7 @@
first_arg++;
*command = build_command_line (commands_control, first_arg);
}
- else if (p_end - p == 6 && !strncmp (p, "python", 6))
+ else if (p_end - p == 12 && !strncmp (p, "python-block", 12))
{
/* Note that we ignore the inline "python command" form
here. */
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -22524,30 +22524,44 @@
@cindex python commands
@cindex commands to access python
-@value{GDBN} provides one command for accessing the Python interpreter,
+@value{GDBN} provides two command for accessing the Python interpreter,
and one related setting:
@table @code
@kindex python
@item python @r{[}@var{code}@r{]}
-The @code{python} command can be used to evaluate Python code.
-
-If given an argument, the @code{python} command will evaluate the
-argument as a Python command. For example:
-
-@smallexample
-(@value{GDBP}) python print 23
+The @code{python} command can be used to start an interactive Python
+prompt.
+
+Alternatively, a single-line Python command can be given as an
+argument, and if the command is an expression, the result will be
+printed. For example:
+
+@smallexample
+(@value{GDBP}) python 2 + 3
+5
+@end smallexample
+
+@kindex python-block
+@item python-block @r{[}@var{code}@r{]}
+The @code{python-block} command can be used to evaluate Python code.
+
+If given an argument, the @code{python-block} command will evaluate
+the argument as a Python command. For example:
+
+@smallexample
+(@value{GDBP}) python-block print 23
23
@end smallexample
-If you do not provide an argument to @code{python}, it will act as a
-multi-line command, like @code{define}. In this case, the Python
+If you do not provide an argument to @code{python-block}, it will act
+as a multi-line command, like @code{define}. In this case, the Python
script is made up of subsequent command lines, given after the
-@code{python} command. This command list is terminated using a line
-containing @code{end}. For example:
-
-@smallexample
-(@value{GDBP}) python
+@code{python-block} command. This command list is terminated using a
+line containing @code{end}. For example:
+
+@smallexample
+(@value{GDBP}) python-block
Type python script
End with a line saying just "end".
>print 23
diff --git a/gdb/python/python.c b/gdb/python/python.c
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -200,6 +200,68 @@
return make_cleanup (restore_python_env, env);
}
+/* Evaluate a Python command like PyRun_SimpleString, but uses
+ Py_single_input which prints the result of expressions, and does
+ not automatically print the stack on errors. */
+
+static int
+eval_python_command (const char *command)
+{
+ PyObject *m, *d, *v;
+
+ m = PyImport_AddModule ("__main__");
+ if (m == NULL)
+ return -1;
+
+ d = PyModule_GetDict (m);
+ if (d == NULL)
+ return -1;
+ v = PyRun_StringFlags (command, Py_single_input, d, d, NULL);
+ if (v == NULL)
+ return -1;
+
+ Py_DECREF (v);
+ if (Py_FlushLine ())
+ PyErr_Clear ();
+
+ return 0;
+}
+
+/* Implementation of the gdb "python" command. */
+
+static void
+python_command (char *arg, int from_tty)
+{
+ struct cleanup *cleanup;
+ int err;
+
+ cleanup = make_cleanup_restore_integer (&interpreter_async);
+ interpreter_async = 0;
+
+ while (arg && *arg && isspace (*arg))
+ ++arg;
+
+ ensure_python_env (get_current_arch (), current_language);
+
+ if (arg && *arg)
+ {
+ err = eval_python_command (arg);
+ }
+ else
+ {
+ err = PyRun_InteractiveLoop(instream, "<stdin>");
+ dont_repeat ();
+ }
+
+ if (err)
+ {
+ gdbpy_print_stack ();
+ error (_("Error while executing Python code."));
+ }
+
+ do_cleanups (cleanup);
+}
+
/* A wrapper around PyRun_SimpleFile. FILE is the Python script to run
named FILENAME.
@@ -302,10 +362,10 @@
do_cleanups (cleanup);
}
-/* Implementation of the gdb "python" command. */
+/* Implementation of the gdb "python-block" command. */
static void
-python_command (char *arg, int from_tty)
+python_block_command (char *arg, int from_tty)
{
struct cleanup *cleanup;
@@ -1137,7 +1197,7 @@
#else /* HAVE_PYTHON */
-/* Dummy implementation of the gdb "python" command. */
+/* Dummy implementation of the gdb "python/python-block" command. */
static void
python_command (char *arg, int from_tty)
@@ -1156,6 +1216,12 @@
}
}
+static void
+python_block_command (char *arg, int from_tty)
+{
+ python_command (arg, from_tty);
+}
+
void
eval_python_from_control_command (struct command_line *cmd)
{
@@ -1225,11 +1291,33 @@
add_com ("python", class_obscure, python_command,
#ifdef HAVE_PYTHON
_("\
+Start a Python interactive 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 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 ("py", "python", class_obscure, 1);
+
+ add_com ("python-block", class_obscure, python_block_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\
+ python-block print 23\n\
\n\
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-16 20:47 [PATCH 1/4]: Make "python" start a standard Python prompt Khoo Yit Phang
@ 2012-07-24 21:19 ` Khoo Yit Phang
2012-07-25 6:45 ` Phil Muldoon
1 sibling, 0 replies; 26+ messages in thread
From: Khoo Yit Phang @ 2012-07-24 21:19 UTC (permalink / raw)
To: Khoo Yit Phang; +Cc: gdb-patches
Hi,
Ping; any interest in this?
Yit
July 16, 2012
On Jul 16, 2012, at 4:47 PM, Khoo Yit Phang wrote:
> Hi,
>
> I'd like to resubmit my patches to make the "python" command start a standard Python prompt (I've completed the copyright assignment). As discussed back in January-February (http://permalink.gmane.org/gmane.comp.gdb.patches/71971), I've updated my patch to move the old "python" to "python-block".
>
> This is the first of four patches. I'll post more patches that will: 1) update the testsuite to use "python-block"; 2) make Ctrl-C work under Python by raising KeyboardInterrupt; and 3) enable readline under the Python prompt.
>
> Thank you,
>
> Yit
> July 16, 2012
>
> <python-interactive>
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-16 20:47 [PATCH 1/4]: Make "python" start a standard Python prompt Khoo Yit Phang
2012-07-24 21:19 ` Khoo Yit Phang
@ 2012-07-25 6:45 ` Phil Muldoon
2012-07-25 16:18 ` Tom Tromey
1 sibling, 1 reply; 26+ messages in thread
From: Phil Muldoon @ 2012-07-25 6:45 UTC (permalink / raw)
To: gdb-patches
On 07/16/2012 09:47 PM, Khoo Yit Phang wrote:
> Hi,
>
> I'd like to resubmit my patches to make the "python" command start a standard Python prompt (I've completed the copyright assignment). As discussed back in January-February (http://permalink.gmane.org/gmane.comp.gdb.patches/71971), I've updated my patch to move the old "python" to "python-block".
Apologies for the delay, I've been mulling if this constitutes an API
change. Because we have an API promise, we cannot materially alter
the behavior of an API without providing compatibility that is
transparent and automatic for older scripts.
I am not sure what the implication for your change is. If a user has
a script (or some code in .gdbinit, or whatever ) that uses "python"
will it break that user's script? If so it is probably better to leave
python as it is, and use python-interactive (or whatever) for the
interactive prompt. Judging that you changed the testsuite to use
python-block, I am guessing it is a material and non-compatible
change? I guess I need more data, testing. I am replying here now as
you pinged the patch (which is totally fine, just updating you).
Cheers
Phil
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-25 6:45 ` Phil Muldoon
@ 2012-07-25 16:18 ` Tom Tromey
2012-07-25 16:58 ` Khoo Yit Phang
0 siblings, 1 reply; 26+ messages in thread
From: Tom Tromey @ 2012-07-25 16:18 UTC (permalink / raw)
To: Phil Muldoon; +Cc: gdb-patches
>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:
Phil> If a user has a script (or some code in .gdbinit, or whatever )
Phil> that uses "python" will it break that user's script?
This is my question as well.
I think it is ok to change the interactive behavior of the "python"
command, but not the behavior in a script.
Tom
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-25 16:18 ` Tom Tromey
@ 2012-07-25 16:58 ` Khoo Yit Phang
2012-07-25 17:20 ` Matt Rice
2012-07-25 17:43 ` Tom Tromey
0 siblings, 2 replies; 26+ messages in thread
From: Khoo Yit Phang @ 2012-07-25 16:58 UTC (permalink / raw)
To: Tom Tromey; +Cc: Khoo Yit Phang, Phil Muldoon, gdb-patches
Hi,
On Jul 25, 2012, at 12:18 PM, Tom Tromey wrote:
>>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:
>
> Phil> If a user has a script (or some code in .gdbinit, or whatever )
> Phil> that uses "python" will it break that user's script?
>
> This is my question as well.
>
> I think it is ok to change the interactive behavior of the "python"
> command, but not the behavior in a script.
>
> Tom
My original patch back in January tried to preserve the behavior of "python" based "from_tty": if it was true, "python" would be interactive; otherwise, it would not. But there were several objections to that, I guess since "from_tty" is hard to track (see http://thread.gmane.org/gmane.comp.gdb.patches/71971). The current implementation is based on the discussion then.
One more option is to move the Python REPL to "python-repl" and alias it to "py", and leave the old "python" as is. The "py" alias gives a convenient short command, "python-repl" can be used from a script to start a REPL, and the old "python" remains as is.
Yit
July 25, 2012
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-25 16:58 ` Khoo Yit Phang
@ 2012-07-25 17:20 ` Matt Rice
2012-07-25 17:29 ` Khoo Yit Phang
2012-07-25 17:43 ` Tom Tromey
1 sibling, 1 reply; 26+ messages in thread
From: Matt Rice @ 2012-07-25 17:20 UTC (permalink / raw)
To: Khoo Yit Phang; +Cc: Tom Tromey, Phil Muldoon, gdb-patches
On Wed, Jul 25, 2012 at 9:58 AM, Khoo Yit Phang <khooyp@cs.umd.edu> wrote:
> One more option is to move the Python REPL to "python-repl" and alias it to "py", and leave the old "python" as is. The "py" alias gives a convenient short command, "python-repl" can be used from a script to start a REPL, and the old "python" remains as is.
>
FWIW i have used the 'py' alias in scripts numerous times (though
afaict the ones i've released into the wild have been switched to
'python' before release), though I believe there to be a few examples
of it on stack overflow (kind of hard to search for though).
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-25 17:20 ` Matt Rice
@ 2012-07-25 17:29 ` Khoo Yit Phang
0 siblings, 0 replies; 26+ messages in thread
From: Khoo Yit Phang @ 2012-07-25 17:29 UTC (permalink / raw)
To: Matt Rice; +Cc: Khoo Yit Phang, Tom Tromey, Phil Muldoon, gdb-patches
Hi,
On Jul 25, 2012, at 1:20 PM, Matt Rice wrote:
> On Wed, Jul 25, 2012 at 9:58 AM, Khoo Yit Phang <khooyp@cs.umd.edu> wrote:
>
>> One more option is to move the Python REPL to "python-repl" and alias it to "py", and leave the old "python" as is. The "py" alias gives a convenient short command, "python-repl" can be used from a script to start a REPL, and the old "python" remains as is.
>>
>
> FWIW i have used the 'py' alias in scripts numerous times (though
> afaict the ones i've released into the wild have been switched to
> 'python' before release), though I believe there to be a few examples
> of it on stack overflow (kind of hard to search for though).
I should mention, "py-repl" and "python" have almost the same behavior when used with an argument (the differences are just that "py-repl" will also print the result of expressions, and uses gdbpy_print_stack to print errors). If "py" is typically used for single-line Python scripts in the wild, then I believe that such scripts will still work, just maybe with different console outputs.
Yit
July 25, 2012
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-25 16:58 ` Khoo Yit Phang
2012-07-25 17:20 ` Matt Rice
@ 2012-07-25 17:43 ` Tom Tromey
2012-07-25 17:55 ` Khoo Yit Phang
1 sibling, 1 reply; 26+ messages in thread
From: Tom Tromey @ 2012-07-25 17:43 UTC (permalink / raw)
To: Khoo Yit Phang; +Cc: Phil Muldoon, gdb-patches
>>>>> "Yit" == Khoo Yit Phang <khooyp@cs.umd.edu> writes:
Yit> My original patch back in January tried to preserve the behavior of
Yit> "python" based "from_tty": if it was true, "python" would be
Yit> interactive; otherwise, it would not. But there were several
Yit> objections to that, I guess since "from_tty" is hard to track (see
Yit> http://thread.gmane.org/gmane.comp.gdb.patches/71971). The current
Yit> implementation is based on the discussion then.
I vaguely remember it. I have a suspicion that I've changed my mind
since the old thread, but I didn't go back and read to see :)
I don't know what would make from_tty hard to track, but ok.
I just think it is very likely that "python" commands are in .gdbinits
and other places, and that breaking these would be bad.
Yit> One more option is to move the Python REPL to "python-repl" and alias
Yit> it to "py", and leave the old "python" as is. The "py" alias gives a
Yit> convenient short command, "python-repl" can be used from a script to
Yit> start a REPL, and the old "python" remains as is.
Like Matt says, "py" is probably already in use (I feel certain I've
seen it) and if anything we should do the converse -- alias "py" to
"python" if a new command goes in.
"python-repl" would be fine by me.
Tom
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-25 17:43 ` Tom Tromey
@ 2012-07-25 17:55 ` Khoo Yit Phang
2012-07-25 17:59 ` Tom Tromey
0 siblings, 1 reply; 26+ messages in thread
From: Khoo Yit Phang @ 2012-07-25 17:55 UTC (permalink / raw)
To: Tom Tromey; +Cc: Khoo Yit Phang, Phil Muldoon, gdb-patches
Hi,
Just to throw out another suggestion: make the new command "python-interactive" and alias it to "pi", and leave the old "python" and alias it to "py". Best of both worlds?
Yit
July 25, 2012
On Jul 25, 2012, at 1:43 PM, Tom Tromey wrote:
>>>>>> "Yit" == Khoo Yit Phang <khooyp@cs.umd.edu> writes:
>
> Yit> My original patch back in January tried to preserve the behavior of
> Yit> "python" based "from_tty": if it was true, "python" would be
> Yit> interactive; otherwise, it would not. But there were several
> Yit> objections to that, I guess since "from_tty" is hard to track (see
> Yit> http://thread.gmane.org/gmane.comp.gdb.patches/71971). The current
> Yit> implementation is based on the discussion then.
>
> I vaguely remember it. I have a suspicion that I've changed my mind
> since the old thread, but I didn't go back and read to see :)
>
> I don't know what would make from_tty hard to track, but ok.
>
> I just think it is very likely that "python" commands are in .gdbinits
> and other places, and that breaking these would be bad.
>
> Yit> One more option is to move the Python REPL to "python-repl" and alias
> Yit> it to "py", and leave the old "python" as is. The "py" alias gives a
> Yit> convenient short command, "python-repl" can be used from a script to
> Yit> start a REPL, and the old "python" remains as is.
>
> Like Matt says, "py" is probably already in use (I feel certain I've
> seen it) and if anything we should do the converse -- alias "py" to
> "python" if a new command goes in.
>
> "python-repl" would be fine by me.
>
> Tom
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-25 17:55 ` Khoo Yit Phang
@ 2012-07-25 17:59 ` Tom Tromey
2012-07-25 21:07 ` Khoo Yit Phang
0 siblings, 1 reply; 26+ messages in thread
From: Tom Tromey @ 2012-07-25 17:59 UTC (permalink / raw)
To: Khoo Yit Phang; +Cc: Phil Muldoon, gdb-patches
>>>>> "Yit" == Khoo Yit Phang <khooyp@cs.umd.edu> writes:
Yit> Just to throw out another suggestion: make the new command
Yit> "python-interactive" and alias it to "pi", and leave the old "python"
Yit> and alias it to "py". Best of both worlds?
Sounds good, do it.
Tom
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-25 17:59 ` Tom Tromey
@ 2012-07-25 21:07 ` Khoo Yit Phang
2012-07-26 17:38 ` Tom Tromey
0 siblings, 1 reply; 26+ messages in thread
From: Khoo Yit Phang @ 2012-07-25 21:07 UTC (permalink / raw)
To: Tom Tromey; +Cc: Khoo Yit Phang, Phil Muldoon, gdb-patches
[-- Attachment #1: Type: text/plain, Size: 194 bytes --]
Hi,
Here's the updated patch that creates a new command "python-interactive", aliased to "pi", that starts a standard Python REPL; and also aliases "python" to "py".
Yit
July 25, 2012
[-- Attachment #2: python-interactive --]
[-- Type: application/octet-stream, Size: 5196 bytes --]
# HG changeset patch
# Parent 2513adddae6097d794942a98815e0482a2c193c7
Add a new "python-interactive" command that starts a standard Python prompt.
- Use Py_single_input mode to interpret "python-interactive" with arguments, so that results of expressions are printed.
- Use Python's built-in interactive loop for "python-interactive" without arguments.
gdb/ChangeLog:
2012-07-16 Khoo Yit Phang <khooyp@cs.umd.edu>
Add a new "python-interactive" command that starts a standard
Python prompt.
* doc/gdb.texinfo (Python Commands): Document the new
"python-interactive" command.
* python/python.c (eval_python_command): New function.
(python_interactive_command): For "python-interactive" with
arguments, call eval_python_command. For "python-interactive"
without arguments, call PyRun_InteractiveLoop.
(_initialize_python): Add "python-interactive" command with
"pi" as alias, and add "py" as an alias to "python".
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -22539,12 +22539,30 @@
@cindex python commands
@cindex commands to access python
-@value{GDBN} provides one command for accessing the Python interpreter,
+@value{GDBN} provides two commands for accessing the Python interpreter,
and one related setting:
@table @code
+@kindex python-interactive
+@kindex pi
+@item python-interactive @r{[}@var{code}@r{]}
+@item pi @r{[}@var{code}@r{]}
+The @code{python-interactive} command can be used to start an
+interactive Python prompt.
+
+Alternatively, a single-line Python command can be given as an
+argument, and if the command is an expression, the result will be
+printed. For example:
+
+@smallexample
+(@value{GDBP}) python-interactive 2 + 3
+5
+@end smallexample
+
@kindex python
+@kindex py
@item python @r{[}@var{code}@r{]}
+@itemx py @r{[}@var{code}@r{]}
The @code{python} command can be used to evaluate Python code.
If given an argument, the @code{python} command will evaluate the
diff --git a/gdb/python/python.c b/gdb/python/python.c
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -151,6 +151,68 @@
return make_cleanup (restore_python_env, env);
}
+/* Evaluate a Python command like PyRun_SimpleString, but uses
+ Py_single_input which prints the result of expressions, and does
+ not automatically print the stack on errors. */
+
+static int
+eval_python_command (const char *command)
+{
+ PyObject *m, *d, *v;
+
+ m = PyImport_AddModule ("__main__");
+ if (m == NULL)
+ return -1;
+
+ d = PyModule_GetDict (m);
+ if (d == NULL)
+ return -1;
+ v = PyRun_StringFlags (command, Py_single_input, d, d, NULL);
+ if (v == NULL)
+ return -1;
+
+ Py_DECREF (v);
+ if (Py_FlushLine ())
+ PyErr_Clear ();
+
+ return 0;
+}
+
+/* Implementation of the gdb "python-interactive" command. */
+
+static void
+python_interactive_command (char *arg, int from_tty)
+{
+ struct cleanup *cleanup;
+ int err;
+
+ cleanup = make_cleanup_restore_integer (&interpreter_async);
+ interpreter_async = 0;
+
+ while (arg && *arg && isspace (*arg))
+ ++arg;
+
+ ensure_python_env (get_current_arch (), current_language);
+
+ if (arg && *arg)
+ {
+ err = eval_python_command (arg);
+ }
+ else
+ {
+ err = PyRun_InteractiveLoop(instream, "<stdin>");
+ dont_repeat ();
+ }
+
+ if (err)
+ {
+ gdbpy_print_stack ();
+ error (_("Error while executing Python code."));
+ }
+
+ do_cleanups (cleanup);
+}
+
/* A wrapper around PyRun_SimpleFile. FILE is the Python script to run
named FILENAME.
@@ -1087,10 +1149,11 @@
#else /* HAVE_PYTHON */
-/* Dummy implementation of the gdb "python" command. */
+/* Dummy implementation of the gdb "python-interactive" and "python"
+ command. */
static void
-python_command (char *arg, int from_tty)
+python_interactive_command (char *arg, int from_tty)
{
while (arg && *arg && isspace (*arg))
++arg;
@@ -1106,6 +1169,12 @@
}
}
+static void
+python_command (char *arg, int from_tty)
+{
+ python_interactive_command (arg, from_tty);
+}
+
void
eval_python_from_control_command (struct command_line *cmd)
{
@@ -1172,6 +1241,29 @@
char *cmd_name;
struct cmd_list_element *cmd;
+ add_com ("python-interactive", class_obscure,
+ python_interactive_command,
+#ifdef HAVE_PYTHON
+ _("\
+Start a Python interactive 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
_("\
@@ -1192,6 +1284,7 @@
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,
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-25 21:07 ` Khoo Yit Phang
@ 2012-07-26 17:38 ` Tom Tromey
2012-07-26 17:52 ` Khoo Yit Phang
0 siblings, 1 reply; 26+ messages in thread
From: Tom Tromey @ 2012-07-26 17:38 UTC (permalink / raw)
To: Khoo Yit Phang; +Cc: Phil Muldoon, gdb-patches
>>>>> "Yit" == Khoo Yit Phang <khooyp@cs.umd.edu> writes:
Yit> Here's the updated patch that creates a new command
Yit> "python-interactive", aliased to "pi", that starts a standard Python
Yit> REPL; and also aliases "python" to "py".
Looks pretty good to me.
I think it could use a NEWS entry.
Aside from doc review I have a couple of nits -- nothing serious.
Yit> + if (arg && *arg)
Yit> + {
Indentation looks wrong.
Yit> + err = PyRun_InteractiveLoop(instream, "<stdin>");
Space before open paren.
Yit> + if (err)
Yit> + {
Indentation.
The code parts are ok with those formatting fixes.
thanks,
Tom
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-26 17:38 ` Tom Tromey
@ 2012-07-26 17:52 ` Khoo Yit Phang
2012-07-26 18:06 ` Eli Zaretskii
0 siblings, 1 reply; 26+ messages in thread
From: Khoo Yit Phang @ 2012-07-26 17:52 UTC (permalink / raw)
To: Tom Tromey; +Cc: Khoo Yit Phang, Phil Muldoon, gdb-patches
Hi,
On Jul 26, 2012, at 1:37 PM, Tom Tromey wrote:
>>>>>> "Yit" == Khoo Yit Phang <khooyp@cs.umd.edu> writes:
>
> Yit> Here's the updated patch that creates a new command
> Yit> "python-interactive", aliased to "pi", that starts a standard Python
> Yit> REPL; and also aliases "python" to "py".
>
> Looks pretty good to me.
>
> I think it could use a NEWS entry.
Do you mean I should edit the NEWS file (under "Changes since GDB 7.5")? How's the following:
* New commands
** "python-interactive [command]"
"pi [command]"
Starts a Python interactive prompt, or evaluate the optional
command and print the result of expressions.
** "py" is a new alias for "python"
Thanks,
Yit
July 26, 2012
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-26 17:52 ` Khoo Yit Phang
@ 2012-07-26 18:06 ` Eli Zaretskii
2012-07-26 18:10 ` Khoo Yit Phang
0 siblings, 1 reply; 26+ messages in thread
From: Eli Zaretskii @ 2012-07-26 18:06 UTC (permalink / raw)
To: Khoo Yit Phang; +Cc: tromey, khooyp, pmuldoon, gdb-patches
> From: Khoo Yit Phang <khooyp@cs.umd.edu>
> Date: Thu, 26 Jul 2012 13:52:06 -0400
> Cc: Khoo Yit Phang <khooyp@cs.umd.edu>, Phil Muldoon <pmuldoon@redhat.com>, gdb-patches@sourceware.org
>
> * New commands
>
> ** "python-interactive [command]"
> "pi [command]"
> Starts a Python interactive prompt, or evaluate the optional
> command and print the result of expressions.
"evaluates" and "prints", for compatibility with "starts".
OK with those changes.
Thanks.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-26 18:06 ` Eli Zaretskii
@ 2012-07-26 18:10 ` Khoo Yit Phang
2012-07-27 2:32 ` Khoo Yit Phang
0 siblings, 1 reply; 26+ messages in thread
From: Khoo Yit Phang @ 2012-07-26 18:10 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: Khoo Yit Phang, tromey, pmuldoon, gdb-patches
[-- Attachment #1: Type: text/plain, Size: 60 bytes --]
Hi,
Here's the updated patch.
Thanks!
Yit
July 26, 2012
[-- Attachment #2: python-interactive --]
[-- Type: application/octet-stream, Size: 5672 bytes --]
# HG changeset patch
# Parent 57c4a59e60c5f8173ad78c87ddde915b3c90fc78
Add a new "python-interactive" command that starts a standard Python prompt.
- Use Py_single_input mode to interpret "python-interactive" with arguments, so that results of expressions are printed.
- Use Python's built-in interactive loop for "python-interactive" without arguments.
gdb/ChangeLog:
2012-07-16 Khoo Yit Phang <khooyp@cs.umd.edu>
Add a new "python-interactive" command that starts a standard
Python prompt.
* doc/gdb.texinfo (Python Commands): Document the new
"python-interactive" command.
* python/python.c (eval_python_command): New function.
(python_interactive_command): For "python-interactive" with
arguments, call eval_python_command. For "python-interactive"
without arguments, call PyRun_InteractiveLoop.
(_initialize_python): Add "python-interactive" command with
"pi" as alias, and add "py" as an alias to "python".
diff --git a/gdb/NEWS b/gdb/NEWS
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,15 @@
*** Changes since GDB 7.5
+* New commands
+
+ ** "python-interactive [command]"
+ "pi [command]"
+ Starts a Python interactive prompt, or evaluates the optional
+ command and prints the result of expressions.
+
+ ** "py" is a new alias for "python".
+
*** Changes in GDB 7.5
* GDB now supports x32 ABI. Visit <http://sites.google.com/site/x32abi/>
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -22539,12 +22539,30 @@
@cindex python commands
@cindex commands to access python
-@value{GDBN} provides one command for accessing the Python interpreter,
+@value{GDBN} provides two commands for accessing the Python interpreter,
and one related setting:
@table @code
+@kindex python-interactive
+@kindex pi
+@item python-interactive @r{[}@var{code}@r{]}
+@itemx pi @r{[}@var{code}@r{]}
+The @code{python-interactive} command can be used to start an
+interactive Python prompt.
+
+Alternatively, a single-line Python command can be given as an
+argument, and if the command is an expression, the result will be
+printed. For example:
+
+@smallexample
+(@value{GDBP}) python-interactive 2 + 3
+5
+@end smallexample
+
@kindex python
+@kindex py
@item python @r{[}@var{code}@r{]}
+@itemx py @r{[}@var{code}@r{]}
The @code{python} command can be used to evaluate Python code.
If given an argument, the @code{python} command will evaluate the
diff --git a/gdb/python/python.c b/gdb/python/python.c
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -151,6 +151,68 @@
return make_cleanup (restore_python_env, env);
}
+/* Evaluate a Python command like PyRun_SimpleString, but uses
+ Py_single_input which prints the result of expressions, and does
+ not automatically print the stack on errors. */
+
+static int
+eval_python_command (const char *command)
+{
+ PyObject *m, *d, *v;
+
+ m = PyImport_AddModule ("__main__");
+ if (m == NULL)
+ return -1;
+
+ d = PyModule_GetDict (m);
+ if (d == NULL)
+ return -1;
+ v = PyRun_StringFlags (command, Py_single_input, d, d, NULL);
+ if (v == NULL)
+ return -1;
+
+ Py_DECREF (v);
+ if (Py_FlushLine ())
+ PyErr_Clear ();
+
+ return 0;
+}
+
+/* Implementation of the gdb "python-interactive" command. */
+
+static void
+python_interactive_command (char *arg, int from_tty)
+{
+ struct cleanup *cleanup;
+ int err;
+
+ cleanup = make_cleanup_restore_integer (&interpreter_async);
+ interpreter_async = 0;
+
+ while (arg && *arg && isspace (*arg))
+ ++arg;
+
+ ensure_python_env (get_current_arch (), current_language);
+
+ if (arg && *arg)
+ {
+ err = eval_python_command (arg);
+ }
+ else
+ {
+ err = PyRun_InteractiveLoop (instream, "<stdin>");
+ dont_repeat ();
+ }
+
+ if (err)
+ {
+ gdbpy_print_stack ();
+ error (_("Error while executing Python code."));
+ }
+
+ do_cleanups (cleanup);
+}
+
/* A wrapper around PyRun_SimpleFile. FILE is the Python script to run
named FILENAME.
@@ -1087,10 +1149,11 @@
#else /* HAVE_PYTHON */
-/* Dummy implementation of the gdb "python" command. */
+/* Dummy implementation of the gdb "python-interactive" and "python"
+ command. */
static void
-python_command (char *arg, int from_tty)
+python_interactive_command (char *arg, int from_tty)
{
while (arg && *arg && isspace (*arg))
++arg;
@@ -1106,6 +1169,12 @@
}
}
+static void
+python_command (char *arg, int from_tty)
+{
+ python_interactive_command (arg, from_tty);
+}
+
void
eval_python_from_control_command (struct command_line *cmd)
{
@@ -1172,6 +1241,29 @@
char *cmd_name;
struct cmd_list_element *cmd;
+ add_com ("python-interactive", class_obscure,
+ python_interactive_command,
+#ifdef HAVE_PYTHON
+ _("\
+Start a Python interactive 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
_("\
@@ -1192,6 +1284,7 @@
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,
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-26 18:10 ` Khoo Yit Phang
@ 2012-07-27 2:32 ` Khoo Yit Phang
2012-07-27 19:23 ` Tom Tromey
2012-07-28 6:53 ` Eli Zaretskii
0 siblings, 2 replies; 26+ messages in thread
From: Khoo Yit Phang @ 2012-07-27 2:32 UTC (permalink / raw)
To: Khoo Yit Phang; +Cc: Eli Zaretskii, tromey, pmuldoon, gdb-patches
[-- Attachment #1: Type: text/plain, Size: 142 bytes --]
Hi,
My working copy was a little old when I made the previous patch and has conflicts for NEWS. Here's a new one.
Yit
July 26, 2012
[-- Attachment #2: python-interactive --]
[-- Type: application/octet-stream, Size: 5748 bytes --]
# HG changeset patch
# Parent 49b1711f6994a0ec1cc0d025b211975c8563f3fc
Add a new "python-interactive" command that starts a standard Python prompt.
- Use Py_single_input mode to interpret "python-interactive" with arguments, so that results of expressions are printed.
- Use Python's built-in interactive loop for "python-interactive" without arguments.
gdb/ChangeLog:
2012-07-16 Khoo Yit Phang <khooyp@cs.umd.edu>
Add a new "python-interactive" command that starts a standard
Python interactive prompt with "pi" as alias, and add "py" as
an alias to "python".
* NEWS: Mention the new commands.
* doc/gdb.texinfo (Python Commands): Document the new
commands.
* python/python.c (eval_python_command): New function.
(python_interactive_command): For "python-interactive" with
arguments, call eval_python_command. For "python-interactive"
without arguments, call PyRun_InteractiveLoop.
(_initialize_python): Add "python-interactive" command with
"pi" as alias, and add "py" as an alias to "python".
diff --git a/gdb/NEWS b/gdb/NEWS
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -17,6 +17,14 @@
maint info bfds
List the BFDs known to GDB.
+python-interactive [command]
+pi [command]
+ Start a Python interactive prompt, or evaluate the optional command
+ and print the result of expressions.
+
+py [command]
+ "py" is a new alias for "python".
+
*** Changes in GDB 7.5
* GDB now supports x32 ABI. Visit <http://sites.google.com/site/x32abi/>
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -22539,12 +22539,30 @@
@cindex python commands
@cindex commands to access python
-@value{GDBN} provides one command for accessing the Python interpreter,
+@value{GDBN} provides two commands for accessing the Python interpreter,
and one related setting:
@table @code
+@kindex python-interactive
+@kindex pi
+@item python-interactive @r{[}@var{code}@r{]}
+@itemx pi @r{[}@var{code}@r{]}
+The @code{python-interactive} command can be used to start an
+interactive Python prompt.
+
+Alternatively, a single-line Python command can be given as an
+argument, and if the command is an expression, the result will be
+printed. For example:
+
+@smallexample
+(@value{GDBP}) python-interactive 2 + 3
+5
+@end smallexample
+
@kindex python
+@kindex py
@item python @r{[}@var{code}@r{]}
+@itemx py @r{[}@var{code}@r{]}
The @code{python} command can be used to evaluate Python code.
If given an argument, the @code{python} command will evaluate the
diff --git a/gdb/python/python.c b/gdb/python/python.c
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -151,6 +151,68 @@
return make_cleanup (restore_python_env, env);
}
+/* Evaluate a Python command like PyRun_SimpleString, but uses
+ Py_single_input which prints the result of expressions, and does
+ not automatically print the stack on errors. */
+
+static int
+eval_python_command (const char *command)
+{
+ PyObject *m, *d, *v;
+
+ m = PyImport_AddModule ("__main__");
+ if (m == NULL)
+ return -1;
+
+ d = PyModule_GetDict (m);
+ if (d == NULL)
+ return -1;
+ v = PyRun_StringFlags (command, Py_single_input, d, d, NULL);
+ if (v == NULL)
+ return -1;
+
+ Py_DECREF (v);
+ if (Py_FlushLine ())
+ PyErr_Clear ();
+
+ return 0;
+}
+
+/* Implementation of the gdb "python-interactive" command. */
+
+static void
+python_interactive_command (char *arg, int from_tty)
+{
+ struct cleanup *cleanup;
+ int err;
+
+ cleanup = make_cleanup_restore_integer (&interpreter_async);
+ interpreter_async = 0;
+
+ while (arg && *arg && isspace (*arg))
+ ++arg;
+
+ ensure_python_env (get_current_arch (), current_language);
+
+ if (arg && *arg)
+ {
+ err = eval_python_command (arg);
+ }
+ else
+ {
+ err = PyRun_InteractiveLoop (instream, "<stdin>");
+ dont_repeat ();
+ }
+
+ if (err)
+ {
+ gdbpy_print_stack ();
+ error (_("Error while executing Python code."));
+ }
+
+ do_cleanups (cleanup);
+}
+
/* A wrapper around PyRun_SimpleFile. FILE is the Python script to run
named FILENAME.
@@ -1087,10 +1149,11 @@
#else /* HAVE_PYTHON */
-/* Dummy implementation of the gdb "python" command. */
+/* Dummy implementation of the gdb "python-interactive" and "python"
+ command. */
static void
-python_command (char *arg, int from_tty)
+python_interactive_command (char *arg, int from_tty)
{
while (arg && *arg && isspace (*arg))
++arg;
@@ -1106,6 +1169,12 @@
}
}
+static void
+python_command (char *arg, int from_tty)
+{
+ python_interactive_command (arg, from_tty);
+}
+
void
eval_python_from_control_command (struct command_line *cmd)
{
@@ -1172,6 +1241,29 @@
char *cmd_name;
struct cmd_list_element *cmd;
+ add_com ("python-interactive", class_obscure,
+ python_interactive_command,
+#ifdef HAVE_PYTHON
+ _("\
+Start a Python interactive 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
_("\
@@ -1192,6 +1284,7 @@
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,
[-- Attachment #3: Type: text/plain, Size: 158 bytes --]
On Jul 26, 2012, at 2:10 PM, Khoo Yit Phang wrote:
> Hi,
>
> Here's the updated patch.
>
> Thanks!
>
> Yit
> July 26, 2012
>
> <python-interactive>
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-27 2:32 ` Khoo Yit Phang
@ 2012-07-27 19:23 ` Tom Tromey
2012-07-28 6:53 ` Eli Zaretskii
1 sibling, 0 replies; 26+ messages in thread
From: Tom Tromey @ 2012-07-27 19:23 UTC (permalink / raw)
To: Khoo Yit Phang; +Cc: Eli Zaretskii, pmuldoon, gdb-patches
>>>>> "Yit" == Khoo Yit Phang <khooyp@cs.umd.edu> writes:
Yit> My working copy was a little old when I made the previous patch and
Yit> has conflicts for NEWS. Here's a new one.
Code parts are ok. Thanks.
Tom
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-27 2:32 ` Khoo Yit Phang
2012-07-27 19:23 ` Tom Tromey
@ 2012-07-28 6:53 ` Eli Zaretskii
2012-07-28 14:35 ` Khoo Yit Phang
1 sibling, 1 reply; 26+ messages in thread
From: Eli Zaretskii @ 2012-07-28 6:53 UTC (permalink / raw)
To: Khoo Yit Phang; +Cc: tromey, pmuldoon, gdb-patches
> From: Khoo Yit Phang <khooyp@cs.umd.edu>
> Date: Thu, 26 Jul 2012 22:32:15 -0400
> Cc: Eli Zaretskii <eliz@gnu.org>, tromey@redhat.com, pmuldoon@redhat.com, gdb-patches@sourceware.org
>
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -17,6 +17,14 @@
> maint info bfds
> List the BFDs known to GDB.
>
> +python-interactive [command]
> +pi [command]
> + Start a Python interactive prompt, or evaluate the optional command
> + and print the result of expressions.
> +
> +py [command]
> + "py" is a new alias for "python".
> +
> *** Changes in GDB 7.5
This part is OK.
> +@item python-interactive @r{[}@var{code}@r{]}
> +@itemx pi @r{[}@var{code}@r{]}
Why "code"? Elsewhere you used "command", which is better, IMO.
> +The @code{python-interactive} command can be used to start an
> +interactive Python prompt.
"Without an argument, the @code{python-interactive} command can be
used ..."
> +Alternatively, a single-line Python command can be given as an
> +argument, and if the command is an expression, the result will be
> +printed.
And if the command is not an expression, what happens then?
> @item python @r{[}@var{code}@r{]}
> +@itemx py @r{[}@var{code}@r{]}
Again, I think "code" is not a good term here.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-28 6:53 ` Eli Zaretskii
@ 2012-07-28 14:35 ` Khoo Yit Phang
2012-07-28 15:05 ` Eli Zaretskii
0 siblings, 1 reply; 26+ messages in thread
From: Khoo Yit Phang @ 2012-07-28 14:35 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: Khoo Yit Phang, tromey, pmuldoon, gdb-patches
Hi,
On Jul 28, 2012, at 2:53 AM, Eli Zaretskii wrote:
>> From: Khoo Yit Phang <khooyp@cs.umd.edu>
>> Date: Thu, 26 Jul 2012 22:32:15 -0400
>> Cc: Eli Zaretskii <eliz@gnu.org>, tromey@redhat.com, pmuldoon@redhat.com, gdb-patches@sourceware.org
>>
>> +@item python-interactive @r{[}@var{code}@r{]}
>> +@itemx pi @r{[}@var{code}@r{]}
>
> Why "code"? Elsewhere you used "command", which is better, IMO.
I had just copied the doc for the old "python" command, which used "code". Shall I change it there too?
>> +The @code{python-interactive} command can be used to start an
>> +interactive Python prompt.
>
> "Without an argument, the @code{python-interactive} command can be
> used ..."
>
>> +Alternatively, a single-line Python command can be given as an
>> +argument, and if the command is an expression, the result will be
>> +printed.
>
> And if the command is not an expression, what happens then?
+Alternatively, a single-line Python command can be given as an
+argument and evaluated. If the command is an expression, the result
+ will be printed; otherwise, nothing will be printed.
>> @item python @r{[}@var{code}@r{]}
>> +@itemx py @r{[}@var{code}@r{]}
>
> Again, I think "code" is not a good term here.
Yit
July 28, 2012
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-28 14:35 ` Khoo Yit Phang
@ 2012-07-28 15:05 ` Eli Zaretskii
2012-07-28 15:14 ` Khoo Yit Phang
0 siblings, 1 reply; 26+ messages in thread
From: Eli Zaretskii @ 2012-07-28 15:05 UTC (permalink / raw)
To: Khoo Yit Phang; +Cc: khooyp, tromey, pmuldoon, gdb-patches
> From: Khoo Yit Phang <khooyp@cs.umd.edu>
> Date: Sat, 28 Jul 2012 10:34:52 -0400
> Cc: Khoo Yit Phang <khooyp@cs.umd.edu>,
> tromey@redhat.com,
> pmuldoon@redhat.com,
> gdb-patches@sourceware.org
>
> >> +@item python-interactive @r{[}@var{code}@r{]}
> >> +@itemx pi @r{[}@var{code}@r{]}
> >
> > Why "code"? Elsewhere you used "command", which is better, IMO.
>
> I had just copied the doc for the old "python" command, which used "code". Shall I change it there too?
Yes, please.
> +Alternatively, a single-line Python command can be given as an
> +argument and evaluated. If the command is an expression, the result
> + will be printed; otherwise, nothing will be printed.
This is good, but please leave two spaces after the period that ends
the first of these two sentences.
Thanks.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-28 15:05 ` Eli Zaretskii
@ 2012-07-28 15:14 ` Khoo Yit Phang
2012-07-31 22:04 ` Khoo Yit Phang
0 siblings, 1 reply; 26+ messages in thread
From: Khoo Yit Phang @ 2012-07-28 15:14 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: Khoo Yit Phang, tromey, pmuldoon, gdb-patches
[-- Attachment #1: Type: text/plain, Size: 61 bytes --]
Hi,
Here's the patch with updated docs.
Yit
July 28, 2012
[-- Attachment #2: python-interactive --]
[-- Type: application/octet-stream, Size: 5863 bytes --]
# HG changeset patch
# Parent bfcc38136f5f6f7257ce116b6df56b6b0589ae4d
Add a new "python-interactive" command that starts a standard Python prompt.
- Use Py_single_input mode to interpret "python-interactive" with arguments, so that results of expressions are printed.
- Use Python's built-in interactive loop for "python-interactive" without arguments.
gdb/ChangeLog:
2012-07-16 Khoo Yit Phang <khooyp@cs.umd.edu>
Add a new "python-interactive" command that starts a standard
Python interactive prompt with "pi" as alias, and add "py" as
an alias to "python".
* NEWS: Mention the new commands.
* doc/gdb.texinfo (Python Commands): Document the new
commands.
* python/python.c (eval_python_command): New function.
(python_interactive_command): For "python-interactive" with
arguments, call eval_python_command. For "python-interactive"
without arguments, call PyRun_InteractiveLoop.
(_initialize_python): Add "python-interactive" command with
"pi" as alias, and add "py" as an alias to "python".
diff --git a/gdb/NEWS b/gdb/NEWS
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -17,6 +17,14 @@
maint info bfds
List the BFDs known to GDB.
+python-interactive [command]
+pi [command]
+ Start a Python interactive prompt, or evaluate the optional command
+ and print the result of expressions.
+
+py [command]
+ "py" is a new alias for "python".
+
*** Changes in GDB 7.5
* GDB now supports x32 ABI. Visit <http://sites.google.com/site/x32abi/>
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -22539,12 +22539,30 @@
@cindex python commands
@cindex commands to access python
-@value{GDBN} provides one command for accessing the Python interpreter,
+@value{GDBN} provides two commands for accessing the Python interpreter,
and one related setting:
@table @code
+@kindex python-interactive
+@kindex pi
+@item python-interactive @r{[}@var{command}@r{]}
+@itemx pi @r{[}@var{command}@r{]}
+Without an argument, the @code{python-interactive} command can be used
+to start an interactive Python prompt.
+
+Alternatively, a single-line Python command can be given as an
+argument and evaluated. If the command is an expression, the result
+will be printed; otherwise, nothing will be printed. For example:
+
+@smallexample
+(@value{GDBP}) python-interactive 2 + 3
+5
+@end smallexample
+
@kindex python
-@item python @r{[}@var{code}@r{]}
+@kindex py
+@item python @r{[}@var{command}@r{]}
+@itemx py @r{[}@var{command}@r{]}
The @code{python} command can be used to evaluate Python code.
If given an argument, the @code{python} command will evaluate the
diff --git a/gdb/python/python.c b/gdb/python/python.c
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -151,6 +151,68 @@
return make_cleanup (restore_python_env, env);
}
+/* Evaluate a Python command like PyRun_SimpleString, but uses
+ Py_single_input which prints the result of expressions, and does
+ not automatically print the stack on errors. */
+
+static int
+eval_python_command (const char *command)
+{
+ PyObject *m, *d, *v;
+
+ m = PyImport_AddModule ("__main__");
+ if (m == NULL)
+ return -1;
+
+ d = PyModule_GetDict (m);
+ if (d == NULL)
+ return -1;
+ v = PyRun_StringFlags (command, Py_single_input, d, d, NULL);
+ if (v == NULL)
+ return -1;
+
+ Py_DECREF (v);
+ if (Py_FlushLine ())
+ PyErr_Clear ();
+
+ return 0;
+}
+
+/* Implementation of the gdb "python-interactive" command. */
+
+static void
+python_interactive_command (char *arg, int from_tty)
+{
+ struct cleanup *cleanup;
+ int err;
+
+ cleanup = make_cleanup_restore_integer (&interpreter_async);
+ interpreter_async = 0;
+
+ while (arg && *arg && isspace (*arg))
+ ++arg;
+
+ ensure_python_env (get_current_arch (), current_language);
+
+ if (arg && *arg)
+ {
+ err = eval_python_command (arg);
+ }
+ else
+ {
+ err = PyRun_InteractiveLoop (instream, "<stdin>");
+ dont_repeat ();
+ }
+
+ if (err)
+ {
+ gdbpy_print_stack ();
+ error (_("Error while executing Python code."));
+ }
+
+ do_cleanups (cleanup);
+}
+
/* A wrapper around PyRun_SimpleFile. FILE is the Python script to run
named FILENAME.
@@ -1087,10 +1149,11 @@
#else /* HAVE_PYTHON */
-/* Dummy implementation of the gdb "python" command. */
+/* Dummy implementation of the gdb "python-interactive" and "python"
+ command. */
static void
-python_command (char *arg, int from_tty)
+python_interactive_command (char *arg, int from_tty)
{
while (arg && *arg && isspace (*arg))
++arg;
@@ -1106,6 +1169,12 @@
}
}
+static void
+python_command (char *arg, int from_tty)
+{
+ python_interactive_command (arg, from_tty);
+}
+
void
eval_python_from_control_command (struct command_line *cmd)
{
@@ -1172,6 +1241,29 @@
char *cmd_name;
struct cmd_list_element *cmd;
+ add_com ("python-interactive", class_obscure,
+ python_interactive_command,
+#ifdef HAVE_PYTHON
+ _("\
+Start a Python interactive 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
_("\
@@ -1192,6 +1284,7 @@
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,
[-- Attachment #3: Type: text/plain, Size: 2 bytes --]
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-28 15:14 ` Khoo Yit Phang
@ 2012-07-31 22:04 ` Khoo Yit Phang
2012-08-01 15:46 ` Tom Tromey
2012-08-22 17:44 ` Khoo Yit Phang
0 siblings, 2 replies; 26+ messages in thread
From: Khoo Yit Phang @ 2012-07-31 22:04 UTC (permalink / raw)
To: Khoo Yit Phang; +Cc: Eli Zaretskii, tromey, pmuldoon, gdb-patches
[-- Attachment #1: Type: text/plain, Size: 257 bytes --]
Hi,
I found a bug in my patch in handling a single-line compound statement. Here's an updated patch that fixes the problem. The only change is in python_interactive_command to add a newline to the end of the argument.
Thanks,
Yit
July 28, 2012
[-- Attachment #2: python-interactive --]
[-- Type: application/octet-stream, Size: 6050 bytes --]
# HG changeset patch
# Parent 163c60206865503cb46b45d395c0f038e172a8dc
Add a new "python-interactive" command that starts a standard Python prompt.
- Use Py_single_input mode to interpret "python-interactive" with arguments, so that results of expressions are printed.
- Use Python's built-in interactive loop for "python-interactive" without arguments.
gdb/ChangeLog:
2012-07-16 Khoo Yit Phang <khooyp@cs.umd.edu>
Add a new "python-interactive" command that starts a standard
Python interactive prompt with "pi" as alias, and add "py" as
an alias to "python".
* NEWS: Mention the new commands.
* doc/gdb.texinfo (Python Commands): Document the new
commands.
* python/python.c (eval_python_command): New function.
(python_interactive_command): For "python-interactive" with
arguments, call eval_python_command. For "python-interactive"
without arguments, call PyRun_InteractiveLoop.
(_initialize_python): Add "python-interactive" command with
"pi" as alias, and add "py" as an alias to "python".
diff --git a/gdb/NEWS b/gdb/NEWS
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -17,6 +17,14 @@
maint info bfds
List the BFDs known to GDB.
+python-interactive [command]
+pi [command]
+ Start a Python interactive prompt, or evaluate the optional command
+ and print the result of expressions.
+
+py [command]
+ "py" is a new alias for "python".
+
*** Changes in GDB 7.5
* GDB now supports x32 ABI. Visit <http://sites.google.com/site/x32abi/>
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -22539,12 +22539,30 @@
@cindex python commands
@cindex commands to access python
-@value{GDBN} provides one command for accessing the Python interpreter,
+@value{GDBN} provides two commands for accessing the Python interpreter,
and one related setting:
@table @code
+@kindex python-interactive
+@kindex pi
+@item python-interactive @r{[}@var{command}@r{]}
+@itemx pi @r{[}@var{command}@r{]}
+Without an argument, the @code{python-interactive} command can be used
+to start an interactive Python prompt.
+
+Alternatively, a single-line Python command can be given as an
+argument and evaluated. If the command is an expression, the result
+will be printed; otherwise, nothing will be printed. For example:
+
+@smallexample
+(@value{GDBP}) python-interactive 2 + 3
+5
+@end smallexample
+
@kindex python
-@item python @r{[}@var{code}@r{]}
+@kindex py
+@item python @r{[}@var{command}@r{]}
+@itemx py @r{[}@var{command}@r{]}
The @code{python} command can be used to evaluate Python code.
If given an argument, the @code{python} command will evaluate the
diff --git a/gdb/python/python.c b/gdb/python/python.c
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -151,6 +151,75 @@
return make_cleanup (restore_python_env, env);
}
+/* Evaluate a Python command like PyRun_SimpleString, but uses
+ Py_single_input which prints the result of expressions, and does
+ not automatically print the stack on errors. */
+
+static int
+eval_python_command (const char *command)
+{
+ PyObject *m, *d, *v;
+
+ m = PyImport_AddModule ("__main__");
+ if (m == NULL)
+ return -1;
+
+ d = PyModule_GetDict (m);
+ if (d == NULL)
+ return -1;
+ v = PyRun_StringFlags (command, Py_single_input, d, d, NULL);
+ if (v == NULL)
+ return -1;
+
+ Py_DECREF (v);
+ if (Py_FlushLine ())
+ PyErr_Clear ();
+
+ return 0;
+}
+
+/* Implementation of the gdb "python-interactive" command. */
+
+static void
+python_interactive_command (char *arg, int from_tty)
+{
+ struct cleanup *cleanup;
+ int err;
+
+ cleanup = make_cleanup_restore_integer (&interpreter_async);
+ interpreter_async = 0;
+
+ while (arg && *arg && isspace (*arg))
+ ++arg;
+
+ ensure_python_env (get_current_arch (), current_language);
+
+ if (arg && *arg)
+ {
+ int len = strlen (arg);
+ char *script = xmalloc (len + 2);
+
+ strcpy (script, arg);
+ script[len] = '\n';
+ script[len + 1] = '\0';
+ err = eval_python_command (script);
+ xfree (script);
+ }
+ else
+ {
+ err = PyRun_InteractiveLoop (instream, "<stdin>");
+ dont_repeat ();
+ }
+
+ if (err)
+ {
+ gdbpy_print_stack ();
+ error (_("Error while executing Python code."));
+ }
+
+ do_cleanups (cleanup);
+}
+
/* A wrapper around PyRun_SimpleFile. FILE is the Python script to run
named FILENAME.
@@ -1087,10 +1156,11 @@
#else /* HAVE_PYTHON */
-/* Dummy implementation of the gdb "python" command. */
+/* Dummy implementation of the gdb "python-interactive" and "python"
+ command. */
static void
-python_command (char *arg, int from_tty)
+python_interactive_command (char *arg, int from_tty)
{
while (arg && *arg && isspace (*arg))
++arg;
@@ -1106,6 +1176,12 @@
}
}
+static void
+python_command (char *arg, int from_tty)
+{
+ python_interactive_command (arg, from_tty);
+}
+
void
eval_python_from_control_command (struct command_line *cmd)
{
@@ -1172,6 +1248,29 @@
char *cmd_name;
struct cmd_list_element *cmd;
+ add_com ("python-interactive", class_obscure,
+ python_interactive_command,
+#ifdef HAVE_PYTHON
+ _("\
+Start a Python interactive 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
_("\
@@ -1192,6 +1291,7 @@
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,
[-- Attachment #3: Type: text/plain, Size: 157 bytes --]
On Jul 28, 2012, at 11:14 AM, Khoo Yit Phang wrote:
> Hi,
>
> Here's the patch with updated docs.
>
> Yit
> July 28, 2012
>
> <python-interactive>
>
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-31 22:04 ` Khoo Yit Phang
@ 2012-08-01 15:46 ` Tom Tromey
2012-08-10 17:28 ` Khoo Yit Phang
2012-08-22 17:44 ` Khoo Yit Phang
1 sibling, 1 reply; 26+ messages in thread
From: Tom Tromey @ 2012-08-01 15:46 UTC (permalink / raw)
To: Khoo Yit Phang; +Cc: Eli Zaretskii, pmuldoon, gdb-patches
>>>>> "Yit" == Khoo Yit Phang <khooyp@cs.umd.edu> writes:
Yit> I found a bug in my patch in handling a single-line compound
Yit> statement. Here's an updated patch that fixes the problem. The only
Yit> change is in python_interactive_command to add a newline to the end of
Yit> the argument.
The patch is ok.
Thanks.
Tom
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-08-01 15:46 ` Tom Tromey
@ 2012-08-10 17:28 ` Khoo Yit Phang
0 siblings, 0 replies; 26+ messages in thread
From: Khoo Yit Phang @ 2012-08-10 17:28 UTC (permalink / raw)
To: Tom Tromey; +Cc: Khoo Yit Phang, Eli Zaretskii, Phil Muldoon, gdb-patches
Hi,
Just to be sure about the process, after the code and doc reviews, a maintainer will check in the code, not me (I don't believe I have access anyway), is that right?
Yit
August 10, 2012
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-07-31 22:04 ` Khoo Yit Phang
2012-08-01 15:46 ` Tom Tromey
@ 2012-08-22 17:44 ` Khoo Yit Phang
2012-08-22 17:59 ` Eli Zaretskii
1 sibling, 1 reply; 26+ messages in thread
From: Khoo Yit Phang @ 2012-08-22 17:44 UTC (permalink / raw)
To: Khoo Yit Phang; +Cc: gdb-patches
Hi,
Ping for a final doc review, in case there's anything I missed? If it looks good, I'll check this patch in.
Thanks!
Yit
August 22, 2012
On Jul 31, 2012, at 6:03 PM, Khoo Yit Phang wrote:
> Hi,
>
> I found a bug in my patch in handling a single-line compound statement. Here's an updated patch that fixes the problem. The only change is in python_interactive_command to add a newline to the end of the argument.
>
> Thanks,
>
> Yit
> July 28, 2012
>
> <python-interactive>
>
> On Jul 28, 2012, at 11:14 AM, Khoo Yit Phang wrote:
>
>> Hi,
>>
>> Here's the patch with updated docs.
>>
>> Yit
>> July 28, 2012
>>
>> <python-interactive>
>>
>
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [PATCH 1/4]: Make "python" start a standard Python prompt
2012-08-22 17:44 ` Khoo Yit Phang
@ 2012-08-22 17:59 ` Eli Zaretskii
0 siblings, 0 replies; 26+ messages in thread
From: Eli Zaretskii @ 2012-08-22 17:59 UTC (permalink / raw)
To: Khoo Yit Phang; +Cc: gdb-patches
> From: Khoo Yit Phang <khooyp@cs.umd.edu>
> Date: Wed, 22 Aug 2012 13:43:47 -0400
> Cc: gdb-patches@sourceware.org
>
> Ping for a final doc review, in case there's anything I missed? If it looks good, I'll check this patch in.
OK for the docs.
Thanks.
^ permalink raw reply [flat|nested] 26+ messages in thread
end of thread, other threads:[~2012-08-22 17:59 UTC | newest]
Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-16 20:47 [PATCH 1/4]: Make "python" start a standard Python prompt Khoo Yit Phang
2012-07-24 21:19 ` Khoo Yit Phang
2012-07-25 6:45 ` Phil Muldoon
2012-07-25 16:18 ` Tom Tromey
2012-07-25 16:58 ` Khoo Yit Phang
2012-07-25 17:20 ` Matt Rice
2012-07-25 17:29 ` Khoo Yit Phang
2012-07-25 17:43 ` Tom Tromey
2012-07-25 17:55 ` Khoo Yit Phang
2012-07-25 17:59 ` Tom Tromey
2012-07-25 21:07 ` Khoo Yit Phang
2012-07-26 17:38 ` Tom Tromey
2012-07-26 17:52 ` Khoo Yit Phang
2012-07-26 18:06 ` Eli Zaretskii
2012-07-26 18:10 ` Khoo Yit Phang
2012-07-27 2:32 ` Khoo Yit Phang
2012-07-27 19:23 ` Tom Tromey
2012-07-28 6:53 ` Eli Zaretskii
2012-07-28 14:35 ` Khoo Yit Phang
2012-07-28 15:05 ` Eli Zaretskii
2012-07-28 15:14 ` Khoo Yit Phang
2012-07-31 22:04 ` Khoo Yit Phang
2012-08-01 15:46 ` Tom Tromey
2012-08-10 17:28 ` Khoo Yit Phang
2012-08-22 17:44 ` Khoo Yit Phang
2012-08-22 17:59 ` Eli Zaretskii
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox