From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14331 invoked by alias); 16 Jul 2012 20:47:35 -0000 Received: (qmail 14321 invoked by uid 22791); 16 Jul 2012 20:47:35 -0000 X-SWARE-Spam-Status: No, hits=-0.3 required=5.0 tests=AWL,BAYES_20,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; Mon, 16 Jul 2012 20:47:22 +0000 Received: from wireless-206-196-164-152.umd.edu (wireless-206-196-164-152.umd.edu [206.196.164.152]) (Authenticated sender: khooyp) by bacon.cs.umd.edu (Postfix) with ESMTPSA id EBF31B403D7; Mon, 16 Jul 2012 16:47:19 -0400 (EDT) From: Khoo Yit Phang Content-Type: multipart/mixed; boundary=Apple-Mail-15--907782986 Subject: [PATCH 1/4]: Make "python" start a standard Python prompt Date: Mon, 16 Jul 2012 20:47:00 -0000 Message-Id: <770F6E2B-70F5-4DF9-8E01-BD4F5FDC7AB1@cs.umd.edu> Cc: Khoo Yit Phang To: gdb-patches@sourceware.org Mime-Version: 1.0 (Apple Message framework v1084) X-CSD-MailScanner-ID: EBF31B403D7.AAFE1 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: 1343076440.53089@jxcz0xSb0J1JGcqciyekmQ 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/msg00222.txt.bz2 --Apple-Mail-15--907782986 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Content-length: 573 Hi, I'd like to resubmit my patches to make the "python" command start a standa= rd Python prompt (I've completed the copyright assignment). As discussed ba= ck in January-February (http://permalink.gmane.org/gmane.comp.gdb.patches/7= 1971), 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) upd= ate the testsuite to use "python-block"; 2) make Ctrl-C work under Python b= y raising KeyboardInterrupt; and 3) enable readline under the Python prompt. Thank you, Yit July 16, 2012 --Apple-Mail-15--907782986 Content-Disposition: attachment; filename=python-interactive Content-Type: application/octet-stream; name="python-interactive" Content-Transfer-Encoding: 7bit Content-length: 6927 # 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 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, ""); + 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\ --Apple-Mail-15--907782986--