xref: /dflybsd-src/contrib/gdb-7/gdb/python/py-utils.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
1c50c785cSJohn Marino /* General utility routines for GDB/Python.
2c50c785cSJohn Marino 
3*ef5ccd6cSJohn Marino    Copyright (C) 2008-2013 Free Software Foundation, Inc.
4c50c785cSJohn Marino 
5c50c785cSJohn Marino    This file is part of GDB.
6c50c785cSJohn Marino 
7c50c785cSJohn Marino    This program is free software; you can redistribute it and/or modify
8c50c785cSJohn Marino    it under the terms of the GNU General Public License as published by
9c50c785cSJohn Marino    the Free Software Foundation; either version 3 of the License, or
10c50c785cSJohn Marino    (at your option) any later version.
11c50c785cSJohn Marino 
12c50c785cSJohn Marino    This program is distributed in the hope that it will be useful,
13c50c785cSJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
14c50c785cSJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15c50c785cSJohn Marino    GNU General Public License for more details.
16c50c785cSJohn Marino 
17c50c785cSJohn Marino    You should have received a copy of the GNU General Public License
18c50c785cSJohn Marino    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19c50c785cSJohn Marino 
20c50c785cSJohn Marino #include "defs.h"
21c50c785cSJohn Marino #include "charset.h"
22c50c785cSJohn Marino #include "value.h"
23c50c785cSJohn Marino #include "python-internal.h"
24c50c785cSJohn Marino 
25c50c785cSJohn Marino 
26c50c785cSJohn Marino /* This is a cleanup function which decrements the refcount on a
27c50c785cSJohn Marino    Python object.  */
28c50c785cSJohn Marino 
29c50c785cSJohn Marino static void
py_decref(void * p)30c50c785cSJohn Marino py_decref (void *p)
31c50c785cSJohn Marino {
32c50c785cSJohn Marino   PyObject *py = p;
33c50c785cSJohn Marino 
34c50c785cSJohn Marino   /* Note that we need the extra braces in this 'if' to avoid a
35c50c785cSJohn Marino      warning from gcc.  */
36c50c785cSJohn Marino   if (py)
37c50c785cSJohn Marino     {
38c50c785cSJohn Marino       Py_DECREF (py);
39c50c785cSJohn Marino     }
40c50c785cSJohn Marino }
41c50c785cSJohn Marino 
42c50c785cSJohn Marino /* Return a new cleanup which will decrement the Python object's
43c50c785cSJohn Marino    refcount when run.  */
44c50c785cSJohn Marino 
45c50c785cSJohn Marino struct cleanup *
make_cleanup_py_decref(PyObject * py)46c50c785cSJohn Marino make_cleanup_py_decref (PyObject *py)
47c50c785cSJohn Marino {
48c50c785cSJohn Marino   return make_cleanup (py_decref, (void *) py);
49c50c785cSJohn Marino }
50c50c785cSJohn Marino 
51c50c785cSJohn Marino /* Converts a Python 8-bit string to a unicode string object.  Assumes the
52c50c785cSJohn Marino    8-bit string is in the host charset.  If an error occurs during conversion,
53c50c785cSJohn Marino    returns NULL with a python exception set.
54c50c785cSJohn Marino 
55c50c785cSJohn Marino    As an added bonus, the functions accepts a unicode string and returns it
56c50c785cSJohn Marino    right away, so callers don't need to check which kind of string they've
57*ef5ccd6cSJohn Marino    got.  In Python 3, all strings are Unicode so this case is always the
58*ef5ccd6cSJohn Marino    one that applies.
59c50c785cSJohn Marino 
60c50c785cSJohn Marino    If the given object is not one of the mentioned string types, NULL is
61c50c785cSJohn Marino    returned, with the TypeError python exception set.  */
62c50c785cSJohn Marino PyObject *
python_string_to_unicode(PyObject * obj)63c50c785cSJohn Marino python_string_to_unicode (PyObject *obj)
64c50c785cSJohn Marino {
65c50c785cSJohn Marino   PyObject *unicode_str;
66c50c785cSJohn Marino 
67c50c785cSJohn Marino   /* If obj is already a unicode string, just return it.
68c50c785cSJohn Marino      I wish life was always that simple...  */
69c50c785cSJohn Marino   if (PyUnicode_Check (obj))
70c50c785cSJohn Marino     {
71c50c785cSJohn Marino       unicode_str = obj;
72c50c785cSJohn Marino       Py_INCREF (obj);
73c50c785cSJohn Marino     }
74*ef5ccd6cSJohn Marino #ifndef IS_PY3K
75c50c785cSJohn Marino   else if (PyString_Check (obj))
76c50c785cSJohn Marino     unicode_str = PyUnicode_FromEncodedObject (obj, host_charset (), NULL);
77*ef5ccd6cSJohn Marino #endif
78c50c785cSJohn Marino   else
79c50c785cSJohn Marino     {
80c50c785cSJohn Marino       PyErr_SetString (PyExc_TypeError,
81c50c785cSJohn Marino 		       _("Expected a string or unicode object."));
82c50c785cSJohn Marino       unicode_str = NULL;
83c50c785cSJohn Marino     }
84c50c785cSJohn Marino 
85c50c785cSJohn Marino   return unicode_str;
86c50c785cSJohn Marino }
87c50c785cSJohn Marino 
88c50c785cSJohn Marino /* Returns a newly allocated string with the contents of the given unicode
89c50c785cSJohn Marino    string object converted to CHARSET.  If an error occurs during the
90c50c785cSJohn Marino    conversion, NULL will be returned and a python exception will be set.
91c50c785cSJohn Marino 
92c50c785cSJohn Marino    The caller is responsible for xfree'ing the string.  */
93c50c785cSJohn Marino static char *
unicode_to_encoded_string(PyObject * unicode_str,const char * charset)94c50c785cSJohn Marino unicode_to_encoded_string (PyObject *unicode_str, const char *charset)
95c50c785cSJohn Marino {
96c50c785cSJohn Marino   char *result;
97c50c785cSJohn Marino   PyObject *string;
98c50c785cSJohn Marino 
99c50c785cSJohn Marino   /* Translate string to named charset.  */
100c50c785cSJohn Marino   string = PyUnicode_AsEncodedString (unicode_str, charset, NULL);
101c50c785cSJohn Marino   if (string == NULL)
102c50c785cSJohn Marino     return NULL;
103c50c785cSJohn Marino 
104*ef5ccd6cSJohn Marino #ifdef IS_PY3K
105*ef5ccd6cSJohn Marino   result = xstrdup (PyBytes_AsString (string));
106*ef5ccd6cSJohn Marino #else
107c50c785cSJohn Marino   result = xstrdup (PyString_AsString (string));
108*ef5ccd6cSJohn Marino #endif
109c50c785cSJohn Marino 
110c50c785cSJohn Marino   Py_DECREF (string);
111c50c785cSJohn Marino 
112c50c785cSJohn Marino   return result;
113c50c785cSJohn Marino }
114c50c785cSJohn Marino 
115c50c785cSJohn Marino /* Returns a PyObject with the contents of the given unicode string
116c50c785cSJohn Marino    object converted to a named charset.  If an error occurs during
117c50c785cSJohn Marino    the conversion, NULL will be returned and a python exception will
118c50c785cSJohn Marino    be set.  */
119c50c785cSJohn Marino static PyObject *
unicode_to_encoded_python_string(PyObject * unicode_str,const char * charset)120c50c785cSJohn Marino unicode_to_encoded_python_string (PyObject *unicode_str, const char *charset)
121c50c785cSJohn Marino {
122c50c785cSJohn Marino   /* Translate string to named charset.  */
123*ef5ccd6cSJohn Marino   return PyUnicode_AsEncodedString (unicode_str, charset, NULL);
124c50c785cSJohn Marino }
125c50c785cSJohn Marino 
126c50c785cSJohn Marino /* Returns a newly allocated string with the contents of the given unicode
127c50c785cSJohn Marino    string object converted to the target's charset.  If an error occurs during
128c50c785cSJohn Marino    the conversion, NULL will be returned and a python exception will be set.
129c50c785cSJohn Marino 
130c50c785cSJohn Marino    The caller is responsible for xfree'ing the string.  */
131c50c785cSJohn Marino char *
unicode_to_target_string(PyObject * unicode_str)132c50c785cSJohn Marino unicode_to_target_string (PyObject *unicode_str)
133c50c785cSJohn Marino {
134c50c785cSJohn Marino   return unicode_to_encoded_string (unicode_str,
135c50c785cSJohn Marino 				    target_charset (python_gdbarch));
136c50c785cSJohn Marino }
137c50c785cSJohn Marino 
138c50c785cSJohn Marino /* Returns a PyObject with the contents of the given unicode string
139c50c785cSJohn Marino    object converted to the target's charset.  If an error occurs
140c50c785cSJohn Marino    during the conversion, NULL will be returned and a python exception
141c50c785cSJohn Marino    will be set.  */
142*ef5ccd6cSJohn Marino static PyObject *
unicode_to_target_python_string(PyObject * unicode_str)143c50c785cSJohn Marino unicode_to_target_python_string (PyObject *unicode_str)
144c50c785cSJohn Marino {
145c50c785cSJohn Marino   return unicode_to_encoded_python_string (unicode_str,
146c50c785cSJohn Marino 					   target_charset (python_gdbarch));
147c50c785cSJohn Marino }
148c50c785cSJohn Marino 
149c50c785cSJohn Marino /* Converts a python string (8-bit or unicode) to a target string in
150c50c785cSJohn Marino    the target's charset.  Returns NULL on error, with a python exception set.
151c50c785cSJohn Marino 
152c50c785cSJohn Marino    The caller is responsible for xfree'ing the string.  */
153c50c785cSJohn Marino char *
python_string_to_target_string(PyObject * obj)154c50c785cSJohn Marino python_string_to_target_string (PyObject *obj)
155c50c785cSJohn Marino {
156c50c785cSJohn Marino   PyObject *str;
157c50c785cSJohn Marino   char *result;
158c50c785cSJohn Marino 
159c50c785cSJohn Marino   str = python_string_to_unicode (obj);
160c50c785cSJohn Marino   if (str == NULL)
161c50c785cSJohn Marino     return NULL;
162c50c785cSJohn Marino 
163c50c785cSJohn Marino   result = unicode_to_target_string (str);
164c50c785cSJohn Marino   Py_DECREF (str);
165c50c785cSJohn Marino   return result;
166c50c785cSJohn Marino }
167c50c785cSJohn Marino 
168c50c785cSJohn Marino /* Converts a python string (8-bit or unicode) to a target string in the
169c50c785cSJohn Marino    target's charset.  Returns NULL on error, with a python exception
170*ef5ccd6cSJohn Marino    set.
171*ef5ccd6cSJohn Marino 
172*ef5ccd6cSJohn Marino    In Python 3, the returned object is a "bytes" object (not a string).  */
173c50c785cSJohn Marino PyObject *
python_string_to_target_python_string(PyObject * obj)174c50c785cSJohn Marino python_string_to_target_python_string (PyObject *obj)
175c50c785cSJohn Marino {
176c50c785cSJohn Marino   PyObject *str;
177c50c785cSJohn Marino   PyObject *result;
178c50c785cSJohn Marino 
179c50c785cSJohn Marino   str = python_string_to_unicode (obj);
180c50c785cSJohn Marino   if (str == NULL)
181c50c785cSJohn Marino     return NULL;
182c50c785cSJohn Marino 
183c50c785cSJohn Marino   result = unicode_to_target_python_string (str);
184c50c785cSJohn Marino   Py_DECREF (str);
185c50c785cSJohn Marino   return result;
186c50c785cSJohn Marino }
187c50c785cSJohn Marino 
188c50c785cSJohn Marino /* Converts a python string (8-bit or unicode) to a target string in
189c50c785cSJohn Marino    the host's charset.  Returns NULL on error, with a python exception set.
190c50c785cSJohn Marino 
191c50c785cSJohn Marino    The caller is responsible for xfree'ing the string.  */
192c50c785cSJohn Marino char *
python_string_to_host_string(PyObject * obj)193c50c785cSJohn Marino python_string_to_host_string (PyObject *obj)
194c50c785cSJohn Marino {
195c50c785cSJohn Marino   PyObject *str;
196c50c785cSJohn Marino   char *result;
197c50c785cSJohn Marino 
198c50c785cSJohn Marino   str = python_string_to_unicode (obj);
199c50c785cSJohn Marino   if (str == NULL)
200c50c785cSJohn Marino     return NULL;
201c50c785cSJohn Marino 
202c50c785cSJohn Marino   result = unicode_to_encoded_string (str, host_charset ());
203c50c785cSJohn Marino   Py_DECREF (str);
204c50c785cSJohn Marino   return result;
205c50c785cSJohn Marino }
206c50c785cSJohn Marino 
207c50c785cSJohn Marino /* Return true if OBJ is a Python string or unicode object, false
208c50c785cSJohn Marino    otherwise.  */
209c50c785cSJohn Marino 
210c50c785cSJohn Marino int
gdbpy_is_string(PyObject * obj)211c50c785cSJohn Marino gdbpy_is_string (PyObject *obj)
212c50c785cSJohn Marino {
213*ef5ccd6cSJohn Marino #ifdef IS_PY3K
214*ef5ccd6cSJohn Marino   return PyUnicode_Check (obj);
215*ef5ccd6cSJohn Marino #else
216c50c785cSJohn Marino   return PyString_Check (obj) || PyUnicode_Check (obj);
217*ef5ccd6cSJohn Marino #endif
218c50c785cSJohn Marino }
219c50c785cSJohn Marino 
220c50c785cSJohn Marino /* Return the string representation of OBJ, i.e., str (obj).
221c50c785cSJohn Marino    Space for the result is malloc'd, the caller must free.
222c50c785cSJohn Marino    If the result is NULL a python error occurred, the caller must clear it.  */
223c50c785cSJohn Marino 
224c50c785cSJohn Marino char *
gdbpy_obj_to_string(PyObject * obj)225c50c785cSJohn Marino gdbpy_obj_to_string (PyObject *obj)
226c50c785cSJohn Marino {
227c50c785cSJohn Marino   PyObject *str_obj = PyObject_Str (obj);
228c50c785cSJohn Marino 
229c50c785cSJohn Marino   if (str_obj != NULL)
230c50c785cSJohn Marino     {
231*ef5ccd6cSJohn Marino #ifdef IS_PY3K
232*ef5ccd6cSJohn Marino       char *msg = python_string_to_host_string (str_obj);
233*ef5ccd6cSJohn Marino #else
234c50c785cSJohn Marino       char *msg = xstrdup (PyString_AsString (str_obj));
235*ef5ccd6cSJohn Marino #endif
236c50c785cSJohn Marino 
237c50c785cSJohn Marino       Py_DECREF (str_obj);
238c50c785cSJohn Marino       return msg;
239c50c785cSJohn Marino     }
240c50c785cSJohn Marino 
241c50c785cSJohn Marino   return NULL;
242c50c785cSJohn Marino }
243c50c785cSJohn Marino 
244c50c785cSJohn Marino /* Return the string representation of the exception represented by
245c50c785cSJohn Marino    TYPE, VALUE which is assumed to have been obtained with PyErr_Fetch,
246c50c785cSJohn Marino    i.e., the error indicator is currently clear.
247c50c785cSJohn Marino    Space for the result is malloc'd, the caller must free.
248c50c785cSJohn Marino    If the result is NULL a python error occurred, the caller must clear it.  */
249c50c785cSJohn Marino 
250c50c785cSJohn Marino char *
gdbpy_exception_to_string(PyObject * ptype,PyObject * pvalue)251c50c785cSJohn Marino gdbpy_exception_to_string (PyObject *ptype, PyObject *pvalue)
252c50c785cSJohn Marino {
253c50c785cSJohn Marino   char *str;
254c50c785cSJohn Marino 
255c50c785cSJohn Marino   /* There are a few cases to consider.
256c50c785cSJohn Marino      For example:
257c50c785cSJohn Marino      pvalue is a string when PyErr_SetString is used.
258c50c785cSJohn Marino      pvalue is not a string when raise "foo" is used, instead it is None
259c50c785cSJohn Marino      and ptype is "foo".
260c50c785cSJohn Marino      So the algorithm we use is to print `str (pvalue)' if it's not
261c50c785cSJohn Marino      None, otherwise we print `str (ptype)'.
262c50c785cSJohn Marino      Using str (aka PyObject_Str) will fetch the error message from
263c50c785cSJohn Marino      gdb.GdbError ("message").  */
264c50c785cSJohn Marino 
265c50c785cSJohn Marino   if (pvalue && pvalue != Py_None)
266c50c785cSJohn Marino     str = gdbpy_obj_to_string (pvalue);
267c50c785cSJohn Marino   else
268c50c785cSJohn Marino     str = gdbpy_obj_to_string (ptype);
269c50c785cSJohn Marino 
270c50c785cSJohn Marino   return str;
271c50c785cSJohn Marino }
272c50c785cSJohn Marino 
273c50c785cSJohn Marino /* Convert a GDB exception to the appropriate Python exception.
274c50c785cSJohn Marino 
275c50c785cSJohn Marino    This sets the Python error indicator, and returns NULL.  */
276c50c785cSJohn Marino 
277c50c785cSJohn Marino PyObject *
gdbpy_convert_exception(struct gdb_exception exception)278c50c785cSJohn Marino gdbpy_convert_exception (struct gdb_exception exception)
279c50c785cSJohn Marino {
280c50c785cSJohn Marino   PyObject *exc_class;
281c50c785cSJohn Marino 
282c50c785cSJohn Marino   if (exception.reason == RETURN_QUIT)
283c50c785cSJohn Marino     exc_class = PyExc_KeyboardInterrupt;
284c50c785cSJohn Marino   else if (exception.error == MEMORY_ERROR)
285c50c785cSJohn Marino     exc_class = gdbpy_gdb_memory_error;
286c50c785cSJohn Marino   else
287c50c785cSJohn Marino     exc_class = gdbpy_gdb_error;
288c50c785cSJohn Marino 
289c50c785cSJohn Marino   return PyErr_Format (exc_class, "%s", exception.message);
290c50c785cSJohn Marino }
291c50c785cSJohn Marino 
292c50c785cSJohn Marino /* Converts OBJ to a CORE_ADDR value.
293c50c785cSJohn Marino 
294c50c785cSJohn Marino    Returns 1 on success or 0 on failure, with a Python exception set.  This
295c50c785cSJohn Marino    function can also throw GDB exceptions.
296c50c785cSJohn Marino */
297c50c785cSJohn Marino 
298c50c785cSJohn Marino int
get_addr_from_python(PyObject * obj,CORE_ADDR * addr)299c50c785cSJohn Marino get_addr_from_python (PyObject *obj, CORE_ADDR *addr)
300c50c785cSJohn Marino {
301c50c785cSJohn Marino   if (gdbpy_is_value_object (obj))
302c50c785cSJohn Marino     *addr = value_as_address (value_object_to_value (obj));
303c50c785cSJohn Marino   else
304c50c785cSJohn Marino     {
305c50c785cSJohn Marino       PyObject *num = PyNumber_Long (obj);
306c50c785cSJohn Marino       gdb_py_ulongest val;
307c50c785cSJohn Marino 
308c50c785cSJohn Marino       if (num == NULL)
309c50c785cSJohn Marino 	return 0;
310c50c785cSJohn Marino 
311c50c785cSJohn Marino       val = gdb_py_long_as_ulongest (num);
312c50c785cSJohn Marino       Py_XDECREF (num);
313c50c785cSJohn Marino       if (PyErr_Occurred ())
314c50c785cSJohn Marino 	return 0;
315c50c785cSJohn Marino 
316c50c785cSJohn Marino       if (sizeof (val) > sizeof (CORE_ADDR) && ((CORE_ADDR) val) != val)
317c50c785cSJohn Marino 	{
318c50c785cSJohn Marino 	  PyErr_SetString (PyExc_ValueError,
319c50c785cSJohn Marino 			   _("Overflow converting to address."));
320c50c785cSJohn Marino 	  return 0;
321c50c785cSJohn Marino 	}
322c50c785cSJohn Marino 
323c50c785cSJohn Marino       *addr = val;
324c50c785cSJohn Marino     }
325c50c785cSJohn Marino 
326c50c785cSJohn Marino   return 1;
327c50c785cSJohn Marino }
328c50c785cSJohn Marino 
329c50c785cSJohn Marino /* Convert a LONGEST to the appropriate Python object -- either an
330c50c785cSJohn Marino    integer object or a long object, depending on its value.  */
331c50c785cSJohn Marino 
332c50c785cSJohn Marino PyObject *
gdb_py_object_from_longest(LONGEST l)333c50c785cSJohn Marino gdb_py_object_from_longest (LONGEST l)
334c50c785cSJohn Marino {
335*ef5ccd6cSJohn Marino #ifdef IS_PY3K
336*ef5ccd6cSJohn Marino   if (sizeof (l) > sizeof (long))
337*ef5ccd6cSJohn Marino     return PyLong_FromLongLong (l);
338*ef5ccd6cSJohn Marino   return PyLong_FromLong (l);
339*ef5ccd6cSJohn Marino #else
340c50c785cSJohn Marino #ifdef HAVE_LONG_LONG		/* Defined by Python.  */
341c50c785cSJohn Marino   /* If we have 'long long', and the value overflows a 'long', use a
342c50c785cSJohn Marino      Python Long; otherwise use a Python Int.  */
343c50c785cSJohn Marino   if (sizeof (l) > sizeof (long)
344c50c785cSJohn Marino       && (l > PyInt_GetMax () || l < (- (LONGEST) PyInt_GetMax ()) - 1))
345c50c785cSJohn Marino     return PyLong_FromLongLong (l);
346c50c785cSJohn Marino #endif
347c50c785cSJohn Marino   return PyInt_FromLong (l);
348*ef5ccd6cSJohn Marino #endif
349c50c785cSJohn Marino }
350c50c785cSJohn Marino 
351c50c785cSJohn Marino /* Convert a ULONGEST to the appropriate Python object -- either an
352c50c785cSJohn Marino    integer object or a long object, depending on its value.  */
353c50c785cSJohn Marino 
354c50c785cSJohn Marino PyObject *
gdb_py_object_from_ulongest(ULONGEST l)355c50c785cSJohn Marino gdb_py_object_from_ulongest (ULONGEST l)
356c50c785cSJohn Marino {
357*ef5ccd6cSJohn Marino #ifdef IS_PY3K
358*ef5ccd6cSJohn Marino   if (sizeof (l) > sizeof (unsigned long))
359*ef5ccd6cSJohn Marino     return PyLong_FromUnsignedLongLong (l);
360*ef5ccd6cSJohn Marino   return PyLong_FromUnsignedLong (l);
361*ef5ccd6cSJohn Marino #else
362c50c785cSJohn Marino #ifdef HAVE_LONG_LONG		/* Defined by Python.  */
363c50c785cSJohn Marino   /* If we have 'long long', and the value overflows a 'long', use a
364c50c785cSJohn Marino      Python Long; otherwise use a Python Int.  */
365c50c785cSJohn Marino   if (sizeof (l) > sizeof (unsigned long) && l > PyInt_GetMax ())
366c50c785cSJohn Marino     return PyLong_FromUnsignedLongLong (l);
367c50c785cSJohn Marino #endif
368c50c785cSJohn Marino 
369c50c785cSJohn Marino   if (l > PyInt_GetMax ())
370c50c785cSJohn Marino     return PyLong_FromUnsignedLong (l);
371c50c785cSJohn Marino 
372c50c785cSJohn Marino   return PyInt_FromLong (l);
373*ef5ccd6cSJohn Marino #endif
374c50c785cSJohn Marino }
375c50c785cSJohn Marino 
376c50c785cSJohn Marino /* Like PyInt_AsLong, but returns 0 on failure, 1 on success, and puts
377c50c785cSJohn Marino    the value into an out parameter.  */
378c50c785cSJohn Marino 
379c50c785cSJohn Marino int
gdb_py_int_as_long(PyObject * obj,long * result)380c50c785cSJohn Marino gdb_py_int_as_long (PyObject *obj, long *result)
381c50c785cSJohn Marino {
382c50c785cSJohn Marino   *result = PyInt_AsLong (obj);
383c50c785cSJohn Marino   return ! (*result == -1 && PyErr_Occurred ());
384c50c785cSJohn Marino }
385*ef5ccd6cSJohn Marino 
386*ef5ccd6cSJohn Marino 
387*ef5ccd6cSJohn Marino 
388*ef5ccd6cSJohn Marino /* Generic implementation of the __dict__ attribute for objects that
389*ef5ccd6cSJohn Marino    have a dictionary.  The CLOSURE argument should be the type object.
390*ef5ccd6cSJohn Marino    This only handles positive values for tp_dictoffset.  */
391*ef5ccd6cSJohn Marino 
392*ef5ccd6cSJohn Marino PyObject *
gdb_py_generic_dict(PyObject * self,void * closure)393*ef5ccd6cSJohn Marino gdb_py_generic_dict (PyObject *self, void *closure)
394*ef5ccd6cSJohn Marino {
395*ef5ccd6cSJohn Marino   PyObject *result;
396*ef5ccd6cSJohn Marino   PyTypeObject *type_obj = closure;
397*ef5ccd6cSJohn Marino   char *raw_ptr;
398*ef5ccd6cSJohn Marino 
399*ef5ccd6cSJohn Marino   raw_ptr = (char *) self + type_obj->tp_dictoffset;
400*ef5ccd6cSJohn Marino   result = * (PyObject **) raw_ptr;
401*ef5ccd6cSJohn Marino 
402*ef5ccd6cSJohn Marino   Py_INCREF (result);
403*ef5ccd6cSJohn Marino   return result;
404*ef5ccd6cSJohn Marino }
405