From 74aedc4602913e330a1ec0cf5e9b8c8fe2b19a48 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Wed, 26 Jan 2011 20:53:45 +0000 Subject: * python/python.c (gdbpy_solib_name): Use gdb_py_longest and GDB_PY_LL_ARG. * python/python-internal.h (GDB_PY_LL_ARG, GDB_PY_LLU_ARG): New macros. (gdb_py_longest, gdb_py_ulongest): New typedefs. (gdb_py_long_from_longest, gdb_py_long_from_ulongest) (gdb_py_long_as_ulongest): New defines. (gdb_py_object_from_longest, gdb_py_object_from_ulongest) (gdb_py_int_as_long): Declare. * python/py-value.c (valpy_lazy_string): Use gdb_py_longest, GDB_PY_LL_ARG, gdb_py_object_from_longest. (valpy_long): Add comment. * python/py-utils.c (get_addr_from_python): Use gdb_py_long_as_ulongest. Handle overflow properly. (gdb_py_object_from_longest): New function. (gdb_py_object_from_ulongest): Likewise. (gdb_py_int_as_long): Likewise. * python/py-type.c (typy_array): Use gdb_py_int_as_long. * python/py-symtab.c (salpy_get_pc): Use gdb_py_long_from_ulongest. (salpy_get_line): Use PyInt_FromLong. * python/py-param.c (set_parameter_value): Use gdb_py_int_as_long. * python/py-lazy-string.c (stpy_get_address): Use gdb_py_long_from_ulongest. * python/py-frame.c (frapy_pc): Use gdb_py_long_from_ulongest. * python/py-cmd.c (cmdpy_completer): Use gdb_py_int_as_long. * python/py-breakpoint.c (bppy_set_thread): Use gdb_py_int_as_long. (bppy_set_task): Likewise. (bppy_set_ignore_count): Likewise. (bppy_set_hit_count): Likewise. * python/py-block.c (blpy_get_start): Use gdb_py_object_from_ulongest. (blpy_get_end): Likewise. (gdbpy_block_for_pc): Use gdb_py_ulongest and GDB_PY_LLU_ARG. --- gdb/python/py-utils.c | 86 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 26 deletions(-) (limited to 'gdb/python/py-utils.c') diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c index 8a9176ac..79e17c4 100644 --- a/gdb/python/py-utils.c +++ b/gdb/python/py-utils.c @@ -304,39 +304,73 @@ get_addr_from_python (PyObject *obj, CORE_ADDR *addr) { if (gdbpy_is_value_object (obj)) *addr = value_as_address (value_object_to_value (obj)); - else if (PyLong_Check (obj)) + else { - /* Assume CORE_ADDR corresponds to unsigned long. */ - *addr = PyLong_AsUnsignedLong (obj); - if (PyErr_Occurred () != NULL) + PyObject *num = PyNumber_Long (obj); + gdb_py_ulongest val; + + if (num == NULL) return 0; - } - else if (PyInt_Check (obj)) - { - long val; - /* Assume CORE_ADDR corresponds to unsigned long. */ - val = PyInt_AsLong (obj); + val = gdb_py_long_as_ulongest (num); + Py_XDECREF (num); + if (PyErr_Occurred ()) + return 0; - if (val >= 0) - *addr = val; - else - { - /* If no error ocurred, VAL is indeed negative. */ - if (PyErr_Occurred () != NULL) + if (sizeof (val) > sizeof (CORE_ADDR) && ((CORE_ADDR) val) != val) + { + PyErr_SetString (PyExc_ValueError, + _("Overflow converting to address.")); return 0; + } - PyErr_SetString (PyExc_ValueError, - _("Supplied address is negative.")); - return 0; - } - } - else - { - PyErr_SetString (PyExc_TypeError, - _("Invalid type for address.")); - return 0; + *addr = val; } return 1; } + +/* Convert a LONGEST to the appropriate Python object -- either an + integer object or a long object, depending on its value. */ + +PyObject * +gdb_py_object_from_longest (LONGEST l) +{ +#ifdef HAVE_LONG_LONG /* Defined by Python. */ + /* If we have 'long long', and the value overflows a 'long', use a + Python Long; otherwise use a Python Int. */ + if (sizeof (l) > sizeof (long) + && (l > PyInt_GetMax () || l < (- (LONGEST) PyInt_GetMax ()) - 1)) + return PyLong_FromLongLong (l); +#endif + return PyInt_FromLong (l); +} + +/* Convert a ULONGEST to the appropriate Python object -- either an + integer object or a long object, depending on its value. */ + +PyObject * +gdb_py_object_from_ulongest (ULONGEST l) +{ +#ifdef HAVE_LONG_LONG /* Defined by Python. */ + /* If we have 'long long', and the value overflows a 'long', use a + Python Long; otherwise use a Python Int. */ + if (sizeof (l) > sizeof (unsigned long) && l > PyInt_GetMax ()) + return PyLong_FromUnsignedLongLong (l); +#endif + + if (l > PyInt_GetMax ()) + return PyLong_FromUnsignedLong (l); + + return PyInt_FromLong (l); +} + +/* Like PyInt_AsLong, but returns 0 on failure, 1 on success, and puts + the value into an out parameter. */ + +int +gdb_py_int_as_long (PyObject *obj, long *result) +{ + *result = PyInt_AsLong (obj); + return ! (*result == -1 && PyErr_Occurred ()); +} -- cgit v1.1