diff options
Diffstat (limited to 'gdb/python/python-utils.c')
-rw-r--r-- | gdb/python/python-utils.c | 54 |
1 files changed, 45 insertions, 9 deletions
diff --git a/gdb/python/python-utils.c b/gdb/python/python-utils.c index 9aae43f..6a95939 100644 --- a/gdb/python/python-utils.c +++ b/gdb/python/python-utils.c @@ -95,26 +95,37 @@ python_string_to_unicode (PyObject *obj) } /* Returns a newly allocated string with the contents of the given unicode - string object converted to the target's charset. If an error occurs during - the conversion, NULL will be returned and a python exception will be set. + string object converted to CHARSET. If an error occurs during the + conversion, NULL will be returned and a python exception will be set. The caller is responsible for xfree'ing the string. */ -char * -unicode_to_target_string (PyObject *unicode_str) +static char * +unicode_to_encoded_string (PyObject *unicode_str, const char *charset) { - char *target_string; + char *result; PyObject *string; - /* Translate string to target's charset. */ - string = PyUnicode_AsEncodedString (unicode_str, target_charset (), NULL); + /* Translate string to named charset. */ + string = PyUnicode_AsEncodedString (unicode_str, charset, NULL); if (string == NULL) return NULL; - target_string = xstrdup (PyString_AsString (string)); + result = xstrdup (PyString_AsString (string)); Py_DECREF (string); - return target_string; + return result; +} + +/* Returns a newly allocated string with the contents of the given unicode + string object converted to the target's charset. If an error occurs during + the conversion, NULL will be returned and a python exception will be set. + + The caller is responsible for xfree'ing the string. */ +char * +unicode_to_target_string (PyObject *unicode_str) +{ + return unicode_to_encoded_string (unicode_str, target_charset ()); } /* Converts a python string (8-bit or unicode) to a target string in @@ -132,3 +143,28 @@ python_string_to_target_string (PyObject *obj) return unicode_to_target_string (str); } + +/* Converts a python string (8-bit or unicode) to a target string in + the host's charset. Returns NULL on error, with a python exception set. + + The caller is responsible for xfree'ing the string. */ +char * +python_string_to_host_string (PyObject *obj) +{ + PyObject *str; + + str = python_string_to_unicode (obj); + if (str == NULL) + return NULL; + + return unicode_to_encoded_string (str, host_charset ()); +} + +/* Return true if OBJ is a Python string or unicode object, false + otherwise. */ + +int +gdbpy_is_string (PyObject *obj) +{ + return PyString_Check (obj) || PyUnicode_Check (obj); +} |