39 PyObject *unicode_str;
43 if (PyUnicode_Check (obj))
50 PyErr_SetString (PyExc_TypeError,
51 _(
"Expected a string object."));
62static gdb::unique_xmalloc_ptr<char>
66 gdbpy_ref<> string (PyUnicode_AsEncodedString (unicode_str, charset, NULL));
70 return gdb::unique_xmalloc_ptr<char>
71 (xstrdup (PyBytes_AsString (
string.
get ())));
82 return gdbpy_ref<> (PyUnicode_AsEncodedString (unicode_str, charset, NULL));
89gdb::unique_xmalloc_ptr<char>
112gdb::unique_xmalloc_ptr<char>
140gdb::unique_xmalloc_ptr<char>
165 return PyUnicode_Check (obj);
171gdb::unique_xmalloc_ptr<char>
184gdb::unique_xmalloc_ptr<char>
205gdb::unique_xmalloc_ptr<char>
220 if (exception.reason == RETURN_QUIT)
221 exc_class = PyExc_KeyboardInterrupt;
222 else if (exception.error == MEMORY_ERROR)
227 PyErr_Format (exc_class,
"%s", exception.what ());
245 catch (
const gdb_exception &except)
259 if (PyErr_Occurred ())
262 if (
sizeof (val) >
sizeof (CORE_ADDR) && ((CORE_ADDR) val) != val)
264 PyErr_SetString (PyExc_ValueError,
265 _(
"Overflow converting to address."));
281 if (
sizeof (l) >
sizeof (
long))
292 if (
sizeof (l) >
sizeof (
unsigned long))
293 return gdbpy_ref<> (PyLong_FromUnsignedLongLong (l));
303 *result = PyLong_AsLong (obj);
304 return ! (*result == -1 && PyErr_Occurred ());
317 PyTypeObject *type_obj = (PyTypeObject *) closure;
320 raw_ptr = (
char *) self + type_obj->tp_dictoffset;
321 result = * (PyObject **) raw_ptr;
336 result = PyModule_AddObject (module,
name,
object);
349 std::string str = string_vprintf (fmt, ap);
352 const char *msg = str.c_str ();
353 if (msg !=
nullptr && *msg !=
'\0')
354 error (_(
"Error occurred in Python: %s"), msg);
356 error (_(
"Error occurred in Python."));
369 gdb::unique_xmalloc_ptr<char> msg = fetched_error.
to_string ();
376 "and then another occurred computing the "
377 "error message.\n"));
388 if (fetched_error.
type_matches (PyExc_KeyboardInterrupt))
391 || msg == NULL || *msg ==
'\0')
395 if (msg != NULL && *msg !=
'\0')
396 error (_(
"Error occurred in Python: %s"), msg.get ());
398 error (_(
"Error occurred in Python."));
401 error (
"%s", msg.get ());
406gdb::unique_xmalloc_ptr<char>
411 struct line_whitespace
415 line_whitespace (
size_t offset,
int ws_count)
417 m_ws_count (ws_count)
421 size_t offset ()
const
426 {
return m_ws_count; }
441 auto count_whitespace = [] (
const char *txt) ->
int
469 gdb::optional<int> min_whitespace;
474 gdb::optional<size_t> all_done_idx;
477 std::vector<line_whitespace> ws_info;
480 const char *tmp = doc.get ();
485 size_t offset = tmp - doc.get ();
486 int ws_count = count_whitespace (tmp);
487 ws_info.emplace_back (offset, ws_count);
495 const char *content_start = tmp;
496 while (*tmp !=
'\0' && *tmp !=
'\n')
503 if (offset > 0 && tmp > content_start)
505 if (!min_whitespace.has_value ())
506 min_whitespace = ws_count;
508 min_whitespace = std::min (*min_whitespace, ws_count);
517 if (tmp > content_start)
518 all_done_idx = ws_info.size ();
529 if (!all_done_idx.has_value () || !min_whitespace.has_value ())
535 char *dst = doc.get ();
536 char *src = doc.get ();
539 size_t dst_offset = 0;
540 size_t src_offset = 0;
541 size_t ws_info_offset = 0;
544 while (src[src_offset] !=
'\0')
548 if (src_offset == ws_info[ws_info_offset].offset ())
552 if (ws_info[ws_info_offset].ws () > 0)
558 if (src[src_offset + ws_info[ws_info_offset].ws ()] ==
'\n'
559 || src[src_offset + ws_info[ws_info_offset].ws ()] ==
'\0')
560 src_offset += ws_info[ws_info_offset].ws ();
562 src_offset += std::min (*min_whitespace,
563 ws_info[ws_info_offset].ws ());
567 if (src[src_offset] ==
'\0')
570 if (ws_info_offset < (ws_info.size () - 1))
572 if (ws_info_offset > *all_done_idx)
579 if ((dst_offset > 0 || src[src_offset] !=
'\n'))
581 dst[dst_offset] = src[src_offset];
591 while (dst_offset > 1 && dst[dst_offset - 1] ==
'\n')
593 dst[dst_offset] =
'\0';
constexpr string_view get()
const char * host_charset(void)
const char * target_charset(struct gdbarch *gdbarch)
static struct gdbarch * get_gdbarch()
bool type_matches(PyObject *type) const
gdb::unique_xmalloc_ptr< char > type_to_string() const
gdb::unique_xmalloc_ptr< char > to_string() const
gdb::ref_ptr< T, gdbpy_ref_policy< T > > gdbpy_ref
gdbpy_ref host_string_to_python_string(const char *str)
gdb::unique_xmalloc_ptr< char > gdbpy_fix_doc_string_indentation(gdb::unique_xmalloc_ptr< char > doc)
gdbpy_ref python_string_to_unicode(PyObject *obj)
gdb::unique_xmalloc_ptr< char > python_string_to_target_string(PyObject *obj)
gdbpy_ref gdb_py_object_from_longest(LONGEST l)
int get_addr_from_python(PyObject *obj, CORE_ADDR *addr)
void gdbpy_convert_exception(const struct gdb_exception &exception)
int gdb_py_int_as_long(PyObject *obj, long *result)
void gdbpy_error(const char *fmt,...)
gdb::unique_xmalloc_ptr< char > gdbpy_obj_to_string(PyObject *obj)
int gdb_pymodule_addobject(PyObject *module, const char *name, PyObject *object)
gdb::unique_xmalloc_ptr< char > python_string_to_host_string(PyObject *obj)
void gdbpy_handle_exception()
gdbpy_ref python_string_to_target_python_string(PyObject *obj)
PyObject * gdb_py_generic_dict(PyObject *self, void *closure)
static gdb::unique_xmalloc_ptr< char > unicode_to_encoded_string(PyObject *unicode_str, const char *charset)
int gdbpy_is_string(PyObject *obj)
static gdbpy_ref unicode_to_encoded_python_string(PyObject *unicode_str, const char *charset)
gdbpy_ref gdb_py_object_from_ulongest(ULONGEST l)
static gdbpy_ref unicode_to_target_python_string(PyObject *unicode_str)
gdb::unique_xmalloc_ptr< char > unicode_to_target_string(PyObject *unicode_str)
int gdbpy_is_value_object(PyObject *obj)
struct value * value_object_to_value(PyObject *self)
void gdbpy_print_stack(void)
gdb::unique_xmalloc_ptr< char > gdbpy_obj_to_string(PyObject *obj)
PyObject * gdbpy_gdb_memory_error
#define GDB_PY_SET_HANDLE_EXCEPTION(Exception)
unsigned long gdb_py_ulongest
PyObject * gdbpy_gdb_error
PyObject * gdbpy_gdberror_exc
#define gdb_py_long_as_ulongest
void gdb_printf(struct ui_file *stream, const char *format,...)
CORE_ADDR value_as_address(struct value *val)