From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26627 invoked by alias); 27 Jul 2012 02:32:33 -0000 Received: (qmail 26619 invoked by uid 22791); 27 Jul 2012 02:32:32 -0000 X-SWARE-Spam-Status: No, hits=-1.7 required=5.0 tests=AWL,BAYES_00,KHOP_THREADED,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from server-nat-6.cs.umd.edu (HELO bacon.cs.umd.edu) (128.8.127.149) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 27 Jul 2012 02:32:19 +0000 Received: from [192.168.32.3] (pool-71-163-240-242.washdc.fios.verizon.net [71.163.240.242]) (Authenticated sender: khooyp) by bacon.cs.umd.edu (Postfix) with ESMTPSA id 4251BB4015E; Thu, 26 Jul 2012 22:32:16 -0400 (EDT) Subject: Re: [PATCH 1/4]: Make "python" start a standard Python prompt Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: multipart/mixed; boundary=Apple-Mail-11--23084069 From: Khoo Yit Phang In-Reply-To: <6943FA37-9279-4213-95A6-7C6546A35BE3@cs.umd.edu> Date: Fri, 27 Jul 2012 02:32:00 -0000 Cc: Eli Zaretskii , tromey@redhat.com, pmuldoon@redhat.com, gdb-patches@sourceware.org Message-Id: <17C09F4A-E788-4ABE-926E-7113ACB97651@cs.umd.edu> References: <770F6E2B-70F5-4DF9-8E01-BD4F5FDC7AB1@cs.umd.edu> <500F95FB.5060904@redhat.com> <87zk6nizl6.fsf@fleche.redhat.com> <1C2FC4AD-E6FE-4857-9DE5-FDE5BE006E95@cs.umd.edu> <874novivob.fsf@fleche.redhat.com> <9DB9F174-129C-4C4A-9708-53A1D521251A@cs.umd.edu> <87mx2nhgch.fsf@fleche.redhat.com> <61B8FB86-D21F-4BA5-B86D-DF0ECF04C29D@cs.umd.edu> <87vchae842.fsf@fleche.redhat.com> <83mx2mjt2j.fsf@gnu.org> <6943FA37-9279-4213-95A6-7C6546A35BE3@cs.umd.edu> To: Khoo Yit Phang X-CSD-MailScanner-ID: 4251BB4015E.AEFBC X-CSD-MailScanner: Found to be clean X-CSD-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-50, required 5, autolearn=not spam, ALL_TRUSTED -50.00) X-CSD-MailScanner-From: khooyp@cs.umd.edu X-CSD-MailScanner-Watermark: 1343961137.9261@Awc/efPrCGv0aBC6jxsJDA Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2012-07/txt/msg00656.txt.bz2 --Apple-Mail-11--23084069 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Content-length: 137 Hi, My working copy was a little old when I made the previous patch and has con= flicts for NEWS. Here's a new one. Yit July 26, 2012 --Apple-Mail-11--23084069 Content-Disposition: attachment; filename=python-interactive Content-Type: application/octet-stream; name="python-interactive" Content-Transfer-Encoding: 7bit Content-length: 5748 # 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 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 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, ""); + 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, --Apple-Mail-11--23084069 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii Content-length: 158 On Jul 26, 2012, at 2:10 PM, Khoo Yit Phang wrote: > Hi, > > Here's the updated patch. > > Thanks! > > Yit > July 26, 2012 > > --Apple-Mail-11--23084069--