]> Git Repo - binutils.git/blob - gdb/python/py-prettyprint.c
Fix PR python/18984
[binutils.git] / gdb / python / py-prettyprint.c
1 /* Python pretty-printing
2
3    Copyright (C) 2008-2016 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "objfiles.h"
22 #include "symtab.h"
23 #include "language.h"
24 #include "valprint.h"
25 #include "extension-priv.h"
26 #include "python.h"
27 #include "python-internal.h"
28
29 /* Return type of print_string_repr.  */
30
31 enum string_repr_result
32   {
33     /* The string method returned None.  */
34     string_repr_none,
35     /* The string method had an error.  */
36     string_repr_error,
37     /* Everything ok.  */
38     string_repr_ok
39   };
40
41 /* Helper function for find_pretty_printer which iterates over a list,
42    calls each function and inspects output.  This will return a
43    printer object if one recognizes VALUE.  If no printer is found, it
44    will return None.  On error, it will set the Python error and
45    return NULL.  */
46
47 static PyObject *
48 search_pp_list (PyObject *list, PyObject *value)
49 {
50   Py_ssize_t pp_list_size, list_index;
51   PyObject *function, *printer = NULL;
52
53   pp_list_size = PyList_Size (list);
54   for (list_index = 0; list_index < pp_list_size; list_index++)
55     {
56       function = PyList_GetItem (list, list_index);
57       if (! function)
58         return NULL;
59
60       /* Skip if disabled.  */
61       if (PyObject_HasAttr (function, gdbpy_enabled_cst))
62         {
63           PyObject *attr = PyObject_GetAttr (function, gdbpy_enabled_cst);
64           int cmp;
65
66           if (!attr)
67             return NULL;
68           cmp = PyObject_IsTrue (attr);
69           Py_DECREF (attr);
70           if (cmp == -1)
71             return NULL;
72
73           if (!cmp)
74             continue;
75         }
76
77       printer = PyObject_CallFunctionObjArgs (function, value, NULL);
78       if (! printer)
79         return NULL;
80       else if (printer != Py_None)
81         return printer;
82
83       Py_DECREF (printer);
84     }
85
86   Py_RETURN_NONE;
87 }
88
89 /* Subroutine of find_pretty_printer to simplify it.
90    Look for a pretty-printer to print VALUE in all objfiles.
91    The result is NULL if there's an error and the search should be terminated.
92    The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
93    Otherwise the result is the pretty-printer function, suitably inc-ref'd.  */
94
95 static PyObject *
96 find_pretty_printer_from_objfiles (PyObject *value)
97 {
98   PyObject *pp_list;
99   PyObject *function;
100   struct objfile *obj;
101
102   ALL_OBJFILES (obj)
103   {
104     PyObject *objf = objfile_to_objfile_object (obj);
105     if (!objf)
106       {
107         /* Ignore the error and continue.  */
108         PyErr_Clear ();
109         continue;
110       }
111
112     pp_list = objfpy_get_printers (objf, NULL);
113     function = search_pp_list (pp_list, value);
114     Py_XDECREF (pp_list);
115
116     /* If there is an error in any objfile list, abort the search and exit.  */
117     if (! function)
118       return NULL;
119
120     if (function != Py_None)
121       return function;
122
123     Py_DECREF (function);
124   }
125
126   Py_RETURN_NONE;
127 }
128
129 /* Subroutine of find_pretty_printer to simplify it.
130    Look for a pretty-printer to print VALUE in the current program space.
131    The result is NULL if there's an error and the search should be terminated.
132    The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
133    Otherwise the result is the pretty-printer function, suitably inc-ref'd.  */
134
135 static PyObject *
136 find_pretty_printer_from_progspace (PyObject *value)
137 {
138   PyObject *pp_list;
139   PyObject *function;
140   PyObject *obj = pspace_to_pspace_object (current_program_space);
141
142   if (!obj)
143     return NULL;
144   pp_list = pspy_get_printers (obj, NULL);
145   function = search_pp_list (pp_list, value);
146   Py_XDECREF (pp_list);
147   return function;
148 }
149
150 /* Subroutine of find_pretty_printer to simplify it.
151    Look for a pretty-printer to print VALUE in the gdb module.
152    The result is NULL if there's an error and the search should be terminated.
153    The result is Py_None, suitably inc-ref'd, if no pretty-printer was found.
154    Otherwise the result is the pretty-printer function, suitably inc-ref'd.  */
155
156 static PyObject *
157 find_pretty_printer_from_gdb (PyObject *value)
158 {
159   PyObject *pp_list;
160   PyObject *function;
161
162   /* Fetch the global pretty printer list.  */
163   if (gdb_python_module == NULL
164       || ! PyObject_HasAttrString (gdb_python_module, "pretty_printers"))
165     Py_RETURN_NONE;
166   pp_list = PyObject_GetAttrString (gdb_python_module, "pretty_printers");
167   if (pp_list == NULL || ! PyList_Check (pp_list))
168     {
169       Py_XDECREF (pp_list);
170       Py_RETURN_NONE;
171     }
172
173   function = search_pp_list (pp_list, value);
174   Py_XDECREF (pp_list);
175   return function;
176 }
177
178 /* Find the pretty-printing constructor function for VALUE.  If no
179    pretty-printer exists, return None.  If one exists, return a new
180    reference.  On error, set the Python error and return NULL.  */
181
182 static PyObject *
183 find_pretty_printer (PyObject *value)
184 {
185   PyObject *function;
186
187   /* Look at the pretty-printer list for each objfile
188      in the current program-space.  */
189   function = find_pretty_printer_from_objfiles (value);
190   if (function == NULL || function != Py_None)
191     return function;
192   Py_DECREF (function);
193
194   /* Look at the pretty-printer list for the current program-space.  */
195   function = find_pretty_printer_from_progspace (value);
196   if (function == NULL || function != Py_None)
197     return function;
198   Py_DECREF (function);
199
200   /* Look at the pretty-printer list in the gdb module.  */
201   function = find_pretty_printer_from_gdb (value);
202   return function;
203 }
204
205 /* Pretty-print a single value, via the printer object PRINTER.
206    If the function returns a string, a PyObject containing the string
207    is returned.  If the function returns Py_NONE that means the pretty
208    printer returned the Python None as a value.  Otherwise, if the
209    function returns a value,  *OUT_VALUE is set to the value, and NULL
210    is returned.  On error, *OUT_VALUE is set to NULL, NULL is
211    returned, with a python exception set.  */
212
213 static PyObject *
214 pretty_print_one_value (PyObject *printer, struct value **out_value)
215 {
216   PyObject *result = NULL;
217
218   *out_value = NULL;
219   TRY
220     {
221       result = PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, NULL);
222       if (result)
223         {
224           if (! gdbpy_is_string (result) && ! gdbpy_is_lazy_string (result)
225               && result != Py_None)
226             {
227               *out_value = convert_value_from_python (result);
228               if (PyErr_Occurred ())
229                 *out_value = NULL;
230               Py_DECREF (result);
231               result = NULL;
232             }
233         }
234     }
235   CATCH (except, RETURN_MASK_ALL)
236     {
237     }
238   END_CATCH
239
240   return result;
241 }
242
243 /* Return the display hint for the object printer, PRINTER.  Return
244    NULL if there is no display_hint method, or if the method did not
245    return a string.  On error, print stack trace and return NULL.  On
246    success, return an xmalloc()d string.  */
247 char *
248 gdbpy_get_display_hint (PyObject *printer)
249 {
250   PyObject *hint;
251   char *result = NULL;
252
253   if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst))
254     return NULL;
255
256   hint = PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst, NULL);
257   if (hint)
258     {
259       if (gdbpy_is_string (hint))
260         {
261           result = python_string_to_host_string (hint);
262           if (result == NULL)
263             gdbpy_print_stack ();
264         }
265       Py_DECREF (hint);
266     }
267   else
268     gdbpy_print_stack ();
269
270   return result;
271 }
272
273 /* A wrapper for gdbpy_print_stack that ignores MemoryError.  */
274
275 static void
276 print_stack_unless_memory_error (struct ui_file *stream)
277 {
278   if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
279     {
280       struct cleanup *cleanup;
281       PyObject *type, *value, *trace;
282       char *msg;
283
284       PyErr_Fetch (&type, &value, &trace);
285       cleanup = make_cleanup_py_decref (type);
286       make_cleanup_py_decref (value);
287       make_cleanup_py_decref (trace);
288
289       msg = gdbpy_exception_to_string (type, value);
290       make_cleanup (xfree, msg);
291
292       if (msg == NULL || *msg == '\0')
293         fprintf_filtered (stream, _("<error reading variable>"));
294       else
295         fprintf_filtered (stream, _("<error reading variable: %s>"), msg);
296
297       do_cleanups (cleanup);
298     }
299   else
300     gdbpy_print_stack ();
301 }
302
303 /* Helper for gdbpy_apply_val_pretty_printer which calls to_string and
304    formats the result.  */
305
306 static enum string_repr_result
307 print_string_repr (PyObject *printer, const char *hint,
308                    struct ui_file *stream, int recurse,
309                    const struct value_print_options *options,
310                    const struct language_defn *language,
311                    struct gdbarch *gdbarch)
312 {
313   struct value *replacement = NULL;
314   PyObject *py_str = NULL;
315   enum string_repr_result result = string_repr_ok;
316
317   py_str = pretty_print_one_value (printer, &replacement);
318   if (py_str)
319     {
320       struct cleanup *cleanup = make_cleanup_py_decref (py_str);
321
322       if (py_str == Py_None)
323         result = string_repr_none;
324       else if (gdbpy_is_lazy_string (py_str))
325         {
326           CORE_ADDR addr;
327           long length;
328           struct type *type;
329           char *encoding = NULL;
330           struct value_print_options local_opts = *options;
331
332           make_cleanup (free_current_contents, &encoding);
333           gdbpy_extract_lazy_string (py_str, &addr, &type,
334                                      &length, &encoding);
335
336           local_opts.addressprint = 0;
337           val_print_string (type, encoding, addr, (int) length,
338                             stream, &local_opts);
339         }
340       else
341         {
342           PyObject *string;
343
344           string = python_string_to_target_python_string (py_str);
345           if (string)
346             {
347               char *output;
348               long length;
349               struct type *type;
350
351               make_cleanup_py_decref (string);
352 #ifdef IS_PY3K
353               output = PyBytes_AS_STRING (string);
354               length = PyBytes_GET_SIZE (string);
355 #else
356               output = PyString_AsString (string);
357               length = PyString_Size (string);
358 #endif
359               type = builtin_type (gdbarch)->builtin_char;
360
361               if (hint && !strcmp (hint, "string"))
362                 LA_PRINT_STRING (stream, type, (gdb_byte *) output,
363                                  length, NULL, 0, options);
364               else
365                 fputs_filtered (output, stream);
366             }
367           else
368             {
369               result = string_repr_error;
370               print_stack_unless_memory_error (stream);
371             }
372         }
373
374       do_cleanups (cleanup);
375     }
376   else if (replacement)
377     {
378       struct value_print_options opts = *options;
379
380       opts.addressprint = 0;
381       common_val_print (replacement, stream, recurse, &opts, language);
382     }
383   else
384     {
385       result = string_repr_error;
386       print_stack_unless_memory_error (stream);
387     }
388
389   return result;
390 }
391
392 #ifndef IS_PY3K
393 static void
394 py_restore_tstate (void *p)
395 {
396   PyFrameObject *frame = (PyFrameObject *) p;
397   PyThreadState *tstate = PyThreadState_GET ();
398
399   tstate->frame = frame;
400 }
401
402 /* Create a dummy PyFrameObject, needed to work around
403    a Python-2.4 bug with generators.  */
404 static PyObject *
405 push_dummy_python_frame (void)
406 {
407   PyObject *empty_string, *null_tuple, *globals;
408   PyCodeObject *code;
409   PyFrameObject *frame;
410   PyThreadState *tstate;
411
412   empty_string = PyString_FromString ("");
413   if (!empty_string)
414     return NULL;
415
416   null_tuple = PyTuple_New (0);
417   if (!null_tuple)
418     {
419       Py_DECREF (empty_string);
420       return NULL;
421     }
422
423   code = PyCode_New (0,                 /* argcount */
424                      0,                 /* nlocals */
425                      0,                 /* stacksize */
426                      0,                 /* flags */
427                      empty_string,      /* code */
428                      null_tuple,        /* consts */
429                      null_tuple,        /* names */
430                      null_tuple,        /* varnames */
431 #if PYTHON_API_VERSION >= 1010
432                      null_tuple,        /* freevars */
433                      null_tuple,        /* cellvars */
434 #endif
435                      empty_string,      /* filename */
436                      empty_string,      /* name */
437                      1,                 /* firstlineno */
438                      empty_string       /* lnotab */
439                     );
440
441   Py_DECREF (empty_string);
442   Py_DECREF (null_tuple);
443
444   if (!code)
445     return NULL;
446
447   globals = PyDict_New ();
448   if (!globals)
449     {
450       Py_DECREF (code);
451       return NULL;
452     }
453
454   tstate = PyThreadState_GET ();
455
456   frame = PyFrame_New (tstate, code, globals, NULL);
457
458   Py_DECREF (globals);
459   Py_DECREF (code);
460
461   if (!frame)
462     return NULL;
463
464   tstate->frame = frame;
465   make_cleanup (py_restore_tstate, frame->f_back);
466   return (PyObject *) frame;
467 }
468 #endif
469
470 /* Helper for gdbpy_apply_val_pretty_printer that formats children of the
471    printer, if any exist.  If is_py_none is true, then nothing has
472    been printed by to_string, and format output accordingly. */
473 static void
474 print_children (PyObject *printer, const char *hint,
475                 struct ui_file *stream, int recurse,
476                 const struct value_print_options *options,
477                 const struct language_defn *language,
478                 int is_py_none)
479 {
480   int is_map, is_array, done_flag, pretty;
481   unsigned int i;
482   PyObject *children, *iter;
483 #ifndef IS_PY3K
484   PyObject *frame;
485 #endif
486   struct cleanup *cleanups;
487
488   if (! PyObject_HasAttr (printer, gdbpy_children_cst))
489     return;
490
491   /* If we are printing a map or an array, we want some special
492      formatting.  */
493   is_map = hint && ! strcmp (hint, "map");
494   is_array = hint && ! strcmp (hint, "array");
495
496   children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
497                                          NULL);
498   if (! children)
499     {
500       print_stack_unless_memory_error (stream);
501       return;
502     }
503
504   cleanups = make_cleanup_py_decref (children);
505
506   iter = PyObject_GetIter (children);
507   if (!iter)
508     {
509       print_stack_unless_memory_error (stream);
510       goto done;
511     }
512   make_cleanup_py_decref (iter);
513
514   /* Use the prettyformat_arrays option if we are printing an array,
515      and the pretty option otherwise.  */
516   if (is_array)
517     pretty = options->prettyformat_arrays;
518   else
519     {
520       if (options->prettyformat == Val_prettyformat)
521         pretty = 1;
522       else
523         pretty = options->prettyformat_structs;
524     }
525
526   /* Manufacture a dummy Python frame to work around Python 2.4 bug,
527      where it insists on having a non-NULL tstate->frame when
528      a generator is called.  */
529 #ifndef IS_PY3K
530   frame = push_dummy_python_frame ();
531   if (!frame)
532     {
533       gdbpy_print_stack ();
534       goto done;
535     }
536   make_cleanup_py_decref (frame);
537 #endif
538
539   done_flag = 0;
540   for (i = 0; i < options->print_max; ++i)
541     {
542       PyObject *py_v, *item = PyIter_Next (iter);
543       const char *name;
544       struct cleanup *inner_cleanup;
545
546       if (! item)
547         {
548           if (PyErr_Occurred ())
549             print_stack_unless_memory_error (stream);
550           /* Set a flag so we can know whether we printed all the
551              available elements.  */
552           else  
553             done_flag = 1;
554           break;
555         }
556
557       if (! PyTuple_Check (item) || PyTuple_Size (item) != 2)
558         {
559           PyErr_SetString (PyExc_TypeError,
560                            _("Result of children iterator not a tuple"
561                              " of two elements."));
562           gdbpy_print_stack ();
563           Py_DECREF (item);
564           continue;
565         }
566       if (! PyArg_ParseTuple (item, "sO", &name, &py_v))
567         {
568           /* The user won't necessarily get a stack trace here, so provide
569              more context.  */
570           if (gdbpy_print_python_errors_p ())
571             fprintf_unfiltered (gdb_stderr,
572                                 _("Bad result from children iterator.\n"));
573           gdbpy_print_stack ();
574           Py_DECREF (item);
575           continue;
576         }
577       inner_cleanup = make_cleanup_py_decref (item);
578
579       /* Print initial "{".  For other elements, there are three
580          cases:
581          1. Maps.  Print a "," after each value element.
582          2. Arrays.  Always print a ",".
583          3. Other.  Always print a ",".  */
584       if (i == 0)
585         {
586          if (is_py_none)
587            fputs_filtered ("{", stream);
588          else
589            fputs_filtered (" = {", stream);
590        }
591
592       else if (! is_map || i % 2 == 0)
593         fputs_filtered (pretty ? "," : ", ", stream);
594
595       /* In summary mode, we just want to print "= {...}" if there is
596          a value.  */
597       if (options->summary)
598         {
599           /* This increment tricks the post-loop logic to print what
600              we want.  */
601           ++i;
602           /* Likewise.  */
603           pretty = 0;
604           break;
605         }
606
607       if (! is_map || i % 2 == 0)
608         {
609           if (pretty)
610             {
611               fputs_filtered ("\n", stream);
612               print_spaces_filtered (2 + 2 * recurse, stream);
613             }
614           else
615             wrap_here (n_spaces (2 + 2 *recurse));
616         }
617
618       if (is_map && i % 2 == 0)
619         fputs_filtered ("[", stream);
620       else if (is_array)
621         {
622           /* We print the index, not whatever the child method
623              returned as the name.  */
624           if (options->print_array_indexes)
625             fprintf_filtered (stream, "[%d] = ", i);
626         }
627       else if (! is_map)
628         {
629           fputs_filtered (name, stream);
630           fputs_filtered (" = ", stream);
631         }
632
633       if (gdbpy_is_lazy_string (py_v))
634         {
635           CORE_ADDR addr;
636           struct type *type;
637           long length;
638           char *encoding = NULL;
639           struct value_print_options local_opts = *options;
640
641           make_cleanup (free_current_contents, &encoding);
642           gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding);
643
644           local_opts.addressprint = 0;
645           val_print_string (type, encoding, addr, (int) length, stream,
646                             &local_opts);
647         }
648       else if (gdbpy_is_string (py_v))
649         {
650           char *output;
651
652           output = python_string_to_host_string (py_v);
653           if (!output)
654             gdbpy_print_stack ();
655           else
656             {
657               fputs_filtered (output, stream);
658               xfree (output);
659             }
660         }
661       else
662         {
663           struct value *value = convert_value_from_python (py_v);
664
665           if (value == NULL)
666             {
667               gdbpy_print_stack ();
668               error (_("Error while executing Python code."));
669             }
670           else
671             common_val_print (value, stream, recurse + 1, options, language);
672         }
673
674       if (is_map && i % 2 == 0)
675         fputs_filtered ("] = ", stream);
676
677       do_cleanups (inner_cleanup);
678     }
679
680   if (i)
681     {
682       if (!done_flag)
683         {
684           if (pretty)
685             {
686               fputs_filtered ("\n", stream);
687               print_spaces_filtered (2 + 2 * recurse, stream);
688             }
689           fputs_filtered ("...", stream);
690         }
691       if (pretty)
692         {
693           fputs_filtered ("\n", stream);
694           print_spaces_filtered (2 * recurse, stream);
695         }
696       fputs_filtered ("}", stream);
697     }
698
699  done:
700   do_cleanups (cleanups);
701 }
702
703 enum ext_lang_rc
704 gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang,
705                                 struct type *type, const gdb_byte *valaddr,
706                                 int embedded_offset, CORE_ADDR address,
707                                 struct ui_file *stream, int recurse,
708                                 const struct value *val,
709                                 const struct value_print_options *options,
710                                 const struct language_defn *language)
711 {
712   struct gdbarch *gdbarch = get_type_arch (type);
713   PyObject *printer = NULL;
714   PyObject *val_obj = NULL;
715   struct value *value;
716   char *hint = NULL;
717   struct cleanup *cleanups;
718   enum ext_lang_rc result = EXT_LANG_RC_NOP;
719   enum string_repr_result print_result;
720
721   /* No pretty-printer support for unavailable values.  */
722   if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
723     return EXT_LANG_RC_NOP;
724
725   if (!gdb_python_initialized)
726     return EXT_LANG_RC_NOP;
727
728   cleanups = ensure_python_env (gdbarch, language);
729
730   /* Instantiate the printer.  */
731   if (valaddr)
732     valaddr += embedded_offset;
733   value = value_from_contents_and_address (type, valaddr,
734                                            address + embedded_offset);
735
736   set_value_component_location (value, val);
737   /* set_value_component_location resets the address, so we may
738      need to set it again.  */
739   if (VALUE_LVAL (value) != lval_internalvar
740       && VALUE_LVAL (value) != lval_internalvar_component
741       && VALUE_LVAL (value) != lval_computed)
742     set_value_address (value, address + embedded_offset);
743
744   val_obj = value_to_value_object (value);
745   if (! val_obj)
746     {
747       result = EXT_LANG_RC_ERROR;
748       goto done;
749     }
750
751   /* Find the constructor.  */
752   printer = find_pretty_printer (val_obj);
753   Py_DECREF (val_obj);
754
755   if (printer == NULL)
756     {
757       result = EXT_LANG_RC_ERROR;
758       goto done;
759     }
760
761   make_cleanup_py_decref (printer);
762   if (printer == Py_None)
763     {
764       result = EXT_LANG_RC_NOP;
765       goto done;
766     }
767
768   /* If we are printing a map, we want some special formatting.  */
769   hint = gdbpy_get_display_hint (printer);
770   make_cleanup (free_current_contents, &hint);
771
772   /* Print the section */
773   print_result = print_string_repr (printer, hint, stream, recurse,
774                                     options, language, gdbarch);
775   if (print_result != string_repr_error)
776     print_children (printer, hint, stream, recurse, options, language,
777                     print_result == string_repr_none);
778
779   result = EXT_LANG_RC_OK;
780
781  done:
782   if (PyErr_Occurred ())
783     print_stack_unless_memory_error (stream);
784   do_cleanups (cleanups);
785   return result;
786 }
787
788
789 /* Apply a pretty-printer for the varobj code.  PRINTER_OBJ is the
790    print object.  It must have a 'to_string' method (but this is
791    checked by varobj, not here) which takes no arguments and
792    returns a string.  The printer will return a value and in the case
793    of a Python string being returned, this function will return a
794    PyObject containing the string.  For any other type, *REPLACEMENT is
795    set to the replacement value and this function returns NULL.  On
796    error, *REPLACEMENT is set to NULL and this function also returns
797    NULL.  */
798 PyObject *
799 apply_varobj_pretty_printer (PyObject *printer_obj,
800                              struct value **replacement,
801                              struct ui_file *stream)
802 {
803   PyObject *py_str = NULL;
804
805   *replacement = NULL;
806   py_str = pretty_print_one_value (printer_obj, replacement);
807
808   if (*replacement == NULL && py_str == NULL)
809     print_stack_unless_memory_error (stream);
810
811   return py_str;
812 }
813
814 /* Find a pretty-printer object for the varobj module.  Returns a new
815    reference to the object if successful; returns NULL if not.  VALUE
816    is the value for which a printer tests to determine if it
817    can pretty-print the value.  */
818 PyObject *
819 gdbpy_get_varobj_pretty_printer (struct value *value)
820 {
821   PyObject *val_obj;
822   PyObject *pretty_printer = NULL;
823
824   TRY
825     {
826       value = value_copy (value);
827     }
828   CATCH (except, RETURN_MASK_ALL)
829     {
830       GDB_PY_HANDLE_EXCEPTION (except);
831     }
832   END_CATCH
833
834   val_obj = value_to_value_object (value);
835   if (! val_obj)
836     return NULL;
837
838   pretty_printer = find_pretty_printer (val_obj);
839   Py_DECREF (val_obj);
840   return pretty_printer;
841 }
842
843 /* A Python function which wraps find_pretty_printer and instantiates
844    the resulting class.  This accepts a Value argument and returns a
845    pretty printer instance, or None.  This function is useful as an
846    argument to the MI command -var-set-visualizer.  */
847 PyObject *
848 gdbpy_default_visualizer (PyObject *self, PyObject *args)
849 {
850   PyObject *val_obj;
851   PyObject *cons;
852   struct value *value;
853
854   if (! PyArg_ParseTuple (args, "O", &val_obj))
855     return NULL;
856   value = value_object_to_value (val_obj);
857   if (! value)
858     {
859       PyErr_SetString (PyExc_TypeError,
860                        _("Argument must be a gdb.Value."));
861       return NULL;
862     }
863
864   cons = find_pretty_printer (val_obj);
865   return cons;
866 }
This page took 0.079647 seconds and 4 git commands to generate.