This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [patch][python] Fix Python 3 long/int logic error when converting a value in GDB
- From: Phil Muldoon <pmuldoon at redhat dot com>
- To: Tom Tromey <tromey at redhat dot com>
- Cc: gdb-patches at sourceware dot org
- Date: Wed, 02 Oct 2013 10:18:43 +0100
- Subject: Re: [patch][python] Fix Python 3 long/int logic error when converting a value in GDB
- Authentication-results: sourceware.org; auth=none
- References: <52396967 dot 50309 at redhat dot com> <87k3iducf8 dot fsf at fleche dot redhat dot com>
On 18/09/13 21:01, Tom Tromey wrote:
>>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:
>
> Phil> I audited the code for other checks like this and found these files
> Phil> use PyInt_Check.
>
> Thank you.
>
> Phil> Of those, the only check that makes a differentiation between a long
> Phil> and an int is py-arch.c. The order for that check is already correct,
> Phil> so no change is needed. The other files just do a simple number
> Phil> check, and those work as intended.
>
> Phil> OK?
>
> I think that since the code is sensitive to the ordering, and since
> we've already made an error here, it would be best to add an explanatory
> comment at the two sites where it matters.
How about these comments for the two places?
Cheers,
Phil
2013-10-02 Phil Muldoon <pmuldoon@redhat.com>
* python/py-value.c (convert_value_from_python): Move PyInt_Check
conversion logic to occur after PyLong_Check. Comment on order
change significance.
* python/py-arch.c (archpy_disassemble): Comment on order of
conversion for integers and longs.
---
gdb/python/py-arch.c | 7 +++++++
gdb/python/py-value.c | 21 ++++++++++++++-------
2 files changed, 21 insertions(+), 7 deletions(-)
diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c
index a31ffdd..a351c12 100644
--- a/gdb/python/py-arch.c
+++ b/gdb/python/py-arch.c
@@ -132,6 +132,13 @@ archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
start = start_temp;
if (end_obj)
{
+ /* Make a long logic check first. In Python 3.x, internally,
+ all integers are represented as longs. In Python 2.x, there
+ is still a differentiation internally between a PyInt and a
+ PyLong. Explicitly do this long check conversion first. In
+ GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
+ to be done first to ensure we do not lose information in the
+ conversion process. */
if (PyLong_Check (end_obj))
end = PyLong_AsUnsignedLongLong (end_obj);
else if (PyInt_Check (end_obj))
diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 0d87219..07feaf8 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -1265,13 +1265,13 @@ convert_value_from_python (PyObject *obj)
if (cmp >= 0)
value = value_from_longest (builtin_type_pybool, cmp);
}
- else if (PyInt_Check (obj))
- {
- long l = PyInt_AsLong (obj);
-
- if (! PyErr_Occurred ())
- value = value_from_longest (builtin_type_pyint, l);
- }
+ /* Make a long logic check first. In Python 3.x, internally,
+ all integers are represented as longs. In Python 2.x, there
+ is still a differentiation internally between a PyInt and a
+ PyLong. Explicitly do this long check conversion first. In
+ GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
+ to be done first to ensure we do not lose information in the
+ conversion process. */
else if (PyLong_Check (obj))
{
LONGEST l = PyLong_AsLongLong (obj);
@@ -1306,6 +1306,13 @@ convert_value_from_python (PyObject *obj)
else
value = value_from_longest (builtin_type_pylong, l);
}
+ else if (PyInt_Check (obj))
+ {
+ long l = PyInt_AsLong (obj);
+
+ if (! PyErr_Occurred ())
+ value = value_from_longest (builtin_type_pyint, l);
+ }
else if (PyFloat_Check (obj))
{
double d = PyFloat_AsDouble (obj);
--
1.8.1.4