This is the mail archive of the
archer@sourceware.org
mailing list for the Archer project.
[python] rfc: PyObject_IsTrue housekeeping patch
- From: Phil Muldoon <pmuldoon at redhat dot com>
- To: Project Archer <archer at sourceware dot org>
- Date: Mon, 26 Jan 2009 12:24:27 +0000
- Subject: [python] rfc: PyObject_IsTrue housekeeping patch
This patch changes the Python C based code to use PyObject_IsTrue() over
direct comparisons with Py_True.
PyObject_IsTrue() can return three states: 0 (False), 1 (True) and -1
(Error). In the case of this patch, anything < 0 is treated as an error
and in most cases the function immediately returns -1 or NULL wherever
appropriate.
In case of an error condition in python-value.c, no immediate return is
called; later in the function, after the if else conditions are unwound,
an existing check for PyErr_Occurred occurs and the exception is dealt
with there.
In the case of python-param.c there is a comparison where the value can
be Py_None, or boolean. In that case I rewrote the logic to test for
Py_None first; if that fails we then check the boolean comparison.
There are no regressions in the Python tests.
2009-01-26 Phil Muldoon <pmuldoon@redhat.com>
* python/python-breakpoint.c (bppy_set_enabled): Use
PyObject_IsTrue and account for comparison failure.
(bppy_set_silent): Likewise.
* python/python-cmd.c (cmdpy_init): Likewise.
* python/python-param.c (set_parameter_value): Likewise.
* python/python-value.c (convert_value_from_python): Likewise.
* python/python.c (execute_gdb_command): Likewise.
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index a2e5838..0742753 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,13 @@
+2009-01-26 Phil Muldoon <pmuldoon@redhat.com>
+
+ * python/python-breakpoint.c (bppy_set_enabled): Use
+ PyObject_IsTrue and account for comparison failure.
+ (bppy_set_silent): Likewise.
+ * python/python-cmd.c (cmdpy_init): Likewise.
+ * python/python-param.c (set_parameter_value): Likewise.
+ * python/python-value.c (convert_value_from_python): Likewise.
+ * python/python.c (execute_gdb_command): Likewise.
+
2009-01-14 Tom Tromey <tromey@redhat.com>
* darwin-nat-info.c (info_mach_region_command): Use
diff --git a/gdb/python/python-breakpoint.c b/gdb/python/python-breakpoint.c
index 4c79093..5e87af6 100644
--- a/gdb/python/python-breakpoint.c
+++ b/gdb/python/python-breakpoint.c
@@ -1,6 +1,6 @@
/* Python interface to breakpoints
- Copyright (C) 2008 Free Software Foundation, Inc.
+ Copyright (C) 2008, 2009 Free Software Foundation, Inc.
This file is part of GDB.
@@ -129,6 +129,7 @@ static int
bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
{
breakpoint_object *self_bp = (breakpoint_object *) self;
+ int cmp;
BPPY_SET_REQUIRE_VALID (self_bp);
@@ -144,11 +145,13 @@ bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
return -1;
}
- if (newvalue == Py_True)
+ cmp = PyObject_IsTrue (newvalue);
+ if (cmp < 0)
+ return -1;
+ else if (cmp == 1)
enable_breakpoint (self_bp->bp);
- else
+ else
disable_breakpoint (self_bp->bp);
-
return 0;
}
@@ -157,6 +160,7 @@ static int
bppy_set_silent (PyObject *self, PyObject *newvalue, void *closure)
{
breakpoint_object *self_bp = (breakpoint_object *) self;
+ int cmp;
BPPY_SET_REQUIRE_VALID (self_bp);
@@ -172,7 +176,11 @@ bppy_set_silent (PyObject *self, PyObject *newvalue, void *closure)
return -1;
}
- self_bp->bp->silent = (newvalue == Py_True);
+ cmp = PyObject_IsTrue (newvalue);
+ if (cmp < 0)
+ return -1;
+ else
+ self_bp->bp->silent = cmp;
return 0;
}
diff --git a/gdb/python/python-cmd.c b/gdb/python/python-cmd.c
index 014274b..61d5e5d 100644
--- a/gdb/python/python-cmd.c
+++ b/gdb/python/python-cmd.c
@@ -1,6 +1,6 @@
/* gdb commands implemented in Python
- Copyright (C) 2008 Free Software Foundation, Inc.
+ Copyright (C) 2008, 2009 Free Software Foundation, Inc.
This file is part of GDB.
@@ -370,6 +370,7 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject *kwds)
struct cmd_list_element **cmd_list;
char *cmd_name, *pfx_name;
PyObject *is_prefix = NULL;
+ int cmp;
if (obj->command)
{
@@ -406,30 +407,35 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject *kwds)
return -1;
pfx_name = NULL;
- if (is_prefix == Py_True)
+ if (is_prefix != NULL)
{
- int i, out;
-
- /* Make a normalized form of the command name. */
- pfx_name = xmalloc (strlen (name) + 2);
-
- i = 0;
- out = 0;
- while (name[i])
+ cmp = PyObject_IsTrue (is_prefix);
+ if (cmp == 1)
{
- /* Skip whitespace. */
- while (name[i] == ' ' || name[i] == '\t')
- ++i;
- /* Copy non-whitespace characters. */
- while (name[i] && name[i] != ' ' && name[i] != '\t')
- pfx_name[out++] = name[i++];
- /* Add a single space after each word -- including the final
- word. */
- pfx_name[out++] = ' ';
+ int i, out;
+
+ /* Make a normalized form of the command name. */
+ pfx_name = xmalloc (strlen (name) + 2);
+
+ i = 0;
+ out = 0;
+ while (name[i])
+ {
+ /* Skip whitespace. */
+ while (name[i] == ' ' || name[i] == '\t')
+ ++i;
+ /* Copy non-whitespace characters. */
+ while (name[i] && name[i] != ' ' && name[i] != '\t')
+ pfx_name[out++] = name[i++];
+ /* Add a single space after each word -- including the final
+ word. */
+ pfx_name[out++] = ' ';
+ }
+ pfx_name[out] = '\0';
}
- pfx_name[out] = '\0';
+ else if (cmp < 0)
+ return -1;
}
-
if (PyObject_HasAttr (self, gdbpy_doc_cst))
{
PyObject *ds_obj = PyObject_GetAttr (self, gdbpy_doc_cst);
diff --git a/gdb/python/python-param.c b/gdb/python/python-param.c
index dc1aec3..1f591a8 100644
--- a/gdb/python/python-param.c
+++ b/gdb/python/python-param.c
@@ -1,6 +1,6 @@
/* gdb parameters implemented in Python
- Copyright (C) 2008 Free Software Foundation, Inc.
+ Copyright (C) 2008, 2009 Free Software Foundation, Inc.
This file is part of GDB.
@@ -114,6 +114,8 @@ get_attr (PyObject *obj, PyObject *attr_name)
static int
set_parameter_value (parmpy_object *self, PyObject *value)
{
+ int cmp;
+
switch (self->type)
{
case var_string:
@@ -172,7 +174,10 @@ set_parameter_value (parmpy_object *self, PyObject *value)
PyErr_SetString (PyExc_RuntimeError, "boolean required");
return -1;
}
- self->value.intval = value == Py_True;
+ cmp = PyObject_IsTrue (value);
+ if (cmp < 0)
+ return -1;
+ self->value.intval = cmp;
break;
case var_auto_boolean:
@@ -183,13 +188,20 @@ set_parameter_value (parmpy_object *self, PyObject *value)
return -1;
}
- if (value == Py_True)
- self->value.autoboolval = AUTO_BOOLEAN_TRUE;
- else if (value == Py_False)
- self->value.autoboolval = AUTO_BOOLEAN_FALSE;
- else
+ if (value == Py_None)
self->value.autoboolval = AUTO_BOOLEAN_AUTO;
- break;
+ else
+ {
+ cmp = PyObject_IsTrue (value);
+ if (cmp < 0 )
+ return -1;
+ if (cmp == 1)
+ self->value.autoboolval = AUTO_BOOLEAN_TRUE;
+ else
+ self->value.autoboolval = AUTO_BOOLEAN_FALSE;
+
+ break;
+ }
case var_integer:
case var_zinteger:
diff --git a/gdb/python/python-value.c b/gdb/python/python-value.c
index c329604..53dcb74 100644
--- a/gdb/python/python-value.c
+++ b/gdb/python/python-value.c
@@ -779,12 +779,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 bdb5319..8fdf1fb 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -260,6 +260,7 @@ execute_gdb_command (PyObject *self, PyObject *args)
char *arg, *newarg;
PyObject *from_tty_obj = NULL;
int from_tty;
+ int cmp;
volatile struct gdb_exception except;
if (! PyArg_ParseTuple (args, "s|O!", &arg, &PyBool_Type, &from_tty_obj))
@@ -267,7 +268,12 @@ execute_gdb_command (PyObject *self, PyObject *args)
from_tty = 0;
if (from_tty_obj)
- from_tty = from_tty_obj == Py_True;
+ {
+ cmp = PyObject_IsTrue (from_tty_obj);
+ if (cmp < 0)
+ return NULL;
+ from_tty = cmp;
+ }
TRY_CATCH (except, RETURN_MASK_ALL)
{