Imported Upstream version 7.9
[platform/upstream/gdb.git] / gdb / python / py-prettyprint.c
1 /* Python pretty-printing
2
3    Copyright (C) 2008-2015 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   volatile struct gdb_exception except;
217   PyObject *result = NULL;
218
219   *out_value = NULL;
220   TRY_CATCH (except, RETURN_MASK_ALL)
221     {
222       result = PyObject_CallMethodObjArgs (printer, gdbpy_to_string_cst, NULL);
223       if (result)
224         {
225           if (! gdbpy_is_string (result) && ! gdbpy_is_lazy_string (result)
226               && result != Py_None)
227             {
228               *out_value = convert_value_from_python (result);
229               if (PyErr_Occurred ())
230                 *out_value = NULL;
231               Py_DECREF (result);
232               result = NULL;
233             }
234         }
235     }
236
237   return result;
238 }
239
240 /* Return the display hint for the object printer, PRINTER.  Return
241    NULL if there is no display_hint method, or if the method did not
242    return a string.  On error, print stack trace and return NULL.  On
243    success, return an xmalloc()d string.  */
244 char *
245 gdbpy_get_display_hint (PyObject *printer)
246 {
247   PyObject *hint;
248   char *result = NULL;
249
250   if (! PyObject_HasAttr (printer, gdbpy_display_hint_cst))
251     return NULL;
252
253   hint = PyObject_CallMethodObjArgs (printer, gdbpy_display_hint_cst, NULL);
254   if (hint)
255     {
256       if (gdbpy_is_string (hint))
257         {
258           result = python_string_to_host_string (hint);
259           if (result == NULL)
260             gdbpy_print_stack ();
261         }
262       Py_DECREF (hint);
263     }
264   else
265     gdbpy_print_stack ();
266
267   return result;
268 }
269
270 /* A wrapper for gdbpy_print_stack that ignores MemoryError.  */
271
272 static void
273 print_stack_unless_memory_error (struct ui_file *stream)
274 {
275   if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
276     {
277       struct cleanup *cleanup;
278       PyObject *type, *value, *trace;
279       char *msg;
280
281       PyErr_Fetch (&type, &value, &trace);
282       cleanup = make_cleanup_py_decref (type);
283       make_cleanup_py_decref (value);
284       make_cleanup_py_decref (trace);
285
286       msg = gdbpy_exception_to_string (type, value);
287       make_cleanup (xfree, msg);
288
289       if (msg == NULL || *msg == '\0')
290         fprintf_filtered (stream, _("<error reading variable>"));
291       else
292         fprintf_filtered (stream, _("<error reading variable: %s>"), msg);
293
294       do_cleanups (cleanup);
295     }
296   else
297     gdbpy_print_stack ();
298 }
299
300 /* Helper for gdbpy_apply_val_pretty_printer which calls to_string and
301    formats the result.  */
302
303 static enum string_repr_result
304 print_string_repr (PyObject *printer, const char *hint,
305                    struct ui_file *stream, int recurse,
306                    const struct value_print_options *options,
307                    const struct language_defn *language,
308                    struct gdbarch *gdbarch)
309 {
310   struct value *replacement = NULL;
311   PyObject *py_str = NULL;
312   enum string_repr_result result = string_repr_ok;
313
314   py_str = pretty_print_one_value (printer, &replacement);
315   if (py_str)
316     {
317       struct cleanup *cleanup = make_cleanup_py_decref (py_str);
318
319       if (py_str == Py_None)
320         result = string_repr_none;
321       else if (gdbpy_is_lazy_string (py_str))
322         {
323           CORE_ADDR addr;
324           long length;
325           struct type *type;
326           char *encoding = NULL;
327           struct value_print_options local_opts = *options;
328
329           make_cleanup (free_current_contents, &encoding);
330           gdbpy_extract_lazy_string (py_str, &addr, &type,
331                                      &length, &encoding);
332
333           local_opts.addressprint = 0;
334           val_print_string (type, encoding, addr, (int) length,
335                             stream, &local_opts);
336         }
337       else
338         {
339           PyObject *string;
340
341           string = python_string_to_target_python_string (py_str);
342           if (string)
343             {
344               char *output;
345               long length;
346               struct type *type;
347
348               make_cleanup_py_decref (string);
349 #ifdef IS_PY3K
350               output = PyBytes_AS_STRING (string);
351               length = PyBytes_GET_SIZE (string);
352 #else
353               output = PyString_AsString (string);
354               length = PyString_Size (string);
355 #endif
356               type = builtin_type (gdbarch)->builtin_char;
357
358               if (hint && !strcmp (hint, "string"))
359                 LA_PRINT_STRING (stream, type, (gdb_byte *) output,
360                                  length, NULL, 0, options);
361               else
362                 fputs_filtered (output, stream);
363             }
364           else
365             {
366               result = string_repr_error;
367               print_stack_unless_memory_error (stream);
368             }
369         }
370
371       do_cleanups (cleanup);
372     }
373   else if (replacement)
374     {
375       struct value_print_options opts = *options;
376
377       opts.addressprint = 0;
378       common_val_print (replacement, stream, recurse, &opts, language);
379     }
380   else
381     {
382       result = string_repr_error;
383       print_stack_unless_memory_error (stream);
384     }
385
386   return result;
387 }
388
389 #ifndef IS_PY3K
390 static void
391 py_restore_tstate (void *p)
392 {
393   PyFrameObject *frame = p;
394   PyThreadState *tstate = PyThreadState_GET ();
395
396   tstate->frame = frame;
397 }
398
399 /* Create a dummy PyFrameObject, needed to work around
400    a Python-2.4 bug with generators.  */
401 static PyObject *
402 push_dummy_python_frame (void)
403 {
404   PyObject *empty_string, *null_tuple, *globals;
405   PyCodeObject *code;
406   PyFrameObject *frame;
407   PyThreadState *tstate;
408
409   empty_string = PyString_FromString ("");
410   if (!empty_string)
411     return NULL;
412
413   null_tuple = PyTuple_New (0);
414   if (!null_tuple)
415     {
416       Py_DECREF (empty_string);
417       return NULL;
418     }
419
420   code = PyCode_New (0,                 /* argcount */
421                      0,                 /* nlocals */
422                      0,                 /* stacksize */
423                      0,                 /* flags */
424                      empty_string,      /* code */
425                      null_tuple,        /* consts */
426                      null_tuple,        /* names */
427                      null_tuple,        /* varnames */
428 #if PYTHON_API_VERSION >= 1010
429                      null_tuple,        /* freevars */
430                      null_tuple,        /* cellvars */
431 #endif
432                      empty_string,      /* filename */
433                      empty_string,      /* name */
434                      1,                 /* firstlineno */
435                      empty_string       /* lnotab */
436                     );
437
438   Py_DECREF (empty_string);
439   Py_DECREF (null_tuple);
440
441   if (!code)
442     return NULL;
443
444   globals = PyDict_New ();
445   if (!globals)
446     {
447       Py_DECREF (code);
448       return NULL;
449     }
450
451   tstate = PyThreadState_GET ();
452
453   frame = PyFrame_New (tstate, code, globals, NULL);
454
455   Py_DECREF (globals);
456   Py_DECREF (code);
457
458   if (!frame)
459     return NULL;
460
461   tstate->frame = frame;
462   make_cleanup (py_restore_tstate, frame->f_back);
463   return (PyObject *) frame;
464 }
465 #endif
466
467 /* Helper for gdbpy_apply_val_pretty_printer that formats children of the
468    printer, if any exist.  If is_py_none is true, then nothing has
469    been printed by to_string, and format output accordingly. */
470 static void
471 print_children (PyObject *printer, const char *hint,
472                 struct ui_file *stream, int recurse,
473                 const struct value_print_options *options,
474                 const struct language_defn *language,
475                 int is_py_none)
476 {
477   int is_map, is_array, done_flag, pretty;
478   unsigned int i;
479   PyObject *children, *iter;
480 #ifndef IS_PY3K
481   PyObject *frame;
482 #endif
483   struct cleanup *cleanups;
484
485   if (! PyObject_HasAttr (printer, gdbpy_children_cst))
486     return;
487
488   /* If we are printing a map or an array, we want some special
489      formatting.  */
490   is_map = hint && ! strcmp (hint, "map");
491   is_array = hint && ! strcmp (hint, "array");
492
493   children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
494                                          NULL);
495   if (! children)
496     {
497       print_stack_unless_memory_error (stream);
498       return;
499     }
500
501   cleanups = make_cleanup_py_decref (children);
502
503   iter = PyObject_GetIter (children);
504   if (!iter)
505     {
506       print_stack_unless_memory_error (stream);
507       goto done;
508     }
509   make_cleanup_py_decref (iter);
510
511   /* Use the prettyformat_arrays option if we are printing an array,
512      and the pretty option otherwise.  */
513   if (is_array)
514     pretty = options->prettyformat_arrays;
515   else
516     {
517       if (options->prettyformat == Val_prettyformat)
518         pretty = 1;
519       else
520         pretty = options->prettyformat_structs;
521     }
522
523   /* Manufacture a dummy Python frame to work around Python 2.4 bug,
524      where it insists on having a non-NULL tstate->frame when
525      a generator is called.  */
526 #ifndef IS_PY3K
527   frame = push_dummy_python_frame ();
528   if (!frame)
529     {
530       gdbpy_print_stack ();
531       goto done;
532     }
533   make_cleanup_py_decref (frame);
534 #endif
535
536   done_flag = 0;
537   for (i = 0; i < options->print_max; ++i)
538     {
539       PyObject *py_v, *item = PyIter_Next (iter);
540       const char *name;
541       struct cleanup *inner_cleanup;
542
543       if (! item)
544         {
545           if (PyErr_Occurred ())
546             print_stack_unless_memory_error (stream);
547           /* Set a flag so we can know whether we printed all the
548              available elements.  */
549           else  
550             done_flag = 1;
551           break;
552         }
553
554       if (! PyArg_ParseTuple (item, "sO", &name, &py_v))
555         {
556           gdbpy_print_stack ();
557           Py_DECREF (item);
558           continue;
559         }
560       inner_cleanup = make_cleanup_py_decref (item);
561
562       /* Print initial "{".  For other elements, there are three
563          cases:
564          1. Maps.  Print a "," after each value element.
565          2. Arrays.  Always print a ",".
566          3. Other.  Always print a ",".  */
567       if (i == 0)
568         {
569          if (is_py_none)
570            fputs_filtered ("{", stream);
571          else
572            fputs_filtered (" = {", stream);
573        }
574
575       else if (! is_map || i % 2 == 0)
576         fputs_filtered (pretty ? "," : ", ", stream);
577
578       /* In summary mode, we just want to print "= {...}" if there is
579          a value.  */
580       if (options->summary)
581         {
582           /* This increment tricks the post-loop logic to print what
583              we want.  */
584           ++i;
585           /* Likewise.  */
586           pretty = 0;
587           break;
588         }
589
590       if (! is_map || i % 2 == 0)
591         {
592           if (pretty)
593             {
594               fputs_filtered ("\n", stream);
595               print_spaces_filtered (2 + 2 * recurse, stream);
596             }
597           else
598             wrap_here (n_spaces (2 + 2 *recurse));
599         }
600
601       if (is_map && i % 2 == 0)
602         fputs_filtered ("[", stream);
603       else if (is_array)
604         {
605           /* We print the index, not whatever the child method
606              returned as the name.  */
607           if (options->print_array_indexes)
608             fprintf_filtered (stream, "[%d] = ", i);
609         }
610       else if (! is_map)
611         {
612           fputs_filtered (name, stream);
613           fputs_filtered (" = ", stream);
614         }
615
616       if (gdbpy_is_lazy_string (py_v))
617         {
618           CORE_ADDR addr;
619           struct type *type;
620           long length;
621           char *encoding = NULL;
622           struct value_print_options local_opts = *options;
623
624           make_cleanup (free_current_contents, &encoding);
625           gdbpy_extract_lazy_string (py_v, &addr, &type, &length, &encoding);
626
627           local_opts.addressprint = 0;
628           val_print_string (type, encoding, addr, (int) length, stream,
629                             &local_opts);
630         }
631       else if (gdbpy_is_string (py_v))
632         {
633           char *output;
634
635           output = python_string_to_host_string (py_v);
636           if (!output)
637             gdbpy_print_stack ();
638           else
639             {
640               fputs_filtered (output, stream);
641               xfree (output);
642             }
643         }
644       else
645         {
646           struct value *value = convert_value_from_python (py_v);
647
648           if (value == NULL)
649             {
650               gdbpy_print_stack ();
651               error (_("Error while executing Python code."));
652             }
653           else
654             common_val_print (value, stream, recurse + 1, options, language);
655         }
656
657       if (is_map && i % 2 == 0)
658         fputs_filtered ("] = ", stream);
659
660       do_cleanups (inner_cleanup);
661     }
662
663   if (i)
664     {
665       if (!done_flag)
666         {
667           if (pretty)
668             {
669               fputs_filtered ("\n", stream);
670               print_spaces_filtered (2 + 2 * recurse, stream);
671             }
672           fputs_filtered ("...", stream);
673         }
674       if (pretty)
675         {
676           fputs_filtered ("\n", stream);
677           print_spaces_filtered (2 * recurse, stream);
678         }
679       fputs_filtered ("}", stream);
680     }
681
682  done:
683   do_cleanups (cleanups);
684 }
685
686 enum ext_lang_rc
687 gdbpy_apply_val_pretty_printer (const struct extension_language_defn *extlang,
688                                 struct type *type, const gdb_byte *valaddr,
689                                 int embedded_offset, CORE_ADDR address,
690                                 struct ui_file *stream, int recurse,
691                                 const struct value *val,
692                                 const struct value_print_options *options,
693                                 const struct language_defn *language)
694 {
695   struct gdbarch *gdbarch = get_type_arch (type);
696   PyObject *printer = NULL;
697   PyObject *val_obj = NULL;
698   struct value *value;
699   char *hint = NULL;
700   struct cleanup *cleanups;
701   enum ext_lang_rc result = EXT_LANG_RC_NOP;
702   enum string_repr_result print_result;
703
704   /* No pretty-printer support for unavailable values.  */
705   if (!value_bytes_available (val, embedded_offset, TYPE_LENGTH (type)))
706     return EXT_LANG_RC_NOP;
707
708   if (!gdb_python_initialized)
709     return EXT_LANG_RC_NOP;
710
711   cleanups = ensure_python_env (gdbarch, language);
712
713   /* Instantiate the printer.  */
714   if (valaddr)
715     valaddr += embedded_offset;
716   value = value_from_contents_and_address (type, valaddr,
717                                            address + embedded_offset);
718
719   set_value_component_location (value, val);
720   /* set_value_component_location resets the address, so we may
721      need to set it again.  */
722   if (VALUE_LVAL (value) != lval_internalvar
723       && VALUE_LVAL (value) != lval_internalvar_component
724       && VALUE_LVAL (value) != lval_computed)
725     set_value_address (value, address + embedded_offset);
726
727   val_obj = value_to_value_object (value);
728   if (! val_obj)
729     {
730       result = EXT_LANG_RC_ERROR;
731       goto done;
732     }
733
734   /* Find the constructor.  */
735   printer = find_pretty_printer (val_obj);
736   Py_DECREF (val_obj);
737
738   if (printer == NULL)
739     {
740       result = EXT_LANG_RC_ERROR;
741       goto done;
742     }
743
744   make_cleanup_py_decref (printer);
745   if (printer == Py_None)
746     {
747       result = EXT_LANG_RC_NOP;
748       goto done;
749     }
750
751   /* If we are printing a map, we want some special formatting.  */
752   hint = gdbpy_get_display_hint (printer);
753   make_cleanup (free_current_contents, &hint);
754
755   /* Print the section */
756   print_result = print_string_repr (printer, hint, stream, recurse,
757                                     options, language, gdbarch);
758   if (print_result != string_repr_error)
759     print_children (printer, hint, stream, recurse, options, language,
760                     print_result == string_repr_none);
761
762   result = EXT_LANG_RC_OK;
763
764  done:
765   if (PyErr_Occurred ())
766     print_stack_unless_memory_error (stream);
767   do_cleanups (cleanups);
768   return result;
769 }
770
771
772 /* Apply a pretty-printer for the varobj code.  PRINTER_OBJ is the
773    print object.  It must have a 'to_string' method (but this is
774    checked by varobj, not here) which takes no arguments and
775    returns a string.  The printer will return a value and in the case
776    of a Python string being returned, this function will return a
777    PyObject containing the string.  For any other type, *REPLACEMENT is
778    set to the replacement value and this function returns NULL.  On
779    error, *REPLACEMENT is set to NULL and this function also returns
780    NULL.  */
781 PyObject *
782 apply_varobj_pretty_printer (PyObject *printer_obj,
783                              struct value **replacement,
784                              struct ui_file *stream)
785 {
786   PyObject *py_str = NULL;
787
788   *replacement = NULL;
789   py_str = pretty_print_one_value (printer_obj, replacement);
790
791   if (*replacement == NULL && py_str == NULL)
792     print_stack_unless_memory_error (stream);
793
794   return py_str;
795 }
796
797 /* Find a pretty-printer object for the varobj module.  Returns a new
798    reference to the object if successful; returns NULL if not.  VALUE
799    is the value for which a printer tests to determine if it
800    can pretty-print the value.  */
801 PyObject *
802 gdbpy_get_varobj_pretty_printer (struct value *value)
803 {
804   PyObject *val_obj;
805   PyObject *pretty_printer = NULL;
806   volatile struct gdb_exception except;
807
808   TRY_CATCH (except, RETURN_MASK_ALL)
809     {
810       value = value_copy (value);
811     }
812   GDB_PY_HANDLE_EXCEPTION (except);
813
814   val_obj = value_to_value_object (value);
815   if (! val_obj)
816     return NULL;
817
818   pretty_printer = find_pretty_printer (val_obj);
819   Py_DECREF (val_obj);
820   return pretty_printer;
821 }
822
823 /* A Python function which wraps find_pretty_printer and instantiates
824    the resulting class.  This accepts a Value argument and returns a
825    pretty printer instance, or None.  This function is useful as an
826    argument to the MI command -var-set-visualizer.  */
827 PyObject *
828 gdbpy_default_visualizer (PyObject *self, PyObject *args)
829 {
830   PyObject *val_obj;
831   PyObject *cons;
832   struct value *value;
833
834   if (! PyArg_ParseTuple (args, "O", &val_obj))
835     return NULL;
836   value = value_object_to_value (val_obj);
837   if (! value)
838     {
839       PyErr_SetString (PyExc_TypeError,
840                        _("Argument must be a gdb.Value."));
841       return NULL;
842     }
843
844   cons = find_pretty_printer (val_obj);
845   return cons;
846 }