1 /* Python interface to values.
3 Copyright (C) 2008, 2009 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 #define builtin_type_pybool \
47 language_bool_type (python_language, python_gdbarch)
49 #define builtin_type_pychar \
50 language_string_char_type (python_language, python_gdbarch)
52 typedef struct value_object
{
54 struct value_object
*next
;
55 struct value_object
*prev
;
61 /* List of all values which are currently exposed to Python. It is
62 maintained so that when an objfile is discarded, preserve_values
63 can copy the values' types if needed. */
64 /* This variable is unnecessarily initialized to NULL in order to
65 work around a linker bug on MacOS. */
66 static value_object
*values_in_python
= NULL
;
68 /* Called by the Python interpreter when deallocating a value object. */
70 valpy_dealloc (PyObject
*obj
)
72 value_object
*self
= (value_object
*) obj
;
74 /* Remove SELF from the global list. */
76 self
->prev
->next
= self
->next
;
79 gdb_assert (values_in_python
== self
);
80 values_in_python
= self
->next
;
83 self
->next
->prev
= self
->prev
;
85 value_free (self
->value
);
88 /* Use braces to appease gcc warning. *sigh* */
90 Py_DECREF (self
->address
);
95 Py_DECREF (self
->type
);
98 self
->ob_type
->tp_free (self
);
101 /* Helper to push a Value object on the global list. */
103 note_value (value_object
*value_obj
)
105 value_obj
->next
= values_in_python
;
107 value_obj
->next
->prev
= value_obj
;
108 value_obj
->prev
= NULL
;
109 values_in_python
= value_obj
;
112 /* Called when a new gdb.Value object needs to be allocated. */
114 valpy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*keywords
)
116 struct value
*value
= NULL
; /* Initialize to appease gcc warning. */
117 value_object
*value_obj
;
119 if (PyTuple_Size (args
) != 1)
121 PyErr_SetString (PyExc_TypeError
, _("Value object creation takes only "
126 value_obj
= (value_object
*) subtype
->tp_alloc (subtype
, 1);
127 if (value_obj
== NULL
)
129 PyErr_SetString (PyExc_MemoryError
, _("Could not allocate memory to "
130 "create Value object."));
134 value
= convert_value_from_python (PyTuple_GetItem (args
, 0));
137 subtype
->tp_free (value_obj
);
141 value_obj
->value
= value
;
142 value_incref (value
);
143 value_obj
->address
= NULL
;
144 value_obj
->type
= NULL
;
145 note_value (value_obj
);
147 return (PyObject
*) value_obj
;
150 /* Iterate over all the Value objects, calling preserve_one_value on
153 preserve_python_values (struct objfile
*objfile
, htab_t copied_types
)
157 for (iter
= values_in_python
; iter
; iter
= iter
->next
)
158 preserve_one_value (iter
->value
, objfile
, copied_types
);
161 /* Given a value of a pointer type, apply the C unary * operator to it. */
163 valpy_dereference (PyObject
*self
, PyObject
*args
)
165 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
166 volatile struct gdb_exception except
;
168 TRY_CATCH (except
, RETURN_MASK_ALL
)
170 res_val
= value_ind (((value_object
*) self
)->value
);
172 GDB_PY_HANDLE_EXCEPTION (except
);
174 return value_to_value_object (res_val
);
177 /* Return "&value". */
179 valpy_get_address (PyObject
*self
, void *closure
)
181 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
182 value_object
*val_obj
= (value_object
*) self
;
183 volatile struct gdb_exception except
;
185 if (!val_obj
->address
)
187 TRY_CATCH (except
, RETURN_MASK_ALL
)
189 res_val
= value_addr (val_obj
->value
);
191 if (except
.reason
< 0)
193 val_obj
->address
= Py_None
;
197 val_obj
->address
= value_to_value_object (res_val
);
200 Py_INCREF (val_obj
->address
);
202 return val_obj
->address
;
205 /* Return type of the value. */
207 valpy_get_type (PyObject
*self
, void *closure
)
209 value_object
*obj
= (value_object
*) self
;
212 obj
->type
= type_to_type_object (value_type (obj
->value
));
216 Py_INCREF (obj
->type
);
219 Py_INCREF (obj
->type
);
223 /* Implementation of gdb.Value.string ([encoding] [, errors]
224 [, length]) -> string. Return Unicode string with value contents.
225 If ENCODING is not given, the string is assumed to be encoded in
226 the target's charset. If LENGTH is provided, only fetch string to
227 the length provided. */
230 valpy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
232 int length
= -1, ret
= 0;
234 struct value
*value
= ((value_object
*) self
)->value
;
235 volatile struct gdb_exception except
;
237 const char *encoding
= NULL
;
238 const char *errors
= NULL
;
239 const char *user_encoding
= NULL
;
240 const char *la_encoding
= NULL
;
241 static char *keywords
[] = { "encoding", "errors", "length" };
243 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|ssi", keywords
,
244 &user_encoding
, &errors
, &length
))
247 TRY_CATCH (except
, RETURN_MASK_ALL
)
249 LA_GET_STRING (value
, &buffer
, &length
, &la_encoding
);
251 GDB_PY_HANDLE_EXCEPTION (except
);
253 encoding
= (user_encoding
&& *user_encoding
) ? user_encoding
: la_encoding
;
254 unicode
= PyUnicode_Decode (buffer
, length
, encoding
, errors
);
260 /* Cast a value to a given type. */
262 valpy_cast (PyObject
*self
, PyObject
*args
)
266 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
267 volatile struct gdb_exception except
;
269 if (! PyArg_ParseTuple (args
, "O", &type_obj
))
272 type
= type_object_to_type (type_obj
);
275 PyErr_SetString (PyExc_RuntimeError
, "argument must be a Type");
279 TRY_CATCH (except
, RETURN_MASK_ALL
)
281 res_val
= value_cast (type
, ((value_object
*) self
)->value
);
283 GDB_PY_HANDLE_EXCEPTION (except
);
285 return value_to_value_object (res_val
);
289 valpy_length (PyObject
*self
)
291 /* We don't support getting the number of elements in a struct / class. */
292 PyErr_SetString (PyExc_NotImplementedError
,
293 "Invalid operation on gdb.Value.");
297 /* Given string name of an element inside structure, return its value
300 valpy_getitem (PyObject
*self
, PyObject
*key
)
302 value_object
*self_value
= (value_object
*) self
;
304 struct value
*res_val
= NULL
;
305 volatile struct gdb_exception except
;
307 if (gdbpy_is_string (key
))
309 field
= python_string_to_host_string (key
);
314 TRY_CATCH (except
, RETURN_MASK_ALL
)
316 struct value
*tmp
= self_value
->value
;
319 res_val
= value_struct_elt (&tmp
, NULL
, field
, 0, NULL
);
322 /* Assume we are attempting an array access, and let the
323 value code throw an exception if the index has an invalid
325 struct value
*idx
= convert_value_from_python (key
);
327 res_val
= value_subscript (tmp
, value_as_long (idx
));
332 GDB_PY_HANDLE_EXCEPTION (except
);
334 return res_val
? value_to_value_object (res_val
) : NULL
;
338 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
340 PyErr_Format (PyExc_NotImplementedError
,
341 _("Setting of struct elements is not currently supported."));
345 /* Called by the Python interpreter to obtain string representation
348 valpy_str (PyObject
*self
)
352 struct cleanup
*old_chain
;
354 struct value_print_options opts
;
355 volatile struct gdb_exception except
;
357 get_user_print_options (&opts
);
360 stb
= mem_fileopen ();
361 old_chain
= make_cleanup_ui_file_delete (stb
);
363 TRY_CATCH (except
, RETURN_MASK_ALL
)
365 common_val_print (((value_object
*) self
)->value
, stb
, 0,
366 &opts
, python_language
);
367 s
= ui_file_xstrdup (stb
, NULL
);
369 GDB_PY_HANDLE_EXCEPTION (except
);
371 do_cleanups (old_chain
);
373 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
379 /* Implements gdb.Value.is_optimized_out. */
381 valpy_get_is_optimized_out (PyObject
*self
, void *closure
)
383 struct value
*value
= ((value_object
*) self
)->value
;
385 if (value_optimized_out (value
))
406 /* If TYPE is a reference, return the target; otherwise return TYPE. */
407 #define STRIP_REFERENCE(TYPE) \
408 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
410 /* Returns a value object which is the result of applying the operation
411 specified by OPCODE to the given arguments. */
413 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
415 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
416 volatile struct gdb_exception except
;
418 TRY_CATCH (except
, RETURN_MASK_ALL
)
420 struct value
*arg1
, *arg2
;
422 /* If the gdb.Value object is the second operand, then it will be passed
423 to us as the OTHER argument, and SELF will be an entirely different
424 kind of object, altogether. Because of this, we can't assume self is
425 a gdb.Value object and need to convert it from python as well. */
426 arg1
= convert_value_from_python (self
);
430 arg2
= convert_value_from_python (other
);
438 struct type
*ltype
= value_type (arg1
);
439 struct type
*rtype
= value_type (arg2
);
441 CHECK_TYPEDEF (ltype
);
442 ltype
= STRIP_REFERENCE (ltype
);
443 CHECK_TYPEDEF (rtype
);
444 rtype
= STRIP_REFERENCE (rtype
);
446 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
447 && is_integral_type (rtype
))
448 res_val
= value_ptradd (arg1
, value_as_long (arg2
));
449 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
450 && is_integral_type (ltype
))
451 res_val
= value_ptradd (arg2
, value_as_long (arg1
));
453 res_val
= value_binop (arg1
, arg2
, BINOP_ADD
);
458 struct type
*ltype
= value_type (arg1
);
459 struct type
*rtype
= value_type (arg2
);
461 CHECK_TYPEDEF (ltype
);
462 ltype
= STRIP_REFERENCE (ltype
);
463 CHECK_TYPEDEF (rtype
);
464 rtype
= STRIP_REFERENCE (rtype
);
466 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
467 && TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
468 /* A ptrdiff_t for the target would be preferable here. */
469 res_val
= value_from_longest (builtin_type_pyint
,
470 value_ptrdiff (arg1
, arg2
));
471 else if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
472 && is_integral_type (rtype
))
473 res_val
= value_ptradd (arg1
, - value_as_long (arg2
));
475 res_val
= value_binop (arg1
, arg2
, BINOP_SUB
);
479 res_val
= value_binop (arg1
, arg2
, BINOP_MUL
);
482 res_val
= value_binop (arg1
, arg2
, BINOP_DIV
);
485 res_val
= value_binop (arg1
, arg2
, BINOP_REM
);
488 res_val
= value_binop (arg1
, arg2
, BINOP_EXP
);
491 res_val
= value_binop (arg1
, arg2
, BINOP_LSH
);
494 res_val
= value_binop (arg1
, arg2
, BINOP_RSH
);
497 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_AND
);
500 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_IOR
);
503 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_XOR
);
507 GDB_PY_HANDLE_EXCEPTION (except
);
509 return res_val
? value_to_value_object (res_val
) : NULL
;
513 valpy_add (PyObject
*self
, PyObject
*other
)
515 return valpy_binop (VALPY_ADD
, self
, other
);
519 valpy_subtract (PyObject
*self
, PyObject
*other
)
521 return valpy_binop (VALPY_SUB
, self
, other
);
525 valpy_multiply (PyObject
*self
, PyObject
*other
)
527 return valpy_binop (VALPY_MUL
, self
, other
);
531 valpy_divide (PyObject
*self
, PyObject
*other
)
533 return valpy_binop (VALPY_DIV
, self
, other
);
537 valpy_remainder (PyObject
*self
, PyObject
*other
)
539 return valpy_binop (VALPY_REM
, self
, other
);
543 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
545 /* We don't support the ternary form of pow. I don't know how to express
546 that, so let's just throw NotImplementedError to at least do something
548 if (unused
!= Py_None
)
550 PyErr_SetString (PyExc_NotImplementedError
,
551 "Invalid operation on gdb.Value.");
555 return valpy_binop (VALPY_POW
, self
, other
);
559 valpy_negative (PyObject
*self
)
561 struct value
*val
= NULL
;
562 volatile struct gdb_exception except
;
564 TRY_CATCH (except
, RETURN_MASK_ALL
)
566 val
= value_neg (((value_object
*) self
)->value
);
568 GDB_PY_HANDLE_EXCEPTION (except
);
570 return value_to_value_object (val
);
574 valpy_positive (PyObject
*self
)
576 return value_to_value_object (((value_object
*) self
)->value
);
580 valpy_absolute (PyObject
*self
)
582 struct value
*value
= ((value_object
*) self
)->value
;
583 if (value_less (value
, value_zero (value_type (value
), not_lval
)))
584 return valpy_negative (self
);
586 return valpy_positive (self
);
589 /* Implements boolean evaluation of gdb.Value. */
591 valpy_nonzero (PyObject
*self
)
593 value_object
*self_value
= (value_object
*) self
;
596 type
= check_typedef (value_type (self_value
->value
));
598 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
599 return !!value_as_long (self_value
->value
);
600 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
601 return value_as_double (self_value
->value
) != 0;
602 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
603 return !decimal_is_zero (value_contents (self_value
->value
),
605 gdbarch_byte_order (get_type_arch (type
)));
608 PyErr_SetString (PyExc_TypeError
, _("Attempted truth testing on invalid "
614 /* Implements ~ for value objects. */
616 valpy_invert (PyObject
*self
)
618 struct value
*val
= NULL
;
619 volatile struct gdb_exception except
;
621 TRY_CATCH (except
, RETURN_MASK_ALL
)
623 val
= value_complement (((value_object
*) self
)->value
);
625 GDB_PY_HANDLE_EXCEPTION (except
);
627 return value_to_value_object (val
);
630 /* Implements left shift for value objects. */
632 valpy_lsh (PyObject
*self
, PyObject
*other
)
634 return valpy_binop (VALPY_LSH
, self
, other
);
637 /* Implements right shift for value objects. */
639 valpy_rsh (PyObject
*self
, PyObject
*other
)
641 return valpy_binop (VALPY_RSH
, self
, other
);
644 /* Implements bitwise and for value objects. */
646 valpy_and (PyObject
*self
, PyObject
*other
)
648 return valpy_binop (VALPY_BITAND
, self
, other
);
651 /* Implements bitwise or for value objects. */
653 valpy_or (PyObject
*self
, PyObject
*other
)
655 return valpy_binop (VALPY_BITOR
, self
, other
);
658 /* Implements bitwise xor for value objects. */
660 valpy_xor (PyObject
*self
, PyObject
*other
)
662 return valpy_binop (VALPY_BITXOR
, self
, other
);
665 /* Implements comparison operations for value objects. */
667 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
670 struct value
*value_other
;
671 volatile struct gdb_exception except
;
673 if (other
== Py_None
)
674 /* Comparing with None is special. From what I can tell, in Python
675 None is smaller than anything else. */
687 PyErr_SetString (PyExc_NotImplementedError
,
688 "Invalid operation on gdb.Value.");
692 TRY_CATCH (except
, RETURN_MASK_ALL
)
694 value_other
= convert_value_from_python (other
);
695 if (value_other
== NULL
)
703 result
= value_less (((value_object
*) self
)->value
, value_other
);
706 result
= value_less (((value_object
*) self
)->value
, value_other
)
707 || value_equal (((value_object
*) self
)->value
, value_other
);
710 result
= value_equal (((value_object
*) self
)->value
, value_other
);
713 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
716 result
= value_less (value_other
, ((value_object
*) self
)->value
);
719 result
= value_less (value_other
, ((value_object
*) self
)->value
)
720 || value_equal (((value_object
*) self
)->value
, value_other
);
724 PyErr_SetString (PyExc_NotImplementedError
,
725 "Invalid operation on gdb.Value.");
730 GDB_PY_HANDLE_EXCEPTION (except
);
732 /* In this case, the Python exception has already been set. */
742 /* Helper function to determine if a type is "int-like". */
744 is_intlike (struct type
*type
, int ptr_ok
)
746 CHECK_TYPEDEF (type
);
747 return (TYPE_CODE (type
) == TYPE_CODE_INT
748 || TYPE_CODE (type
) == TYPE_CODE_ENUM
749 || TYPE_CODE (type
) == TYPE_CODE_BOOL
750 || TYPE_CODE (type
) == TYPE_CODE_CHAR
751 || (ptr_ok
&& TYPE_CODE (type
) == TYPE_CODE_PTR
));
754 /* Implements conversion to int. */
756 valpy_int (PyObject
*self
)
758 struct value
*value
= ((value_object
*) self
)->value
;
759 struct type
*type
= value_type (value
);
761 volatile struct gdb_exception except
;
763 CHECK_TYPEDEF (type
);
764 if (!is_intlike (type
, 0))
766 PyErr_SetString (PyExc_RuntimeError
, "cannot convert value to int");
770 TRY_CATCH (except
, RETURN_MASK_ALL
)
772 l
= value_as_long (value
);
774 GDB_PY_HANDLE_EXCEPTION (except
);
776 return PyInt_FromLong (l
);
779 /* Implements conversion to long. */
781 valpy_long (PyObject
*self
)
783 struct value
*value
= ((value_object
*) self
)->value
;
784 struct type
*type
= value_type (value
);
786 volatile struct gdb_exception except
;
788 if (!is_intlike (type
, 1))
790 PyErr_SetString (PyExc_RuntimeError
, "cannot convert value to long");
794 TRY_CATCH (except
, RETURN_MASK_ALL
)
796 l
= value_as_long (value
);
798 GDB_PY_HANDLE_EXCEPTION (except
);
800 return PyLong_FromLong (l
);
803 /* Implements conversion to float. */
805 valpy_float (PyObject
*self
)
807 struct value
*value
= ((value_object
*) self
)->value
;
808 struct type
*type
= value_type (value
);
810 volatile struct gdb_exception except
;
812 CHECK_TYPEDEF (type
);
813 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
815 PyErr_SetString (PyExc_RuntimeError
, "cannot convert value to float");
819 TRY_CATCH (except
, RETURN_MASK_ALL
)
821 d
= value_as_double (value
);
823 GDB_PY_HANDLE_EXCEPTION (except
);
825 return PyFloat_FromDouble (d
);
828 /* Returns an object for a value which is released from the all_values chain,
829 so its lifetime is not bound to the execution of a command. */
831 value_to_value_object (struct value
*val
)
833 value_object
*val_obj
;
835 val_obj
= PyObject_New (value_object
, &value_object_type
);
838 val_obj
->value
= val
;
840 val_obj
->address
= NULL
;
841 val_obj
->type
= NULL
;
842 note_value (val_obj
);
845 return (PyObject
*) val_obj
;
848 /* Returns a borrowed reference to the struct value corresponding to
849 the given value object. */
851 value_object_to_value (PyObject
*self
)
854 if (! PyObject_TypeCheck (self
, &value_object_type
))
856 real
= (value_object
*) self
;
860 /* Try to convert a Python value to a gdb value. If the value cannot
861 be converted, set a Python exception and return NULL. Returns a
862 reference to a new value on the all_values chain. */
865 convert_value_from_python (PyObject
*obj
)
867 struct value
*value
= NULL
; /* -Wall */
868 PyObject
*target_str
, *unicode_str
;
870 volatile struct gdb_exception except
;
873 gdb_assert (obj
!= NULL
);
875 TRY_CATCH (except
, RETURN_MASK_ALL
)
877 if (PyBool_Check (obj
))
879 cmp
= PyObject_IsTrue (obj
);
881 value
= value_from_longest (builtin_type_pybool
, cmp
);
883 else if (PyInt_Check (obj
))
885 long l
= PyInt_AsLong (obj
);
887 if (! PyErr_Occurred ())
888 value
= value_from_longest (builtin_type_pyint
, l
);
890 else if (PyLong_Check (obj
))
892 LONGEST l
= PyLong_AsLongLong (obj
);
894 if (! PyErr_Occurred ())
895 value
= value_from_longest (builtin_type_pylong
, l
);
897 else if (PyFloat_Check (obj
))
899 double d
= PyFloat_AsDouble (obj
);
901 if (! PyErr_Occurred ())
902 value
= value_from_double (builtin_type_pyfloat
, d
);
904 else if (gdbpy_is_string (obj
))
908 s
= python_string_to_target_string (obj
);
911 old
= make_cleanup (xfree
, s
);
912 value
= value_cstring (s
, strlen (s
), builtin_type_pychar
);
916 else if (PyObject_TypeCheck (obj
, &value_object_type
))
917 value
= value_copy (((value_object
*) obj
)->value
);
919 PyErr_Format (PyExc_TypeError
, _("Could not convert Python object: %s"),
920 PyString_AsString (PyObject_Str (obj
)));
922 if (except
.reason
< 0)
924 PyErr_Format (except
.reason
== RETURN_QUIT
925 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
926 "%s", except
.message
);
933 /* Returns value object in the ARGth position in GDB's history. */
935 gdbpy_history (PyObject
*self
, PyObject
*args
)
938 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
939 volatile struct gdb_exception except
;
941 if (!PyArg_ParseTuple (args
, "i", &i
))
944 TRY_CATCH (except
, RETURN_MASK_ALL
)
946 res_val
= access_value_history (i
);
948 GDB_PY_HANDLE_EXCEPTION (except
);
950 return value_to_value_object (res_val
);
954 gdbpy_initialize_values (void)
956 if (PyType_Ready (&value_object_type
) < 0)
959 Py_INCREF (&value_object_type
);
960 PyModule_AddObject (gdb_module
, "Value", (PyObject
*) &value_object_type
);
962 values_in_python
= NULL
;
967 static PyGetSetDef value_object_getset
[] = {
968 { "address", valpy_get_address
, NULL
, "The address of the value.",
970 { "is_optimized_out", valpy_get_is_optimized_out
, NULL
,
971 "Boolean telling whether the value is optimized out (i.e., not available).",
973 { "type", valpy_get_type
, NULL
, "Type of the value.", NULL
},
974 {NULL
} /* Sentinel */
977 static PyMethodDef value_object_methods
[] = {
978 { "cast", valpy_cast
, METH_VARARGS
, "Cast the value to the supplied type." },
979 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
980 { "string", (PyCFunction
) valpy_string
, METH_VARARGS
| METH_KEYWORDS
,
981 "string ([encoding] [, errors] [, length]) -> string\n\
982 Return Unicode string representation of the value." },
983 {NULL
} /* Sentinel */
986 static PyNumberMethods value_object_as_number
= {
992 NULL
, /* nb_divmod */
993 valpy_power
, /* nb_power */
994 valpy_negative
, /* nb_negative */
995 valpy_positive
, /* nb_positive */
996 valpy_absolute
, /* nb_absolute */
997 valpy_nonzero
, /* nb_nonzero */
998 valpy_invert
, /* nb_invert */
999 valpy_lsh
, /* nb_lshift */
1000 valpy_rsh
, /* nb_rshift */
1001 valpy_and
, /* nb_and */
1002 valpy_xor
, /* nb_xor */
1003 valpy_or
, /* nb_or */
1004 NULL
, /* nb_coerce */
1005 valpy_int
, /* nb_int */
1006 valpy_long
, /* nb_long */
1007 valpy_float
, /* nb_float */
1012 static PyMappingMethods value_object_as_mapping
= {
1018 PyTypeObject value_object_type
= {
1019 PyObject_HEAD_INIT (NULL
)
1021 "gdb.Value", /*tp_name*/
1022 sizeof (value_object
), /*tp_basicsize*/
1024 valpy_dealloc
, /*tp_dealloc*/
1030 &value_object_as_number
, /*tp_as_number*/
1031 0, /*tp_as_sequence*/
1032 &value_object_as_mapping
, /*tp_as_mapping*/
1035 valpy_str
, /*tp_str*/
1039 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
, /*tp_flags*/
1040 "GDB value object", /* tp_doc */
1041 0, /* tp_traverse */
1043 valpy_richcompare
, /* tp_richcompare */
1044 0, /* tp_weaklistoffset */
1046 0, /* tp_iternext */
1047 value_object_methods
, /* tp_methods */
1049 value_object_getset
, /* tp_getset */
1052 0, /* tp_descr_get */
1053 0, /* tp_descr_set */
1054 0, /* tp_dictoffset */
1057 valpy_new
/* tp_new */
1063 preserve_python_values (struct objfile
*objfile
, htab_t copied_types
)
1068 #endif /* HAVE_PYTHON */