1 /* Python interface to values.
3 Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "gdb_assert.h"
24 #include "exceptions.h"
31 #include "python-internal.h"
33 /* Even though Python scalar types directly map to host types, we use
34 target types here to remain consistent with the the values system in
35 GDB (which uses target arithmetic). */
37 /* Python's integer type corresponds to C's long type. */
38 #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long
40 /* Python's float type corresponds to C's double type. */
41 #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double
43 /* Python's long type corresponds to C's long long type. */
44 #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long
46 /* Python's long type corresponds to C's long long type. Unsigned version. */
47 #define builtin_type_upylong builtin_type \
48 (python_gdbarch)->builtin_unsigned_long_long
50 #define builtin_type_pybool \
51 language_bool_type (python_language, python_gdbarch)
53 #define builtin_type_pychar \
54 language_string_char_type (python_language, python_gdbarch)
56 typedef struct value_object {
58 struct value_object *next;
59 struct value_object *prev;
65 /* List of all values which are currently exposed to Python. It is
66 maintained so that when an objfile is discarded, preserve_values
67 can copy the values' types if needed. */
68 /* This variable is unnecessarily initialized to NULL in order to
69 work around a linker bug on MacOS. */
70 static value_object *values_in_python = NULL;
72 /* Called by the Python interpreter when deallocating a value object. */
74 valpy_dealloc (PyObject *obj)
76 value_object *self = (value_object *) obj;
78 /* Remove SELF from the global list. */
80 self->prev->next = self->next;
83 gdb_assert (values_in_python == self);
84 values_in_python = self->next;
87 self->next->prev = self->prev;
89 value_free (self->value);
92 /* Use braces to appease gcc warning. *sigh* */
94 Py_DECREF (self->address);
99 Py_DECREF (self->type);
102 self->ob_type->tp_free (self);
105 /* Helper to push a Value object on the global list. */
107 note_value (value_object *value_obj)
109 value_obj->next = values_in_python;
111 value_obj->next->prev = value_obj;
112 value_obj->prev = NULL;
113 values_in_python = value_obj;
116 /* Called when a new gdb.Value object needs to be allocated. */
118 valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords)
120 struct value *value = NULL; /* Initialize to appease gcc warning. */
121 value_object *value_obj;
123 if (PyTuple_Size (args) != 1)
125 PyErr_SetString (PyExc_TypeError, _("Value object creation takes only "
130 value_obj = (value_object *) subtype->tp_alloc (subtype, 1);
131 if (value_obj == NULL)
133 PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory to "
134 "create Value object."));
138 value = convert_value_from_python (PyTuple_GetItem (args, 0));
141 subtype->tp_free (value_obj);
145 value_obj->value = value;
146 value_incref (value);
147 value_obj->address = NULL;
148 value_obj->type = NULL;
149 note_value (value_obj);
151 return (PyObject *) value_obj;
154 /* Iterate over all the Value objects, calling preserve_one_value on
157 preserve_python_values (struct objfile *objfile, htab_t copied_types)
161 for (iter = values_in_python; iter; iter = iter->next)
162 preserve_one_value (iter->value, objfile, copied_types);
165 /* Given a value of a pointer type, apply the C unary * operator to it. */
167 valpy_dereference (PyObject *self, PyObject *args)
169 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
170 volatile struct gdb_exception except;
172 TRY_CATCH (except, RETURN_MASK_ALL)
174 res_val = value_ind (((value_object *) self)->value);
176 GDB_PY_HANDLE_EXCEPTION (except);
178 return value_to_value_object (res_val);
181 /* Return "&value". */
183 valpy_get_address (PyObject *self, void *closure)
185 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
186 value_object *val_obj = (value_object *) self;
187 volatile struct gdb_exception except;
189 if (!val_obj->address)
191 TRY_CATCH (except, RETURN_MASK_ALL)
193 res_val = value_addr (val_obj->value);
195 if (except.reason < 0)
197 val_obj->address = Py_None;
201 val_obj->address = value_to_value_object (res_val);
204 Py_INCREF (val_obj->address);
206 return val_obj->address;
209 /* Return type of the value. */
211 valpy_get_type (PyObject *self, void *closure)
213 value_object *obj = (value_object *) self;
217 obj->type = type_to_type_object (value_type (obj->value));
221 Py_INCREF (obj->type);
224 Py_INCREF (obj->type);
228 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
229 string. Return a PyObject representing a lazy_string_object type.
230 A lazy string is a pointer to a string with an optional encoding and
231 length. If ENCODING is not given, encoding is set to None. If an
232 ENCODING is provided the encoding parameter is set to ENCODING, but
233 the string is not encoded. If LENGTH is provided then the length
234 parameter is set to LENGTH, otherwise length will be set to -1 (first
235 null of appropriate with). */
237 valpy_lazy_string (PyObject *self, PyObject *args, PyObject *kw)
240 struct value *value = ((value_object *) self)->value;
241 const char *user_encoding = NULL;
242 static char *keywords[] = { "encoding", "length", NULL };
245 if (!PyArg_ParseTupleAndKeywords (args, kw, "|si", keywords,
246 &user_encoding, &length))
249 if (TYPE_CODE (value_type (value)) == TYPE_CODE_PTR)
250 value = value_ind (value);
252 str_obj = gdbpy_create_lazy_string_object (value_address (value), length,
253 user_encoding, value_type (value));
255 return (PyObject *) str_obj;
258 /* Implementation of gdb.Value.string ([encoding] [, errors]
259 [, length]) -> string. Return Unicode string with value contents.
260 If ENCODING is not given, the string is assumed to be encoded in
261 the target's charset. If LENGTH is provided, only fetch string to
262 the length provided. */
265 valpy_string (PyObject *self, PyObject *args, PyObject *kw)
269 struct value *value = ((value_object *) self)->value;
270 volatile struct gdb_exception except;
272 const char *encoding = NULL;
273 const char *errors = NULL;
274 const char *user_encoding = NULL;
275 const char *la_encoding = NULL;
276 struct type *char_type;
277 static char *keywords[] = { "encoding", "errors", "length", NULL };
279 if (!PyArg_ParseTupleAndKeywords (args, kw, "|ssi", keywords,
280 &user_encoding, &errors, &length))
283 TRY_CATCH (except, RETURN_MASK_ALL)
285 LA_GET_STRING (value, &buffer, &length, &char_type, &la_encoding);
287 GDB_PY_HANDLE_EXCEPTION (except);
289 encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding;
290 unicode = PyUnicode_Decode (buffer, length * TYPE_LENGTH (char_type),
297 /* Cast a value to a given type. */
299 valpy_cast (PyObject *self, PyObject *args)
303 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
304 volatile struct gdb_exception except;
306 if (! PyArg_ParseTuple (args, "O", &type_obj))
309 type = type_object_to_type (type_obj);
312 PyErr_SetString (PyExc_RuntimeError,
313 _("Argument must be a type."));
317 TRY_CATCH (except, RETURN_MASK_ALL)
319 res_val = value_cast (type, ((value_object *) self)->value);
321 GDB_PY_HANDLE_EXCEPTION (except);
323 return value_to_value_object (res_val);
327 valpy_length (PyObject *self)
329 /* We don't support getting the number of elements in a struct / class. */
330 PyErr_SetString (PyExc_NotImplementedError,
331 _("Invalid operation on gdb.Value."));
335 /* Given string name of an element inside structure, return its value
338 valpy_getitem (PyObject *self, PyObject *key)
340 value_object *self_value = (value_object *) self;
342 struct value *res_val = NULL;
343 volatile struct gdb_exception except;
345 if (gdbpy_is_string (key))
347 field = python_string_to_host_string (key);
352 TRY_CATCH (except, RETURN_MASK_ALL)
354 struct value *tmp = self_value->value;
357 res_val = value_struct_elt (&tmp, NULL, field, 0, NULL);
360 /* Assume we are attempting an array access, and let the
361 value code throw an exception if the index has an invalid
363 struct value *idx = convert_value_from_python (key);
367 /* Check the value's type is something that can be accessed via
371 tmp = coerce_ref (tmp);
372 type = check_typedef (value_type (tmp));
373 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
374 && TYPE_CODE (type) != TYPE_CODE_PTR)
375 error( _("Cannot subscript requested type."));
377 res_val = value_subscript (tmp, value_as_long (idx));
383 GDB_PY_HANDLE_EXCEPTION (except);
385 return res_val ? value_to_value_object (res_val) : NULL;
389 valpy_setitem (PyObject *self, PyObject *key, PyObject *value)
391 PyErr_Format (PyExc_NotImplementedError,
392 _("Setting of struct elements is not currently supported."));
396 /* Called by the Python interpreter to obtain string representation
399 valpy_str (PyObject *self)
403 struct cleanup *old_chain;
405 struct value_print_options opts;
406 volatile struct gdb_exception except;
408 get_user_print_options (&opts);
411 stb = mem_fileopen ();
412 old_chain = make_cleanup_ui_file_delete (stb);
414 TRY_CATCH (except, RETURN_MASK_ALL)
416 common_val_print (((value_object *) self)->value, stb, 0,
417 &opts, python_language);
418 s = ui_file_xstrdup (stb, NULL);
420 GDB_PY_HANDLE_EXCEPTION (except);
422 do_cleanups (old_chain);
424 result = PyUnicode_Decode (s, strlen (s), host_charset (), NULL);
430 /* Implements gdb.Value.is_optimized_out. */
432 valpy_get_is_optimized_out (PyObject *self, void *closure)
434 struct value *value = ((value_object *) self)->value;
436 if (value_optimized_out (value))
442 /* Calculate and return the address of the PyObject as the value of
443 the builtin __hash__ call. */
445 valpy_hash (PyObject *self)
447 return (long) (intptr_t) self;
465 /* If TYPE is a reference, return the target; otherwise return TYPE. */
466 #define STRIP_REFERENCE(TYPE) \
467 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
469 /* Returns a value object which is the result of applying the operation
470 specified by OPCODE to the given arguments. */
472 valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
474 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
475 volatile struct gdb_exception except;
477 TRY_CATCH (except, RETURN_MASK_ALL)
479 struct value *arg1, *arg2;
481 /* If the gdb.Value object is the second operand, then it will be passed
482 to us as the OTHER argument, and SELF will be an entirely different
483 kind of object, altogether. Because of this, we can't assume self is
484 a gdb.Value object and need to convert it from python as well. */
485 arg1 = convert_value_from_python (self);
489 arg2 = convert_value_from_python (other);
497 struct type *ltype = value_type (arg1);
498 struct type *rtype = value_type (arg2);
500 CHECK_TYPEDEF (ltype);
501 ltype = STRIP_REFERENCE (ltype);
502 CHECK_TYPEDEF (rtype);
503 rtype = STRIP_REFERENCE (rtype);
505 if (TYPE_CODE (ltype) == TYPE_CODE_PTR
506 && is_integral_type (rtype))
507 res_val = value_ptradd (arg1, value_as_long (arg2));
508 else if (TYPE_CODE (rtype) == TYPE_CODE_PTR
509 && is_integral_type (ltype))
510 res_val = value_ptradd (arg2, value_as_long (arg1));
512 res_val = value_binop (arg1, arg2, BINOP_ADD);
517 struct type *ltype = value_type (arg1);
518 struct type *rtype = value_type (arg2);
520 CHECK_TYPEDEF (ltype);
521 ltype = STRIP_REFERENCE (ltype);
522 CHECK_TYPEDEF (rtype);
523 rtype = STRIP_REFERENCE (rtype);
525 if (TYPE_CODE (ltype) == TYPE_CODE_PTR
526 && TYPE_CODE (rtype) == TYPE_CODE_PTR)
527 /* A ptrdiff_t for the target would be preferable here. */
528 res_val = value_from_longest (builtin_type_pyint,
529 value_ptrdiff (arg1, arg2));
530 else if (TYPE_CODE (ltype) == TYPE_CODE_PTR
531 && is_integral_type (rtype))
532 res_val = value_ptradd (arg1, - value_as_long (arg2));
534 res_val = value_binop (arg1, arg2, BINOP_SUB);
538 res_val = value_binop (arg1, arg2, BINOP_MUL);
541 res_val = value_binop (arg1, arg2, BINOP_DIV);
544 res_val = value_binop (arg1, arg2, BINOP_REM);
547 res_val = value_binop (arg1, arg2, BINOP_EXP);
550 res_val = value_binop (arg1, arg2, BINOP_LSH);
553 res_val = value_binop (arg1, arg2, BINOP_RSH);
556 res_val = value_binop (arg1, arg2, BINOP_BITWISE_AND);
559 res_val = value_binop (arg1, arg2, BINOP_BITWISE_IOR);
562 res_val = value_binop (arg1, arg2, BINOP_BITWISE_XOR);
566 GDB_PY_HANDLE_EXCEPTION (except);
568 return res_val ? value_to_value_object (res_val) : NULL;
572 valpy_add (PyObject *self, PyObject *other)
574 return valpy_binop (VALPY_ADD, self, other);
578 valpy_subtract (PyObject *self, PyObject *other)
580 return valpy_binop (VALPY_SUB, self, other);
584 valpy_multiply (PyObject *self, PyObject *other)
586 return valpy_binop (VALPY_MUL, self, other);
590 valpy_divide (PyObject *self, PyObject *other)
592 return valpy_binop (VALPY_DIV, self, other);
596 valpy_remainder (PyObject *self, PyObject *other)
598 return valpy_binop (VALPY_REM, self, other);
602 valpy_power (PyObject *self, PyObject *other, PyObject *unused)
604 /* We don't support the ternary form of pow. I don't know how to express
605 that, so let's just throw NotImplementedError to at least do something
607 if (unused != Py_None)
609 PyErr_SetString (PyExc_NotImplementedError,
610 "Invalid operation on gdb.Value.");
614 return valpy_binop (VALPY_POW, self, other);
618 valpy_negative (PyObject *self)
620 struct value *val = NULL;
621 volatile struct gdb_exception except;
623 TRY_CATCH (except, RETURN_MASK_ALL)
625 val = value_neg (((value_object *) self)->value);
627 GDB_PY_HANDLE_EXCEPTION (except);
629 return value_to_value_object (val);
633 valpy_positive (PyObject *self)
635 return value_to_value_object (((value_object *) self)->value);
639 valpy_absolute (PyObject *self)
641 struct value *value = ((value_object *) self)->value;
643 if (value_less (value, value_zero (value_type (value), not_lval)))
644 return valpy_negative (self);
646 return valpy_positive (self);
649 /* Implements boolean evaluation of gdb.Value. */
651 valpy_nonzero (PyObject *self)
653 value_object *self_value = (value_object *) self;
656 type = check_typedef (value_type (self_value->value));
658 if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR)
659 return !!value_as_long (self_value->value);
660 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
661 return value_as_double (self_value->value) != 0;
662 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
663 return !decimal_is_zero (value_contents (self_value->value),
665 gdbarch_byte_order (get_type_arch (type)));
668 PyErr_SetString (PyExc_TypeError, _("Attempted truth testing on invalid "
674 /* Implements ~ for value objects. */
676 valpy_invert (PyObject *self)
678 struct value *val = NULL;
679 volatile struct gdb_exception except;
681 TRY_CATCH (except, RETURN_MASK_ALL)
683 val = value_complement (((value_object *) self)->value);
685 GDB_PY_HANDLE_EXCEPTION (except);
687 return value_to_value_object (val);
690 /* Implements left shift for value objects. */
692 valpy_lsh (PyObject *self, PyObject *other)
694 return valpy_binop (VALPY_LSH, self, other);
697 /* Implements right shift for value objects. */
699 valpy_rsh (PyObject *self, PyObject *other)
701 return valpy_binop (VALPY_RSH, self, other);
704 /* Implements bitwise and for value objects. */
706 valpy_and (PyObject *self, PyObject *other)
708 return valpy_binop (VALPY_BITAND, self, other);
711 /* Implements bitwise or for value objects. */
713 valpy_or (PyObject *self, PyObject *other)
715 return valpy_binop (VALPY_BITOR, self, other);
718 /* Implements bitwise xor for value objects. */
720 valpy_xor (PyObject *self, PyObject *other)
722 return valpy_binop (VALPY_BITXOR, self, other);
725 /* Implements comparison operations for value objects. */
727 valpy_richcompare (PyObject *self, PyObject *other, int op)
730 struct value *value_other;
731 volatile struct gdb_exception except;
733 if (other == Py_None)
734 /* Comparing with None is special. From what I can tell, in Python
735 None is smaller than anything else. */
747 PyErr_SetString (PyExc_NotImplementedError,
748 _("Invalid operation on gdb.Value."));
752 TRY_CATCH (except, RETURN_MASK_ALL)
754 value_other = convert_value_from_python (other);
755 if (value_other == NULL)
763 result = value_less (((value_object *) self)->value, value_other);
766 result = value_less (((value_object *) self)->value, value_other)
767 || value_equal (((value_object *) self)->value, value_other);
770 result = value_equal (((value_object *) self)->value, value_other);
773 result = !value_equal (((value_object *) self)->value, value_other);
776 result = value_less (value_other, ((value_object *) self)->value);
779 result = value_less (value_other, ((value_object *) self)->value)
780 || value_equal (((value_object *) self)->value, value_other);
784 PyErr_SetString (PyExc_NotImplementedError,
785 _("Invalid operation on gdb.Value."));
790 GDB_PY_HANDLE_EXCEPTION (except);
792 /* In this case, the Python exception has already been set. */
802 /* Helper function to determine if a type is "int-like". */
804 is_intlike (struct type *type, int ptr_ok)
806 CHECK_TYPEDEF (type);
807 return (TYPE_CODE (type) == TYPE_CODE_INT
808 || TYPE_CODE (type) == TYPE_CODE_ENUM
809 || TYPE_CODE (type) == TYPE_CODE_BOOL
810 || TYPE_CODE (type) == TYPE_CODE_CHAR
811 || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR));
814 /* Implements conversion to int. */
816 valpy_int (PyObject *self)
818 struct value *value = ((value_object *) self)->value;
819 struct type *type = value_type (value);
821 volatile struct gdb_exception except;
823 CHECK_TYPEDEF (type);
824 if (!is_intlike (type, 0))
826 PyErr_SetString (PyExc_RuntimeError,
827 _("Cannot convert value to int."));
831 TRY_CATCH (except, RETURN_MASK_ALL)
833 l = value_as_long (value);
835 GDB_PY_HANDLE_EXCEPTION (except);
837 #ifdef HAVE_LONG_LONG /* Defined by Python. */
838 /* If we have 'long long', and the value overflows a 'long', use a
839 Python Long; otherwise use a Python Int. */
840 if (sizeof (l) > sizeof (long) && (l > PyInt_GetMax ()
841 || l < (- (LONGEST) PyInt_GetMax ()) - 1))
842 return PyLong_FromLongLong (l);
844 return PyInt_FromLong (l);
847 /* Implements conversion to long. */
849 valpy_long (PyObject *self)
851 struct value *value = ((value_object *) self)->value;
852 struct type *type = value_type (value);
854 volatile struct gdb_exception except;
856 if (!is_intlike (type, 1))
858 PyErr_SetString (PyExc_RuntimeError,
859 _("Cannot convert value to long."));
863 TRY_CATCH (except, RETURN_MASK_ALL)
865 l = value_as_long (value);
867 GDB_PY_HANDLE_EXCEPTION (except);
869 #ifdef HAVE_LONG_LONG /* Defined by Python. */
870 return PyLong_FromLongLong (l);
872 return PyLong_FromLong (l);
876 /* Implements conversion to float. */
878 valpy_float (PyObject *self)
880 struct value *value = ((value_object *) self)->value;
881 struct type *type = value_type (value);
883 volatile struct gdb_exception except;
885 CHECK_TYPEDEF (type);
886 if (TYPE_CODE (type) != TYPE_CODE_FLT)
888 PyErr_SetString (PyExc_RuntimeError,
889 _("Cannot convert value to float."));
893 TRY_CATCH (except, RETURN_MASK_ALL)
895 d = value_as_double (value);
897 GDB_PY_HANDLE_EXCEPTION (except);
899 return PyFloat_FromDouble (d);
902 /* Returns an object for a value which is released from the all_values chain,
903 so its lifetime is not bound to the execution of a command. */
905 value_to_value_object (struct value *val)
907 value_object *val_obj;
909 val_obj = PyObject_New (value_object, &value_object_type);
912 val_obj->value = val;
914 val_obj->address = NULL;
915 val_obj->type = NULL;
916 note_value (val_obj);
919 return (PyObject *) val_obj;
922 /* Returns a borrowed reference to the struct value corresponding to
923 the given value object. */
925 value_object_to_value (PyObject *self)
929 if (! PyObject_TypeCheck (self, &value_object_type))
931 real = (value_object *) self;
935 /* Try to convert a Python value to a gdb value. If the value cannot
936 be converted, set a Python exception and return NULL. Returns a
937 reference to a new value on the all_values chain. */
940 convert_value_from_python (PyObject *obj)
942 struct value *value = NULL; /* -Wall */
944 volatile struct gdb_exception except;
947 gdb_assert (obj != NULL);
949 TRY_CATCH (except, RETURN_MASK_ALL)
951 if (PyBool_Check (obj))
953 cmp = PyObject_IsTrue (obj);
955 value = value_from_longest (builtin_type_pybool, cmp);
957 else if (PyInt_Check (obj))
959 long l = PyInt_AsLong (obj);
961 if (! PyErr_Occurred ())
962 value = value_from_longest (builtin_type_pyint, l);
964 else if (PyLong_Check (obj))
966 LONGEST l = PyLong_AsLongLong (obj);
968 if (PyErr_Occurred ())
970 /* If the error was an overflow, we can try converting to
972 if (PyErr_ExceptionMatches (PyExc_OverflowError))
974 PyObject *etype, *evalue, *etraceback, *zero;
976 PyErr_Fetch (&etype, &evalue, &etraceback);
977 zero = PyInt_FromLong (0);
979 /* Check whether obj is positive. */
980 if (PyObject_RichCompareBool (obj, zero, Py_GT) > 0)
984 ul = PyLong_AsUnsignedLongLong (obj);
985 if (! PyErr_Occurred ())
986 value = value_from_ulongest (builtin_type_upylong, ul);
989 /* There's nothing we can do. */
990 PyErr_Restore (etype, evalue, etraceback);
996 value = value_from_longest (builtin_type_pylong, l);
998 else if (PyFloat_Check (obj))
1000 double d = PyFloat_AsDouble (obj);
1002 if (! PyErr_Occurred ())
1003 value = value_from_double (builtin_type_pyfloat, d);
1005 else if (gdbpy_is_string (obj))
1009 s = python_string_to_target_string (obj);
1012 old = make_cleanup (xfree, s);
1013 value = value_cstring (s, strlen (s), builtin_type_pychar);
1017 else if (PyObject_TypeCheck (obj, &value_object_type))
1018 value = value_copy (((value_object *) obj)->value);
1019 else if (gdbpy_is_lazy_string (obj))
1022 PyObject *function = PyString_FromString ("value");
1024 result = PyObject_CallMethodObjArgs (obj, function, NULL);
1025 value = value_copy (((value_object *) result)->value);
1028 PyErr_Format (PyExc_TypeError, _("Could not convert Python object: %s."),
1029 PyString_AsString (PyObject_Str (obj)));
1031 if (except.reason < 0)
1033 PyErr_Format (except.reason == RETURN_QUIT
1034 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
1035 "%s", except.message);
1042 /* Returns value object in the ARGth position in GDB's history. */
1044 gdbpy_history (PyObject *self, PyObject *args)
1047 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
1048 volatile struct gdb_exception except;
1050 if (!PyArg_ParseTuple (args, "i", &i))
1053 TRY_CATCH (except, RETURN_MASK_ALL)
1055 res_val = access_value_history (i);
1057 GDB_PY_HANDLE_EXCEPTION (except);
1059 return value_to_value_object (res_val);
1062 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
1065 gdbpy_is_value_object (PyObject *obj)
1067 return PyObject_TypeCheck (obj, &value_object_type);
1071 gdbpy_initialize_values (void)
1073 if (PyType_Ready (&value_object_type) < 0)
1076 Py_INCREF (&value_object_type);
1077 PyModule_AddObject (gdb_module, "Value", (PyObject *) &value_object_type);
1079 values_in_python = NULL;
1084 static PyGetSetDef value_object_getset[] = {
1085 { "address", valpy_get_address, NULL, "The address of the value.",
1087 { "is_optimized_out", valpy_get_is_optimized_out, NULL,
1088 "Boolean telling whether the value is optimized out (i.e., not available).",
1090 { "type", valpy_get_type, NULL, "Type of the value.", NULL },
1091 {NULL} /* Sentinel */
1094 static PyMethodDef value_object_methods[] = {
1095 { "cast", valpy_cast, METH_VARARGS, "Cast the value to the supplied type." },
1096 { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
1097 { "lazy_string", (PyCFunction) valpy_lazy_string, METH_VARARGS | METH_KEYWORDS,
1098 "lazy_string ([encoding] [, length]) -> lazy_string\n\
1099 Return a lazy string representation of the value." },
1100 { "string", (PyCFunction) valpy_string, METH_VARARGS | METH_KEYWORDS,
1101 "string ([encoding] [, errors] [, length]) -> string\n\
1102 Return Unicode string representation of the value." },
1103 {NULL} /* Sentinel */
1106 static PyNumberMethods value_object_as_number = {
1112 NULL, /* nb_divmod */
1113 valpy_power, /* nb_power */
1114 valpy_negative, /* nb_negative */
1115 valpy_positive, /* nb_positive */
1116 valpy_absolute, /* nb_absolute */
1117 valpy_nonzero, /* nb_nonzero */
1118 valpy_invert, /* nb_invert */
1119 valpy_lsh, /* nb_lshift */
1120 valpy_rsh, /* nb_rshift */
1121 valpy_and, /* nb_and */
1122 valpy_xor, /* nb_xor */
1123 valpy_or, /* nb_or */
1124 NULL, /* nb_coerce */
1125 valpy_int, /* nb_int */
1126 valpy_long, /* nb_long */
1127 valpy_float, /* nb_float */
1132 static PyMappingMethods value_object_as_mapping = {
1138 PyTypeObject value_object_type = {
1139 PyObject_HEAD_INIT (NULL)
1141 "gdb.Value", /*tp_name*/
1142 sizeof (value_object), /*tp_basicsize*/
1144 valpy_dealloc, /*tp_dealloc*/
1150 &value_object_as_number, /*tp_as_number*/
1151 0, /*tp_as_sequence*/
1152 &value_object_as_mapping, /*tp_as_mapping*/
1153 valpy_hash, /*tp_hash*/
1155 valpy_str, /*tp_str*/
1159 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
1160 "GDB value object", /* tp_doc */
1161 0, /* tp_traverse */
1163 valpy_richcompare, /* tp_richcompare */
1164 0, /* tp_weaklistoffset */
1166 0, /* tp_iternext */
1167 value_object_methods, /* tp_methods */
1169 value_object_getset, /* tp_getset */
1172 0, /* tp_descr_get */
1173 0, /* tp_descr_set */
1174 0, /* tp_dictoffset */
1177 valpy_new /* tp_new */
1183 preserve_python_values (struct objfile *objfile, htab_t copied_types)
1188 #endif /* HAVE_PYTHON */