From: Khoo Yit Phang <khooyp@cs.umd.edu>
To: Eli Zaretskii <eliz@gnu.org>
Cc: Khoo Yit Phang <khooyp@cs.umd.edu>,
tromey@redhat.com, pmuldoon@redhat.com,
gdb-patches@sourceware.org
Subject: Re: [PATCH 1/4]: Make "python" start a standard Python prompt
Date: Thu, 26 Jul 2012 18:10:00 -0000 [thread overview]
Message-ID: <6943FA37-9279-4213-95A6-7C6546A35BE3@cs.umd.edu> (raw)
In-Reply-To: <83mx2mjt2j.fsf@gnu.org>
[-- 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,
next prev parent reply other threads:[~2012-07-26 18:10 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-07-16 20:47 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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6943FA37-9279-4213-95A6-7C6546A35BE3@cs.umd.edu \
--to=khooyp@cs.umd.edu \
--cc=eliz@gnu.org \
--cc=gdb-patches@sourceware.org \
--cc=pmuldoon@redhat.com \
--cc=tromey@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox