44 if (PyUnicode_Check (obj))
51 PyErr_SetString (PyExc_TypeError,
52 _(
"Expected a string object."));
63static gdb::unique_xmalloc_ptr<char>
67 gdbpy_ref<> string (PyUnicode_AsEncodedString (unicode_str, charset, NULL));
71 return gdb::unique_xmalloc_ptr<char>
72 (xstrdup (PyBytes_AsString (
string.
get ())));
83 return gdbpy_ref<> (PyUnicode_AsEncodedString (unicode_str, charset, NULL));
90gdb::unique_xmalloc_ptr<char>
113gdb::unique_xmalloc_ptr<char>
141gdb::unique_xmalloc_ptr<char>
166 return PyUnicode_Check (obj);
172gdb::unique_xmalloc_ptr<char>
185gdb::unique_xmalloc_ptr<char>
206gdb::unique_xmalloc_ptr<char>
221 if (exception.reason == RETURN_QUIT)
222 exc_class = PyExc_KeyboardInterrupt;
223 else if (exception.reason == RETURN_FORCED_QUIT)
225 else if (exception.error == MEMORY_ERROR)
230 PyErr_Format (exc_class,
"%s", exception.what ());
248 catch (
const gdb_exception &except)
262 if (PyErr_Occurred ())
265 if (
sizeof (val) >
sizeof (CORE_ADDR) && ((CORE_ADDR) val) != val)
267 PyErr_SetString (PyExc_ValueError,
268 _(
"Overflow converting to address."));
284 if (
sizeof (l) >
sizeof (
long))
295 if (
sizeof (l) >
sizeof (
unsigned long))
296 return gdbpy_ref<> (PyLong_FromUnsignedLongLong (l));
306 *result = PyLong_AsLong (obj);
307 return ! (*result == -1 && PyErr_Occurred ());
320 PyTypeObject *type_obj = (PyTypeObject *) closure;
323 raw_ptr = (
char *) self + type_obj->tp_dictoffset;
339 result = PyModule_AddObject (module,
name,
object);
352 std::string str = string_vprintf (fmt, ap);
355 const char *msg = str.c_str ();
356 if (msg !=
nullptr && *msg !=
'\0')
357 error (_(
"Error occurred in Python: %s"), msg);
359 error (_(
"Error occurred in Python."));
372 gdb::unique_xmalloc_ptr<char> msg = fetched_error.
to_string ();
379 "and then another occurred computing the "
380 "error message.\n"));
391 if (fetched_error.
type_matches (PyExc_KeyboardInterrupt))
394 || msg == NULL || *msg ==
'\0')
398 if (msg != NULL && *msg !=
'\0')
399 error (_(
"Error occurred in Python: %s"), msg.get ());
401 error (_(
"Error occurred in Python."));
404 error (
"%s", msg.get ());
409gdb::unique_xmalloc_ptr<char>
414 struct line_whitespace
418 line_whitespace (
size_t offset,
int ws_count)
420 m_ws_count (ws_count)
424 size_t offset ()
const
429 {
return m_ws_count; }
444 auto count_whitespace = [] (
const char *txt) ->
int
472 gdb::optional<int> min_whitespace;
477 gdb::optional<size_t> all_done_idx;
480 std::vector<line_whitespace> ws_info;
483 const char *tmp = doc.get ();
488 size_t offset = tmp - doc.get ();
489 int ws_count = count_whitespace (tmp);
490 ws_info.emplace_back (offset, ws_count);
498 const char *content_start = tmp;
499 while (*tmp !=
'\0' && *tmp !=
'\n')
506 if (offset > 0 && tmp > content_start)
508 if (!min_whitespace.has_value ())
509 min_whitespace = ws_count;
511 min_whitespace = std::min (*min_whitespace, ws_count);
520 if (tmp > content_start)
521 all_done_idx = ws_info.size ();
532 if (!all_done_idx.has_value () || !min_whitespace.has_value ())
538 char *dst = doc.get ();
539 char *src = doc.get ();
542 size_t dst_offset = 0;
543 size_t src_offset = 0;
544 size_t ws_info_offset = 0;
547 while (src[src_offset] !=
'\0')
551 if (src_offset == ws_info[ws_info_offset].offset ())
555 if (ws_info[ws_info_offset].ws () > 0)
561 if (src[src_offset + ws_info[ws_info_offset].ws ()] ==
'\n'
562 || src[src_offset + ws_info[ws_info_offset].ws ()] ==
'\0')
563 src_offset += ws_info[ws_info_offset].ws ();
565 src_offset += std::min (*min_whitespace,
566 ws_info[ws_info_offset].ws ());
570 if (src[src_offset] ==
'\0')
573 if (ws_info_offset < (ws_info.size () - 1))
575 if (ws_info_offset > *all_done_idx)
582 if ((dst_offset > 0 || src[src_offset] !=
'\n'))
584 dst[dst_offset] = src[src_offset];
594 while (dst_offset > 1 && dst[dst_offset - 1] ==
'\n')
596 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 quit_force(int *exit_arg, int from_tty)
void gdb_printf(struct ui_file *stream, const char *format,...)
CORE_ADDR value_as_address(struct value *val)