From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7295 invoked by alias); 2 Feb 2009 13:06:26 -0000 Received: (qmail 7287 invoked by uid 22791); 2 Feb 2009 13:06:25 -0000 X-SWARE-Spam-Status: No, hits=-2.3 required=5.0 tests=AWL,BAYES_00,J_CHICKENPOX_36,J_CHICKENPOX_37,SPF_PASS X-Spam-Check-By: sourceware.org Received: from e24smtp02.br.ibm.com (HELO e24smtp02.br.ibm.com) (32.104.18.86) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 02 Feb 2009 13:06:19 +0000 Received: from d24relay01.br.ibm.com (d24relay01.br.ibm.com [9.8.31.16]) by e24smtp02.br.ibm.com (8.13.1/8.13.1) with ESMTP id n12DD44V011899 for ; Mon, 2 Feb 2009 11:13:04 -0200 Received: from d24av02.br.ibm.com (d24av02.br.ibm.com [9.18.232.47]) by d24relay01.br.ibm.com (8.13.8/8.13.8/NCO v9.1) with ESMTP id n12E5h3n3465360 for ; Mon, 2 Feb 2009 11:05:43 -0300 Received: from d24av02.br.ibm.com (loopback [127.0.0.1]) by d24av02.br.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id n12D6FwL020223 for ; Mon, 2 Feb 2009 11:06:15 -0200 Received: from [9.8.5.242] ([9.8.5.242]) by d24av02.br.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id n12D6ECF020203 for ; Mon, 2 Feb 2009 11:06:14 -0200 Subject: [RFA][python] Fixes for existing Python code. From: Thiago Jung Bauermann To: gdb-patches ml Content-Type: text/plain Date: Mon, 02 Feb 2009 13:06:00 -0000 Message-Id: <1233579973.7000.8.camel@localhost.localdomain> Mime-Version: 1.0 Content-Transfer-Encoding: 7bit X-IsSubscribed: yes 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: 2009-02/txt/msg00024.txt.bz2 Hi, This patch has some fixes for the Python code which is currently in. They are the following: - add from_tty argument to execute_gdb_command; - fix error checking of function PyRun_SimpleString; - reorganize python.c to minimize forward declarations; - properly check Python booleans. I'd also like to remind that the following patches for Python support are still pending: http://sourceware.org/ml/gdb-patches/2009-01/msg00016.html http://sourceware.org/ml/gdb-patches/2009-01/msg00003.html http://sourceware.org/ml/gdb-patches/2009-01/msg00004.html This patch and the next two that I'm posting are on top of the three patches above. Ok to commit? -- []'s Thiago Jung Bauermann IBM Linux Technology Center gdb/ 2009-02-02 Tom Tromey Phil Muldoon Thiago Jung Bauermann * python/python-value.c (convert_value_from_python): Properly check Python booleans. * python/python.c (GdbMethods): Move to bottom of file. (get_parameter, execute_gdb_command, gdbpy_write, gdbpy_flush): Remove forward declarations. (eval_python_from_control_command): Fix error checking of function PyRun_SimpleString. Fix error string. (python_command): Likewise. (execute_gdb_command): Added from_tty argument. gdb/doc/ 2009-02-02 Tom Tromey * gdb.texinfo (Basic Python): Document execute's from_tty argument. diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 45cad64..2c6c2ea 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -18078,11 +18078,15 @@ methods and classes added by @value{GDBN} are placed in this module. use in all scripts evaluated by the @code{python} command. @findex gdb.execute -@defun execute command +@defun execute command [from_tty] Evaluate @var{command}, a string, as a @value{GDBN} CLI command. If a GDB exception happens while @var{command} runs, it is translated as described in @ref{Exception Handling,,Exception Handling}. If no exceptions occur, this function returns @code{None}. + +@var{from_tty} specifies whether @value{GDBN} ought to consider this +command as having originated from the user invoking it interactively. +It must be a boolean value. If omitted, it defaults to @code{False}. @end defun @findex gdb.get_parameter diff --git a/gdb/python/python-value.c b/gdb/python/python-value.c index c6775b2..d407d05 100644 --- a/gdb/python/python-value.c +++ b/gdb/python/python-value.c @@ -732,12 +732,17 @@ convert_value_from_python (PyObject *obj) struct value *value = NULL; /* -Wall */ PyObject *target_str, *unicode_str; struct cleanup *old; + int cmp; if (! obj) error (_("Internal error while converting Python value.")); - if (PyBool_Check (obj)) - value = value_from_longest (builtin_type_pybool, obj == Py_True); + if (PyBool_Check (obj)) + { + cmp = PyObject_IsTrue (obj); + if (cmp >= 0) + value = value_from_longest (builtin_type_pybool, cmp); + } else if (PyInt_Check (obj)) value = value_from_longest (builtin_type_pyint, PyInt_AsLong (obj)); else if (PyLong_Check (obj)) diff --git a/gdb/python/python.c b/gdb/python/python.c index 991321f..96bb5f5 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -42,31 +42,10 @@ static int gdbpy_should_print_stack = 1; #include "target.h" #include "gdbthread.h" +static PyMethodDef GdbMethods[]; PyObject *gdb_module; -static PyObject *get_parameter (PyObject *, PyObject *); -static PyObject *execute_gdb_command (PyObject *, PyObject *); -static PyObject *gdbpy_write (PyObject *, PyObject *); -static PyObject *gdbpy_flush (PyObject *, PyObject *); - -static PyMethodDef GdbMethods[] = -{ - { "history", gdbpy_history, METH_VARARGS, - "Get a value from history" }, - { "execute", execute_gdb_command, METH_VARARGS, - "Execute a gdb command" }, - { "get_parameter", get_parameter, METH_VARARGS, - "Return a gdb parameter's value" }, - - { "write", gdbpy_write, METH_VARARGS, - "Write a string using gdb's filtered stream." }, - { "flush", gdbpy_flush, METH_NOARGS, - "Flush gdb's filtered stdout stream." }, - - {NULL, NULL, 0, NULL} -}; - /* Given a command_line, return a command string suitable for passing to Python. Lines in the string are separated by newlines. The return value is allocated using xmalloc and the caller is @@ -102,6 +81,7 @@ compute_python_string (struct command_line *l) void eval_python_from_control_command (struct command_line *cmd) { + int ret; char *script; struct cleanup *cleanup; PyGILState_STATE state; @@ -113,12 +93,12 @@ eval_python_from_control_command (struct command_line *cmd) cleanup = make_cleanup_py_restore_gil (&state); script = compute_python_string (cmd->body_list[0]); - PyRun_SimpleString (script); + ret = PyRun_SimpleString (script); xfree (script); - if (PyErr_Occurred ()) + if (ret) { gdbpy_print_stack (); - error (_("error while executing Python code")); + error (_("Error while executing Python code.")); } do_cleanups (cleanup); @@ -139,11 +119,10 @@ python_command (char *arg, int from_tty) ++arg; if (arg && *arg) { - PyRun_SimpleString (arg); - if (PyErr_Occurred ()) + if (PyRun_SimpleString (arg)) { gdbpy_print_stack (); - error (_("error while executing Python code")); + error (_("Error while executing Python code.")); } } else @@ -256,14 +235,26 @@ execute_gdb_command (PyObject *self, PyObject *args) { struct cmd_list_element *alias, *prefix, *cmd; char *arg, *newarg; + PyObject *from_tty_obj = NULL; + int from_tty; + int cmp; volatile struct gdb_exception except; - if (! PyArg_ParseTuple (args, "s", &arg)) + if (! PyArg_ParseTuple (args, "s|O!", &arg, &PyBool_Type, &from_tty_obj)) return NULL; + from_tty = 0; + if (from_tty_obj) + { + cmp = PyObject_IsTrue (from_tty_obj); + if (cmp < 0) + return NULL; + from_tty = cmp; + } + TRY_CATCH (except, RETURN_MASK_ALL) { - execute_command (arg, 0); + execute_command (arg, from_tty); } GDB_PY_HANDLE_EXCEPTION (except); @@ -451,3 +442,26 @@ sys.stdout = GdbOutputFile()\n\ #endif /* HAVE_PYTHON */ } + + + +#if HAVE_PYTHON + +static PyMethodDef GdbMethods[] = +{ + { "history", gdbpy_history, METH_VARARGS, + "Get a value from history" }, + { "execute", execute_gdb_command, METH_VARARGS, + "Execute a gdb command" }, + { "get_parameter", get_parameter, METH_VARARGS, + "Return a gdb parameter's value" }, + + { "write", gdbpy_write, METH_VARARGS, + "Write a string using gdb's filtered stream." }, + { "flush", gdbpy_flush, METH_NOARGS, + "Flush gdb's filtered stdout stream." }, + + {NULL, NULL, 0, NULL} +}; + +#endif /* HAVE_PYTHON */