diff --git a/gdb/python/python-value.c b/gdb/python/python-value.c index 92e0431..420d26f 100644 --- a/gdb/python/python-value.c +++ b/gdb/python/python-value.c @@ -533,7 +688,7 @@ value_to_value_object (struct value *val) } /* Try to convert a Python value to a gdb value. If the value cannot - be converted, throw a gdb exception. */ + be converted, set a Python exception and return NULL. */ struct value * convert_value_from_python (PyObject *obj) @@ -541,53 +696,72 @@ convert_value_from_python (PyObject *obj) struct value *value = NULL; /* -Wall */ PyObject *target_str, *unicode_str; struct cleanup *old; + volatile struct gdb_exception except; + int cmp; - if (! obj) - error (_("Internal error while converting Python value.")); + gdb_assert (obj != NULL); + TRY_CATCH (except, RETURN_MASK_ALL) + { if (PyBool_Check (obj)) - value = value_from_longest (builtin_type_pybool, obj == Py_True); + { + 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)); + { + long l = PyInt_AsLong (obj); + + if (! PyErr_Occurred ()) + value = value_from_longest (builtin_type_pyint, l); + } else if (PyLong_Check (obj)) { LONGEST l = PyLong_AsLongLong (obj); + if (! PyErr_Occurred ()) value = value_from_longest (builtin_type_pylong, l); } else if (PyFloat_Check (obj)) { double d = PyFloat_AsDouble (obj); + if (! PyErr_Occurred ()) value = value_from_double (builtin_type_pyfloat, d); } - else if (PyString_Check (obj) || PyUnicode_Check (obj)) + else if (gdbpy_is_string (obj)) { char *s; s = python_string_to_target_string (obj); - if (s == NULL) - return NULL; - + if (s != NULL) + { old = make_cleanup (xfree, s); value = value_from_string (s); do_cleanups (old); } + } else if (PyObject_TypeCheck (obj, &value_object_type)) value = ((value_object *) obj)->value; else - error (_("Could not convert Python object: %s"), + PyErr_Format (PyExc_TypeError, _("Could not convert Python object: %s"), PyString_AsString (PyObject_Str (obj))); - - if (PyErr_Occurred ()) - error (_("Error converting Python value.")); + } + if (except.reason < 0) + { + PyErr_Format (except.reason == RETURN_QUIT + ? PyExc_KeyboardInterrupt : PyExc_RuntimeError, + "%s", except.message); + return NULL; + } return value; } /* Returns value object in the ARGth position in GDB's history. */ PyObject * -gdbpy_get_value_from_history (PyObject *self, PyObject *args) +gdbpy_history (PyObject *self, PyObject *args) { int i; struct value *res_val = NULL; /* Initialize to appease gcc warning. */