1
1
/* Python interface to values.
3
Copyright (C) 2008, 2009 Free Software Foundation, Inc.
3
Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
5
5
This file is part of GDB.
220
220
return obj->type;
223
/* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
224
string. Return a PyObject representing a lazy_string_object type.
225
A lazy string is a pointer to a string with an optional encoding and
226
length. If ENCODING is not given, encoding is set to None. If an
227
ENCODING is provided the encoding parameter is set to ENCODING, but
228
the string is not encoded. If LENGTH is provided then the length
229
parameter is set to LENGTH, otherwise length will be set to -1 (first
230
null of appropriate with). */
232
valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw)
235
struct value *value = ((value_object *) self)->value;
236
const char *user_encoding = NULL;
237
static char *keywords[] = { "encoding", "length", NULL };
240
if (!PyArg_ParseTupleAndKeywords (args, kw, "|si", keywords,
241
&user_encoding, &length))
244
if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR)
245
value = value_ind (value);
247
str_obj = gdbpy_create_lazy_string_object (value_address (value), length,
248
user_encoding, value_type (value));
250
return (PyObject *) str_obj;
223
253
/* Implementation of gdb.Value.string ([encoding] [, errors]
224
254
[, length]) -> string. Return Unicode string with value contents.
225
255
If ENCODING is not given, the string is assumed to be encoded in
238
268
const char *errors = NULL;
239
269
const char *user_encoding = NULL;
240
270
const char *la_encoding = NULL;
271
struct type *char_type;
241
272
static char *keywords[] = { "encoding", "errors", "length", NULL };
243
274
if (!PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords,
247
278
TRY_CATCH (except, RETURN_MASK_ALL)
249
LA_GET_STRING (value, &buffer, &length, &la_encoding);
280
LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding);
251
282
GDB_PY_HANDLE_EXCEPTION (except);
253
284
encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding;
254
unicode = PyUnicode_Decode (buffer, length, encoding, errors);
285
unicode = PyUnicode_Decode (buffer, length * TYPE_LENGTH (char_type),
938
970
else if (PyObject_TypeCheck (obj, &value_object_type))
939
971
value = value_copy (((value_object *) obj)->value);
972
else if (gdbpy_is_lazy_string (obj))
975
PyObject *function = PyString_FromString ("value");
976
result = PyObject_CallMethodObjArgs (obj, function, NULL);
977
value = value_copy (((value_object *) result)->value);
941
980
PyErr_Format (PyExc_TypeError, _("Could not convert Python object: %s"),
942
981
PyString_AsString (PyObject_Str (obj)));
999
1038
static PyMethodDef value_object_methods[] = {
1000
1039
{ "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." },
1001
1040
{ "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
1041
{ "lazy_string", (PyCFunction) valpy_lazy_string, METH_VARARGS | METH_KEYWORDS,
1042
"lazy_string ([encoding] [, length]) -> lazy_string\n\
1043
Return a lazy string representation of the value." },
1002
1044
{ "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS,
1003
1045
"string ([encoding] [, errors] [, length]) -> string\n\
1004
1046
Return Unicode string representation of the value." },