/* General utility routines for GDB/Python.
- Copyright (C) 2008-2013 Free Software Foundation, Inc.
+ Copyright (C) 2008-2014 Free Software Foundation, Inc.
This file is part of GDB.
{
PyObject *py = p;
- /* Note that we need the extra braces in this 'if' to avoid a
- warning from gcc. */
- if (py)
- {
- Py_DECREF (py);
- }
+ Py_DECREF (py);
}
/* Return a new cleanup which will decrement the Python object's
return make_cleanup (py_decref, (void *) py);
}
+/* This is a cleanup function which decrements the refcount on a
+ Python object. This function accounts appropriately for NULL
+ references. */
+
+static void
+py_xdecref (void *p)
+{
+ PyObject *py = p;
+
+ Py_XDECREF (py);
+}
+
+/* Return a new cleanup which will decrement the Python object's
+ refcount when run. Account for and operate on NULL references
+ correctly. */
+
+struct cleanup *
+make_cleanup_py_xdecref (PyObject *py)
+{
+ return make_cleanup (py_xdecref, py);
+}
+
/* Converts a Python 8-bit string to a unicode string object. Assumes the
8-bit string is in the host charset. If an error occurs during conversion,
returns NULL with a python exception set.
As an added bonus, the functions accepts a unicode string and returns it
right away, so callers don't need to check which kind of string they've
- got. In Python 3, all strings are Unicode so this case is always the
+ got. In Python 3, all strings are Unicode so this case is always the
one that applies.
If the given object is not one of the mentioned string types, NULL is
if (str == NULL)
return NULL;
- result = unicode_to_encoded_string (str, host_charset ());
+ result = unicode_to_encoded_string (str, host_charset ());
Py_DECREF (str);
return result;
}
}
/* Convert a GDB exception to the appropriate Python exception.
-
- This sets the Python error indicator, and returns NULL. */
-PyObject *
+ This sets the Python error indicator. */
+
+void
gdbpy_convert_exception (struct gdb_exception exception)
{
PyObject *exc_class;
else
exc_class = gdbpy_gdb_error;
- return PyErr_Format (exc_class, "%s", exception.message);
+ PyErr_Format (exc_class, "%s", exception.message);
}
/* Converts OBJ to a CORE_ADDR value.
- Returns 1 on success or 0 on failure, with a Python exception set. This
- function can also throw GDB exceptions.
+ Returns 0 on success or -1 on failure, with a Python exception set.
*/
int
get_addr_from_python (PyObject *obj, CORE_ADDR *addr)
{
if (gdbpy_is_value_object (obj))
- *addr = value_as_address (value_object_to_value (obj));
+ {
+ volatile struct gdb_exception except;
+
+ TRY_CATCH (except, RETURN_MASK_ALL)
+ {
+ *addr = value_as_address (value_object_to_value (obj));
+ }
+ GDB_PY_SET_HANDLE_EXCEPTION (except);
+ }
else
{
PyObject *num = PyNumber_Long (obj);
gdb_py_ulongest val;
if (num == NULL)
- return 0;
+ return -1;
val = gdb_py_long_as_ulongest (num);
Py_XDECREF (num);
if (PyErr_Occurred ())
- return 0;
+ return -1;
if (sizeof (val) > sizeof (CORE_ADDR) && ((CORE_ADDR) val) != val)
{
PyErr_SetString (PyExc_ValueError,
_("Overflow converting to address."));
- return 0;
+ return -1;
}
*addr = val;
}
- return 1;
+ return 0;
}
/* Convert a LONGEST to the appropriate Python object -- either an
Py_INCREF (result);
return result;
}
+
+/* Like PyModule_AddObject, but does not steal a reference to
+ OBJECT. */
+
+int
+gdb_pymodule_addobject (PyObject *module, const char *name, PyObject *object)
+{
+ int result;
+
+ Py_INCREF (object);
+ /* Python 2.4 did not have a 'const' here. */
+ result = PyModule_AddObject (module, (char *) name, object);
+ if (result < 0)
+ Py_DECREF (object);
+ return result;
+}