+
+ return 0;
+}
+
+/* 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 IS_PY3K
+ if (sizeof (l) > sizeof (long))
+ return PyLong_FromLongLong (l);
+ return PyLong_FromLong (l);
+#else
+#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);
+#endif
+}
+
+/* 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 IS_PY3K
+ if (sizeof (l) > sizeof (unsigned long))
+ return PyLong_FromUnsignedLongLong (l);
+ return PyLong_FromUnsignedLong (l);
+#else
+#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);
+#endif
+}
+
+/* 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 ());
+}
+
+\f
+
+/* Generic implementation of the __dict__ attribute for objects that
+ have a dictionary. The CLOSURE argument should be the type object.
+ This only handles positive values for tp_dictoffset. */
+
+PyObject *
+gdb_py_generic_dict (PyObject *self, void *closure)
+{
+ PyObject *result;
+ PyTypeObject *type_obj = (PyTypeObject *) closure;
+ char *raw_ptr;
+
+ raw_ptr = (char *) self + type_obj->tp_dictoffset;
+ result = * (PyObject **) raw_ptr;
+
+ 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;
+}
+
+/* Handle a Python exception when the special gdb.GdbError treatment
+ is desired. This should only be called when an exception is set.
+ If the exception is a gdb.GdbError, throw a gdb exception with the
+ exception text. For other exceptions, print the Python stack and
+ then throw a gdb exception. */
+
+void
+gdbpy_handle_exception ()
+{
+ PyObject *ptype, *pvalue, *ptraceback;
+
+ PyErr_Fetch (&ptype, &pvalue, &ptraceback);
+
+ /* Try to fetch an error message contained within ptype, pvalue.
+ When fetching the error message we need to make our own copy,
+ we no longer own ptype, pvalue after the call to PyErr_Restore. */
+
+ gdb::unique_xmalloc_ptr<char>
+ msg (gdbpy_exception_to_string (ptype, pvalue));
+
+ if (msg == NULL)