/* Apply a pretty-printer for the varobj code. PRINTER_OBJ is the print object. It must have a 'to_string' method (but this is checked by varobj, not here) which takes no arguments and returns a string. The printer will return a value and in the case of a Python string being returned, this function will return a PyObject containing the string. For any other type, *REPLACEMENT is set to the replacement value and this function returns NULL. On error, *REPLACEMENT is set to NULL and this function also returns NULL. */ PyObject * apply_varobj_pretty_printer (PyObject *printer_obj, struct value **replacement, struct ui_file *stream) { PyObject *py_str = NULL; *replacement = NULL; py_str = pretty_print_one_value (printer_obj, replacement); if (*replacement == NULL && py_str == NULL) print_stack_unless_memory_error (stream); return py_str; }
static enum string_repr_result print_string_repr (PyObject *printer, const char *hint, struct ui_file *stream, int recurse, const struct value_print_options *options, const struct language_defn *language, struct gdbarch *gdbarch) { struct value *replacement = NULL; enum string_repr_result result = string_repr_ok; gdbpy_ref<> py_str (pretty_print_one_value (printer, &replacement)); if (py_str != NULL) { if (py_str == Py_None) result = string_repr_none; else if (gdbpy_is_lazy_string (py_str.get ())) { CORE_ADDR addr; long length; struct type *type; gdb::unique_xmalloc_ptr<char> encoding; struct value_print_options local_opts = *options; gdbpy_extract_lazy_string (py_str.get (), &addr, &type, &length, &encoding); local_opts.addressprint = 0; val_print_string (type, encoding.get (), addr, (int) length, stream, &local_opts); } else { gdbpy_ref<> string (python_string_to_target_python_string (py_str.get ())); if (string != NULL) { char *output; long length; struct type *type; #ifdef IS_PY3K output = PyBytes_AS_STRING (string.get ()); length = PyBytes_GET_SIZE (string.get ()); #else output = PyString_AsString (string.get ()); length = PyString_Size (string.get ()); #endif type = builtin_type (gdbarch)->builtin_char; if (hint && !strcmp (hint, "string")) LA_PRINT_STRING (stream, type, (gdb_byte *) output, length, NULL, 0, options); else fputs_filtered (output, stream); } else { result = string_repr_error; print_stack_unless_memory_error (stream); } } } else if (replacement) { struct value_print_options opts = *options; opts.addressprint = 0; common_val_print (replacement, stream, recurse, &opts, language); } else { result = string_repr_error; print_stack_unless_memory_error (stream); } return result; }
static enum string_repr_result print_string_repr (PyObject *printer, const char *hint, struct ui_file *stream, int recurse, const struct value_print_options *options, const struct language_defn *language, struct gdbarch *gdbarch) { struct value *replacement = NULL; PyObject *py_str = NULL; enum string_repr_result result = string_repr_ok; py_str = pretty_print_one_value (printer, &replacement); if (py_str) { struct cleanup *cleanup = make_cleanup_py_decref (py_str); if (py_str == Py_None) result = string_repr_none; else if (gdbpy_is_lazy_string (py_str)) { CORE_ADDR addr; long length; struct type *type; char *encoding = NULL; struct value_print_options local_opts = *options; make_cleanup (free_current_contents, &encoding); gdbpy_extract_lazy_string (py_str, &addr, &type, &length, &encoding); local_opts.addressprint = 0; val_print_string (type, encoding, addr, (int) length, stream, &local_opts); } else { PyObject *string; string = python_string_to_target_python_string (py_str); if (string) { char *output; long length; struct type *type; make_cleanup_py_decref (string); #ifdef IS_PY3K output = PyBytes_AS_STRING (string); length = PyBytes_GET_SIZE (string); #else output = PyString_AsString (string); length = PyString_Size (string); #endif type = builtin_type (gdbarch)->builtin_char; if (hint && !strcmp (hint, "string")) LA_PRINT_STRING (stream, type, (gdb_byte *) output, length, NULL, 0, options); else fputs_filtered (output, stream); } else { result = string_repr_error; print_stack_unless_memory_error (stream); } } do_cleanups (cleanup); } else if (replacement) { struct value_print_options opts = *options; opts.addressprint = 0; common_val_print (replacement, stream, recurse, &opts, language); } else { result = string_repr_error; print_stack_unless_memory_error (stream); } return result; }