1 /* Python interface to values.
3 Copyright (C) 2008-2018 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/>. */
24 #include "target-float.h"
27 #include "expression.h"
31 #include "python-internal.h"
34 /* Even though Python scalar types directly map to host types, we use
35 target types here to remain consistent with the values system in
36 GDB (which uses target arithmetic). */
38 /* Python's integer type corresponds to C's long type. */
39 #define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long
41 /* Python's float type corresponds to C's double type. */
42 #define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double
44 /* Python's long type corresponds to C's long long type. */
45 #define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_long
47 /* Python's long type corresponds to C's long long type. Unsigned version. */
48 #define builtin_type_upylong builtin_type \
49 (python_gdbarch)->builtin_unsigned_long_long
51 #define builtin_type_pybool \
52 language_bool_type (python_language, python_gdbarch)
54 #define builtin_type_pychar \
55 language_string_char_type (python_language, python_gdbarch)
57 typedef struct value_object
{
59 struct value_object
*next
;
60 struct value_object
*prev
;
64 PyObject
*dynamic_type
;
67 /* List of all values which are currently exposed to Python. It is
68 maintained so that when an objfile is discarded, preserve_values
69 can copy the values' types if needed. */
70 /* This variable is unnecessarily initialized to NULL in order to
71 work around a linker bug on MacOS. */
72 static value_object
*values_in_python
= NULL
;
74 /* Called by the Python interpreter when deallocating a value object. */
76 valpy_dealloc (PyObject
*obj
)
78 value_object
*self
= (value_object
*) obj
;
80 /* Remove SELF from the global list. */
82 self
->prev
->next
= self
->next
;
85 gdb_assert (values_in_python
== self
);
86 values_in_python
= self
->next
;
89 self
->next
->prev
= self
->prev
;
91 value_decref (self
->value
);
94 /* Use braces to appease gcc warning. *sigh* */
96 Py_DECREF (self
->address
);
101 Py_DECREF (self
->type
);
104 Py_XDECREF (self
->dynamic_type
);
106 Py_TYPE (self
)->tp_free (self
);
109 /* Helper to push a Value object on the global list. */
111 note_value (value_object
*value_obj
)
113 value_obj
->next
= values_in_python
;
115 value_obj
->next
->prev
= value_obj
;
116 value_obj
->prev
= NULL
;
117 values_in_python
= value_obj
;
120 /* Called when a new gdb.Value object needs to be allocated. Returns NULL on
121 error, with a python exception set. */
123 valpy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*keywords
)
125 struct value
*value
= NULL
; /* Initialize to appease gcc warning. */
126 value_object
*value_obj
;
128 if (PyTuple_Size (args
) != 1)
130 PyErr_SetString (PyExc_TypeError
, _("Value object creation takes only "
135 value_obj
= (value_object
*) subtype
->tp_alloc (subtype
, 1);
136 if (value_obj
== NULL
)
138 PyErr_SetString (PyExc_MemoryError
, _("Could not allocate memory to "
139 "create Value object."));
143 value
= convert_value_from_python (PyTuple_GetItem (args
, 0));
146 subtype
->tp_free (value_obj
);
150 value_obj
->value
= release_value (value
).release ();
151 value_obj
->address
= NULL
;
152 value_obj
->type
= NULL
;
153 value_obj
->dynamic_type
= NULL
;
154 note_value (value_obj
);
156 return (PyObject
*) value_obj
;
159 /* Iterate over all the Value objects, calling preserve_one_value on
162 gdbpy_preserve_values (const struct extension_language_defn
*extlang
,
163 struct objfile
*objfile
, htab_t copied_types
)
167 for (iter
= values_in_python
; iter
; iter
= iter
->next
)
168 preserve_one_value (iter
->value
, objfile
, copied_types
);
171 /* Given a value of a pointer type, apply the C unary * operator to it. */
173 valpy_dereference (PyObject
*self
, PyObject
*args
)
175 PyObject
*result
= NULL
;
179 struct value
*res_val
;
180 scoped_value_mark free_values
;
182 res_val
= value_ind (((value_object
*) self
)->value
);
183 result
= value_to_value_object (res_val
);
185 CATCH (except
, RETURN_MASK_ALL
)
187 GDB_PY_HANDLE_EXCEPTION (except
);
194 /* Given a value of a pointer type or a reference type, return the value
195 referenced. The difference between this function and valpy_dereference is
196 that the latter applies * unary operator to a value, which need not always
197 result in the value referenced. For example, for a value which is a reference
198 to an 'int' pointer ('int *'), valpy_dereference will result in a value of
199 type 'int' while valpy_referenced_value will result in a value of type
203 valpy_referenced_value (PyObject
*self
, PyObject
*args
)
205 PyObject
*result
= NULL
;
209 struct value
*self_val
, *res_val
;
210 scoped_value_mark free_values
;
212 self_val
= ((value_object
*) self
)->value
;
213 switch (TYPE_CODE (check_typedef (value_type (self_val
))))
216 res_val
= value_ind (self_val
);
219 case TYPE_CODE_RVALUE_REF
:
220 res_val
= coerce_ref (self_val
);
223 error(_("Trying to get the referenced value from a value which is "
224 "neither a pointer nor a reference."));
227 result
= value_to_value_object (res_val
);
229 CATCH (except
, RETURN_MASK_ALL
)
231 GDB_PY_HANDLE_EXCEPTION (except
);
238 /* Return a value which is a reference to the value. */
241 valpy_reference_value (PyObject
*self
, PyObject
*args
, enum type_code refcode
)
243 PyObject
*result
= NULL
;
247 struct value
*self_val
;
248 scoped_value_mark free_values
;
250 self_val
= ((value_object
*) self
)->value
;
251 result
= value_to_value_object (value_ref (self_val
, refcode
));
253 CATCH (except
, RETURN_MASK_ALL
)
255 GDB_PY_HANDLE_EXCEPTION (except
);
263 valpy_lvalue_reference_value (PyObject
*self
, PyObject
*args
)
265 return valpy_reference_value (self
, args
, TYPE_CODE_REF
);
269 valpy_rvalue_reference_value (PyObject
*self
, PyObject
*args
)
271 return valpy_reference_value (self
, args
, TYPE_CODE_RVALUE_REF
);
274 /* Return a "const" qualified version of the value. */
277 valpy_const_value (PyObject
*self
, PyObject
*args
)
279 PyObject
*result
= NULL
;
283 struct value
*self_val
, *res_val
;
284 scoped_value_mark free_values
;
286 self_val
= ((value_object
*) self
)->value
;
287 res_val
= make_cv_value (1, 0, self_val
);
288 result
= value_to_value_object (res_val
);
290 CATCH (except
, RETURN_MASK_ALL
)
292 GDB_PY_HANDLE_EXCEPTION (except
);
299 /* Return "&value". */
301 valpy_get_address (PyObject
*self
, void *closure
)
303 value_object
*val_obj
= (value_object
*) self
;
305 if (!val_obj
->address
)
309 struct value
*res_val
;
310 scoped_value_mark free_values
;
312 res_val
= value_addr (val_obj
->value
);
313 val_obj
->address
= value_to_value_object (res_val
);
315 CATCH (except
, RETURN_MASK_ALL
)
317 val_obj
->address
= Py_None
;
323 Py_XINCREF (val_obj
->address
);
325 return val_obj
->address
;
328 /* Return type of the value. */
330 valpy_get_type (PyObject
*self
, void *closure
)
332 value_object
*obj
= (value_object
*) self
;
336 obj
->type
= type_to_type_object (value_type (obj
->value
));
340 Py_INCREF (obj
->type
);
344 /* Return dynamic type of the value. */
347 valpy_get_dynamic_type (PyObject
*self
, void *closure
)
349 value_object
*obj
= (value_object
*) self
;
350 struct type
*type
= NULL
;
352 if (obj
->dynamic_type
!= NULL
)
354 Py_INCREF (obj
->dynamic_type
);
355 return obj
->dynamic_type
;
360 struct value
*val
= obj
->value
;
361 scoped_value_mark free_values
;
363 type
= value_type (val
);
364 type
= check_typedef (type
);
366 if (((TYPE_CODE (type
) == TYPE_CODE_PTR
) || TYPE_IS_REFERENCE (type
))
367 && (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_STRUCT
))
369 struct value
*target
;
370 int was_pointer
= TYPE_CODE (type
) == TYPE_CODE_PTR
;
373 target
= value_ind (val
);
375 target
= coerce_ref (val
);
376 type
= value_rtti_type (target
, NULL
, NULL
, NULL
);
381 type
= lookup_pointer_type (type
);
383 type
= lookup_lvalue_reference_type (type
);
386 else if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
387 type
= value_rtti_type (val
, NULL
, NULL
, NULL
);
390 /* Re-use object's static type. */
394 CATCH (except
, RETURN_MASK_ALL
)
396 GDB_PY_HANDLE_EXCEPTION (except
);
401 obj
->dynamic_type
= valpy_get_type (self
, NULL
);
403 obj
->dynamic_type
= type_to_type_object (type
);
405 Py_XINCREF (obj
->dynamic_type
);
406 return obj
->dynamic_type
;
409 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
410 string. Return a PyObject representing a lazy_string_object type.
411 A lazy string is a pointer to a string with an optional encoding and
412 length. If ENCODING is not given, encoding is set to None. If an
413 ENCODING is provided the encoding parameter is set to ENCODING, but
414 the string is not encoded.
415 If LENGTH is provided then the length parameter is set to LENGTH.
416 Otherwise if the value is an array of known length then the array's length
417 is used. Otherwise the length will be set to -1 (meaning first null of
420 Note: In order to not break any existing uses this allows creating
421 lazy strings from anything. PR 20769. E.g.,
422 gdb.parse_and_eval("my_int_variable").lazy_string().
423 "It's easier to relax restrictions than it is to impose them after the
424 fact." So we should be flagging any unintended uses as errors, but it's
425 perhaps too late for that. */
428 valpy_lazy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
430 gdb_py_longest length
= -1;
431 struct value
*value
= ((value_object
*) self
)->value
;
432 const char *user_encoding
= NULL
;
433 static const char *keywords
[] = { "encoding", "length", NULL
};
434 PyObject
*str_obj
= NULL
;
436 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "|s" GDB_PY_LL_ARG
,
437 keywords
, &user_encoding
, &length
))
442 PyErr_SetString (PyExc_ValueError
, _("Invalid length."));
448 scoped_value_mark free_values
;
449 struct type
*type
, *realtype
;
452 type
= value_type (value
);
453 realtype
= check_typedef (type
);
455 switch (TYPE_CODE (realtype
))
457 case TYPE_CODE_ARRAY
:
459 LONGEST array_length
= -1;
460 LONGEST low_bound
, high_bound
;
462 /* PR 20786: There's no way to specify an array of length zero.
463 Record a length of [0,-1] which is how Ada does it. Anything
464 we do is broken, but this one possible solution. */
465 if (get_array_bounds (realtype
, &low_bound
, &high_bound
))
466 array_length
= high_bound
- low_bound
+ 1;
468 length
= array_length
;
469 else if (array_length
== -1)
471 type
= lookup_array_range_type (TYPE_TARGET_TYPE (realtype
),
474 else if (length
!= array_length
)
476 /* We need to create a new array type with the
478 if (length
> array_length
)
479 error (_("Length is larger than array size."));
480 type
= lookup_array_range_type (TYPE_TARGET_TYPE (realtype
),
482 low_bound
+ length
- 1);
484 addr
= value_address (value
);
488 /* If a length is specified we defer creating an array of the
489 specified width until we need to. */
490 addr
= value_as_address (value
);
493 /* Should flag an error here. PR 20769. */
494 addr
= value_address (value
);
498 str_obj
= gdbpy_create_lazy_string_object (addr
, length
, user_encoding
,
501 CATCH (except
, RETURN_MASK_ALL
)
503 GDB_PY_HANDLE_EXCEPTION (except
);
510 /* Implementation of gdb.Value.string ([encoding] [, errors]
511 [, length]) -> string. Return Unicode string with value contents.
512 If ENCODING is not given, the string is assumed to be encoded in
513 the target's charset. If LENGTH is provided, only fetch string to
514 the length provided. */
517 valpy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
520 gdb::unique_xmalloc_ptr
<gdb_byte
> buffer
;
521 struct value
*value
= ((value_object
*) self
)->value
;
522 const char *encoding
= NULL
;
523 const char *errors
= NULL
;
524 const char *user_encoding
= NULL
;
525 const char *la_encoding
= NULL
;
526 struct type
*char_type
;
527 static const char *keywords
[] = { "encoding", "errors", "length", NULL
};
529 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kw
, "|ssi", keywords
,
530 &user_encoding
, &errors
, &length
))
535 LA_GET_STRING (value
, &buffer
, &length
, &char_type
, &la_encoding
);
537 CATCH (except
, RETURN_MASK_ALL
)
539 GDB_PY_HANDLE_EXCEPTION (except
);
543 encoding
= (user_encoding
&& *user_encoding
) ? user_encoding
: la_encoding
;
544 return PyUnicode_Decode ((const char *) buffer
.get (),
545 length
* TYPE_LENGTH (char_type
),
549 /* A helper function that implements the various cast operators. */
552 valpy_do_cast (PyObject
*self
, PyObject
*args
, enum exp_opcode op
)
554 PyObject
*type_obj
, *result
= NULL
;
557 if (! PyArg_ParseTuple (args
, "O", &type_obj
))
560 type
= type_object_to_type (type_obj
);
563 PyErr_SetString (PyExc_RuntimeError
,
564 _("Argument must be a type."));
570 struct value
*val
= ((value_object
*) self
)->value
;
571 struct value
*res_val
;
572 scoped_value_mark free_values
;
574 if (op
== UNOP_DYNAMIC_CAST
)
575 res_val
= value_dynamic_cast (type
, val
);
576 else if (op
== UNOP_REINTERPRET_CAST
)
577 res_val
= value_reinterpret_cast (type
, val
);
580 gdb_assert (op
== UNOP_CAST
);
581 res_val
= value_cast (type
, val
);
584 result
= value_to_value_object (res_val
);
586 CATCH (except
, RETURN_MASK_ALL
)
588 GDB_PY_HANDLE_EXCEPTION (except
);
595 /* Implementation of the "cast" method. */
598 valpy_cast (PyObject
*self
, PyObject
*args
)
600 return valpy_do_cast (self
, args
, UNOP_CAST
);
603 /* Implementation of the "dynamic_cast" method. */
606 valpy_dynamic_cast (PyObject
*self
, PyObject
*args
)
608 return valpy_do_cast (self
, args
, UNOP_DYNAMIC_CAST
);
611 /* Implementation of the "reinterpret_cast" method. */
614 valpy_reinterpret_cast (PyObject
*self
, PyObject
*args
)
616 return valpy_do_cast (self
, args
, UNOP_REINTERPRET_CAST
);
620 valpy_length (PyObject
*self
)
622 /* We don't support getting the number of elements in a struct / class. */
623 PyErr_SetString (PyExc_NotImplementedError
,
624 _("Invalid operation on gdb.Value."));
628 /* Return 1 if the gdb.Field object FIELD is present in the value V.
629 Returns 0 otherwise. If any Python error occurs, -1 is returned. */
632 value_has_field (struct value
*v
, PyObject
*field
)
634 struct type
*parent_type
, *val_type
;
635 enum type_code type_code
;
636 gdbpy_ref
<> type_object (PyObject_GetAttrString (field
, "parent_type"));
639 if (type_object
== NULL
)
642 parent_type
= type_object_to_type (type_object
.get ());
643 if (parent_type
== NULL
)
645 PyErr_SetString (PyExc_TypeError
,
646 _("'parent_type' attribute of gdb.Field object is not a"
647 "gdb.Type object."));
653 val_type
= value_type (v
);
654 val_type
= check_typedef (val_type
);
655 if (TYPE_IS_REFERENCE (val_type
) || TYPE_CODE (val_type
) == TYPE_CODE_PTR
)
656 val_type
= check_typedef (TYPE_TARGET_TYPE (val_type
));
658 type_code
= TYPE_CODE (val_type
);
659 if ((type_code
== TYPE_CODE_STRUCT
|| type_code
== TYPE_CODE_UNION
)
660 && types_equal (val_type
, parent_type
))
665 CATCH (except
, RETURN_MASK_ALL
)
667 GDB_PY_SET_HANDLE_EXCEPTION (except
);
674 /* Return the value of a flag FLAG_NAME in a gdb.Field object FIELD.
675 Returns 1 if the flag value is true, 0 if it is false, and -1 if
676 a Python error occurs. */
679 get_field_flag (PyObject
*field
, const char *flag_name
)
681 gdbpy_ref
<> flag_object (PyObject_GetAttrString (field
, flag_name
));
683 if (flag_object
== NULL
)
686 return PyObject_IsTrue (flag_object
.get ());
689 /* Return the "type" attribute of a gdb.Field object.
690 Returns NULL on error, with a Python exception set. */
693 get_field_type (PyObject
*field
)
695 gdbpy_ref
<> ftype_obj (PyObject_GetAttrString (field
, "type"));
698 if (ftype_obj
== NULL
)
700 ftype
= type_object_to_type (ftype_obj
.get ());
702 PyErr_SetString (PyExc_TypeError
,
703 _("'type' attribute of gdb.Field object is not a "
704 "gdb.Type object."));
709 /* Given string name or a gdb.Field object corresponding to an element inside
710 a structure, return its value object. Returns NULL on error, with a python
714 valpy_getitem (PyObject
*self
, PyObject
*key
)
716 struct gdb_exception except
= exception_none
;
717 value_object
*self_value
= (value_object
*) self
;
718 gdb::unique_xmalloc_ptr
<char> field
;
719 struct type
*base_class_type
= NULL
, *field_type
= NULL
;
721 PyObject
*result
= NULL
;
723 if (gdbpy_is_string (key
))
725 field
= python_string_to_host_string (key
);
729 else if (gdbpy_is_field (key
))
731 int is_base_class
, valid_field
;
733 valid_field
= value_has_field (self_value
->value
, key
);
736 else if (valid_field
== 0)
738 PyErr_SetString (PyExc_TypeError
,
739 _("Invalid lookup for a field not contained in "
745 is_base_class
= get_field_flag (key
, "is_base_class");
746 if (is_base_class
< 0)
748 else if (is_base_class
> 0)
750 base_class_type
= get_field_type (key
);
751 if (base_class_type
== NULL
)
756 gdbpy_ref
<> name_obj (PyObject_GetAttrString (key
, "name"));
758 if (name_obj
== NULL
)
761 if (name_obj
!= Py_None
)
763 field
= python_string_to_host_string (name_obj
.get ());
769 if (!PyObject_HasAttrString (key
, "bitpos"))
771 PyErr_SetString (PyExc_AttributeError
,
772 _("gdb.Field object has no name and no "
773 "'bitpos' attribute."));
777 gdbpy_ref
<> bitpos_obj (PyObject_GetAttrString (key
, "bitpos"));
778 if (bitpos_obj
== NULL
)
780 if (!gdb_py_int_as_long (bitpos_obj
.get (), &bitpos
))
783 field_type
= get_field_type (key
);
784 if (field_type
== NULL
)
792 struct value
*tmp
= self_value
->value
;
793 struct value
*res_val
= NULL
;
794 scoped_value_mark free_values
;
797 res_val
= value_struct_elt (&tmp
, NULL
, field
.get (), NULL
,
798 "struct/class/union");
799 else if (bitpos
>= 0)
800 res_val
= value_struct_elt_bitpos (&tmp
, bitpos
, field_type
,
801 "struct/class/union");
802 else if (base_class_type
!= NULL
)
804 struct type
*val_type
;
806 val_type
= check_typedef (value_type (tmp
));
807 if (TYPE_CODE (val_type
) == TYPE_CODE_PTR
)
808 res_val
= value_cast (lookup_pointer_type (base_class_type
), tmp
);
809 else if (TYPE_CODE (val_type
) == TYPE_CODE_REF
)
810 res_val
= value_cast (lookup_lvalue_reference_type (base_class_type
),
812 else if (TYPE_CODE (val_type
) == TYPE_CODE_RVALUE_REF
)
813 res_val
= value_cast (lookup_rvalue_reference_type (base_class_type
),
816 res_val
= value_cast (base_class_type
, tmp
);
820 /* Assume we are attempting an array access, and let the
821 value code throw an exception if the index has an invalid
823 struct value
*idx
= convert_value_from_python (key
);
827 /* Check the value's type is something that can be accessed via
831 tmp
= coerce_ref (tmp
);
832 type
= check_typedef (value_type (tmp
));
833 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
834 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
835 error (_("Cannot subscript requested type."));
837 res_val
= value_subscript (tmp
, value_as_long (idx
));
842 result
= value_to_value_object (res_val
);
844 CATCH (ex
, RETURN_MASK_ALL
)
850 GDB_PY_HANDLE_EXCEPTION (except
);
856 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
858 PyErr_Format (PyExc_NotImplementedError
,
859 _("Setting of struct elements is not currently supported."));
863 /* Called by the Python interpreter to perform an inferior function
864 call on the value. Returns NULL on error, with a python exception set. */
866 valpy_call (PyObject
*self
, PyObject
*args
, PyObject
*keywords
)
868 Py_ssize_t args_count
;
869 struct value
*function
= ((value_object
*) self
)->value
;
870 struct value
**vargs
= NULL
;
871 struct type
*ftype
= NULL
;
872 PyObject
*result
= NULL
;
876 ftype
= check_typedef (value_type (function
));
878 CATCH (except
, RETURN_MASK_ALL
)
880 GDB_PY_HANDLE_EXCEPTION (except
);
884 if (TYPE_CODE (ftype
) != TYPE_CODE_FUNC
)
886 PyErr_SetString (PyExc_RuntimeError
,
887 _("Value is not callable (not TYPE_CODE_FUNC)."));
891 if (! PyTuple_Check (args
))
893 PyErr_SetString (PyExc_TypeError
,
894 _("Inferior arguments must be provided in a tuple."));
898 args_count
= PyTuple_Size (args
);
903 vargs
= XALLOCAVEC (struct value
*, args_count
);
904 for (i
= 0; i
< args_count
; i
++)
906 PyObject
*item
= PyTuple_GetItem (args
, i
);
911 vargs
[i
] = convert_value_from_python (item
);
912 if (vargs
[i
] == NULL
)
919 scoped_value_mark free_values
;
922 = call_function_by_hand (function
, NULL
,
923 gdb::make_array_view (vargs
, args_count
));
924 result
= value_to_value_object (return_value
);
926 CATCH (except
, RETURN_MASK_ALL
)
928 GDB_PY_HANDLE_EXCEPTION (except
);
935 /* Called by the Python interpreter to obtain string representation
938 valpy_str (PyObject
*self
)
940 struct value_print_options opts
;
942 get_user_print_options (&opts
);
949 common_val_print (((value_object
*) self
)->value
, &stb
, 0,
950 &opts
, python_language
);
952 CATCH (except
, RETURN_MASK_ALL
)
954 GDB_PY_HANDLE_EXCEPTION (except
);
958 return PyUnicode_Decode (stb
.c_str (), stb
.size (), host_charset (), NULL
);
961 /* Implements gdb.Value.is_optimized_out. */
963 valpy_get_is_optimized_out (PyObject
*self
, void *closure
)
965 struct value
*value
= ((value_object
*) self
)->value
;
970 opt
= value_optimized_out (value
);
972 CATCH (except
, RETURN_MASK_ALL
)
974 GDB_PY_HANDLE_EXCEPTION (except
);
984 /* Implements gdb.Value.is_lazy. */
986 valpy_get_is_lazy (PyObject
*self
, void *closure
)
988 struct value
*value
= ((value_object
*) self
)->value
;
993 opt
= value_lazy (value
);
995 CATCH (except
, RETURN_MASK_ALL
)
997 GDB_PY_HANDLE_EXCEPTION (except
);
1007 /* Implements gdb.Value.fetch_lazy (). */
1009 valpy_fetch_lazy (PyObject
*self
, PyObject
*args
)
1011 struct value
*value
= ((value_object
*) self
)->value
;
1015 if (value_lazy (value
))
1016 value_fetch_lazy (value
);
1018 CATCH (except
, RETURN_MASK_ALL
)
1020 GDB_PY_HANDLE_EXCEPTION (except
);
1027 /* Calculate and return the address of the PyObject as the value of
1028 the builtin __hash__ call. */
1030 valpy_hash (PyObject
*self
)
1032 return (intptr_t) self
;
1050 /* If TYPE is a reference, return the target; otherwise return TYPE. */
1051 #define STRIP_REFERENCE(TYPE) \
1052 (TYPE_IS_REFERENCE (TYPE) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
1054 /* Helper for valpy_binop. Returns a value object which is the result
1055 of applying the operation specified by OPCODE to the given
1056 arguments. Throws a GDB exception on error. */
1059 valpy_binop_throw (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
1061 PyObject
*result
= NULL
;
1063 struct value
*arg1
, *arg2
;
1064 struct value
*res_val
= NULL
;
1065 enum exp_opcode op
= OP_NULL
;
1068 scoped_value_mark free_values
;
1070 /* If the gdb.Value object is the second operand, then it will be
1071 passed to us as the OTHER argument, and SELF will be an entirely
1072 different kind of object, altogether. Because of this, we can't
1073 assume self is a gdb.Value object and need to convert it from
1075 arg1
= convert_value_from_python (self
);
1079 arg2
= convert_value_from_python (other
);
1087 struct type
*ltype
= value_type (arg1
);
1088 struct type
*rtype
= value_type (arg2
);
1090 ltype
= check_typedef (ltype
);
1091 ltype
= STRIP_REFERENCE (ltype
);
1092 rtype
= check_typedef (rtype
);
1093 rtype
= STRIP_REFERENCE (rtype
);
1096 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
1097 && is_integral_type (rtype
))
1098 res_val
= value_ptradd (arg1
, value_as_long (arg2
));
1099 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
1100 && is_integral_type (ltype
))
1101 res_val
= value_ptradd (arg2
, value_as_long (arg1
));
1111 struct type
*ltype
= value_type (arg1
);
1112 struct type
*rtype
= value_type (arg2
);
1114 ltype
= check_typedef (ltype
);
1115 ltype
= STRIP_REFERENCE (ltype
);
1116 rtype
= check_typedef (rtype
);
1117 rtype
= STRIP_REFERENCE (rtype
);
1120 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
1121 && TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
1122 /* A ptrdiff_t for the target would be preferable here. */
1123 res_val
= value_from_longest (builtin_type_pyint
,
1124 value_ptrdiff (arg1
, arg2
));
1125 else if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
1126 && is_integral_type (rtype
))
1127 res_val
= value_ptradd (arg1
, - value_as_long (arg2
));
1154 op
= BINOP_BITWISE_AND
;
1157 op
= BINOP_BITWISE_IOR
;
1160 op
= BINOP_BITWISE_XOR
;
1166 if (binop_user_defined_p (op
, arg1
, arg2
))
1167 res_val
= value_x_binop (arg1
, arg2
, op
, OP_NULL
, EVAL_NORMAL
);
1169 res_val
= value_binop (arg1
, arg2
, op
);
1173 result
= value_to_value_object (res_val
);
1178 /* Returns a value object which is the result of applying the operation
1179 specified by OPCODE to the given arguments. Returns NULL on error, with
1180 a python exception set. */
1182 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
1184 PyObject
*result
= NULL
;
1188 result
= valpy_binop_throw (opcode
, self
, other
);
1190 CATCH (except
, RETURN_MASK_ALL
)
1192 GDB_PY_HANDLE_EXCEPTION (except
);
1200 valpy_add (PyObject
*self
, PyObject
*other
)
1202 return valpy_binop (VALPY_ADD
, self
, other
);
1206 valpy_subtract (PyObject
*self
, PyObject
*other
)
1208 return valpy_binop (VALPY_SUB
, self
, other
);
1212 valpy_multiply (PyObject
*self
, PyObject
*other
)
1214 return valpy_binop (VALPY_MUL
, self
, other
);
1218 valpy_divide (PyObject
*self
, PyObject
*other
)
1220 return valpy_binop (VALPY_DIV
, self
, other
);
1224 valpy_remainder (PyObject
*self
, PyObject
*other
)
1226 return valpy_binop (VALPY_REM
, self
, other
);
1230 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
1232 /* We don't support the ternary form of pow. I don't know how to express
1233 that, so let's just throw NotImplementedError to at least do something
1235 if (unused
!= Py_None
)
1237 PyErr_SetString (PyExc_NotImplementedError
,
1238 "Invalid operation on gdb.Value.");
1242 return valpy_binop (VALPY_POW
, self
, other
);
1246 valpy_negative (PyObject
*self
)
1248 PyObject
*result
= NULL
;
1252 /* Perhaps overkill, but consistency has some virtue. */
1253 scoped_value_mark free_values
;
1256 val
= value_neg (((value_object
*) self
)->value
);
1257 result
= value_to_value_object (val
);
1259 CATCH (except
, RETURN_MASK_ALL
)
1261 GDB_PY_HANDLE_EXCEPTION (except
);
1269 valpy_positive (PyObject
*self
)
1271 return value_to_value_object (((value_object
*) self
)->value
);
1275 valpy_absolute (PyObject
*self
)
1277 struct value
*value
= ((value_object
*) self
)->value
;
1282 scoped_value_mark free_values
;
1284 if (value_less (value
, value_zero (value_type (value
), not_lval
)))
1287 CATCH (except
, RETURN_MASK_ALL
)
1289 GDB_PY_HANDLE_EXCEPTION (except
);
1294 return valpy_positive (self
);
1296 return valpy_negative (self
);
1299 /* Implements boolean evaluation of gdb.Value. */
1301 valpy_nonzero (PyObject
*self
)
1303 struct gdb_exception except
= exception_none
;
1304 value_object
*self_value
= (value_object
*) self
;
1306 int nonzero
= 0; /* Appease GCC warning. */
1310 type
= check_typedef (value_type (self_value
->value
));
1312 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
1313 nonzero
= !!value_as_long (self_value
->value
);
1314 else if (is_floating_value (self_value
->value
))
1315 nonzero
= !target_float_is_zero (value_contents (self_value
->value
),
1318 /* All other values are True. */
1321 CATCH (ex
, RETURN_MASK_ALL
)
1327 /* This is not documented in the Python documentation, but if this
1328 function fails, return -1 as slot_nb_nonzero does (the default
1329 Python nonzero function). */
1330 GDB_PY_SET_HANDLE_EXCEPTION (except
);
1335 /* Implements ~ for value objects. */
1337 valpy_invert (PyObject
*self
)
1339 struct value
*val
= NULL
;
1343 val
= value_complement (((value_object
*) self
)->value
);
1345 CATCH (except
, RETURN_MASK_ALL
)
1347 GDB_PY_HANDLE_EXCEPTION (except
);
1351 return value_to_value_object (val
);
1354 /* Implements left shift for value objects. */
1356 valpy_lsh (PyObject
*self
, PyObject
*other
)
1358 return valpy_binop (VALPY_LSH
, self
, other
);
1361 /* Implements right shift for value objects. */
1363 valpy_rsh (PyObject
*self
, PyObject
*other
)
1365 return valpy_binop (VALPY_RSH
, self
, other
);
1368 /* Implements bitwise and for value objects. */
1370 valpy_and (PyObject
*self
, PyObject
*other
)
1372 return valpy_binop (VALPY_BITAND
, self
, other
);
1375 /* Implements bitwise or for value objects. */
1377 valpy_or (PyObject
*self
, PyObject
*other
)
1379 return valpy_binop (VALPY_BITOR
, self
, other
);
1382 /* Implements bitwise xor for value objects. */
1384 valpy_xor (PyObject
*self
, PyObject
*other
)
1386 return valpy_binop (VALPY_BITXOR
, self
, other
);
1389 /* Helper for valpy_richcompare. Implements comparison operations for
1390 value objects. Returns true/false on success. Returns -1 with a
1391 Python exception set if a Python error is detected. Throws a GDB
1392 exception on other errors (memory error, etc.). */
1395 valpy_richcompare_throw (PyObject
*self
, PyObject
*other
, int op
)
1398 struct value
*value_other
;
1399 struct value
*value_self
;
1401 scoped_value_mark free_values
;
1403 value_other
= convert_value_from_python (other
);
1404 if (value_other
== NULL
)
1407 value_self
= ((value_object
*) self
)->value
;
1412 result
= value_less (value_self
, value_other
);
1415 result
= value_less (value_self
, value_other
)
1416 || value_equal (value_self
, value_other
);
1419 result
= value_equal (value_self
, value_other
);
1422 result
= !value_equal (value_self
, value_other
);
1425 result
= value_less (value_other
, value_self
);
1428 result
= (value_less (value_other
, value_self
)
1429 || value_equal (value_self
, value_other
));
1433 PyErr_SetString (PyExc_NotImplementedError
,
1434 _("Invalid operation on gdb.Value."));
1443 /* Implements comparison operations for value objects. Returns NULL on error,
1444 with a python exception set. */
1446 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
1450 if (other
== Py_None
)
1451 /* Comparing with None is special. From what I can tell, in Python
1452 None is smaller than anything else. */
1464 PyErr_SetString (PyExc_NotImplementedError
,
1465 _("Invalid operation on gdb.Value."));
1471 result
= valpy_richcompare_throw (self
, other
, op
);
1473 CATCH (except
, RETURN_MASK_ALL
)
1475 GDB_PY_HANDLE_EXCEPTION (except
);
1479 /* In this case, the Python exception has already been set. */
1490 /* Implements conversion to int. */
1492 valpy_int (PyObject
*self
)
1494 struct value
*value
= ((value_object
*) self
)->value
;
1495 struct type
*type
= value_type (value
);
1500 if (is_floating_value (value
))
1502 type
= builtin_type_pylong
;
1503 value
= value_cast (type
, value
);
1506 if (!is_integral_type (type
)
1507 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
1508 error (_("Cannot convert value to int."));
1510 l
= value_as_long (value
);
1512 CATCH (except
, RETURN_MASK_ALL
)
1514 GDB_PY_HANDLE_EXCEPTION (except
);
1518 if (TYPE_UNSIGNED (type
))
1519 return gdb_py_object_from_ulongest (l
).release ();
1521 return gdb_py_object_from_longest (l
).release ();
1525 /* Implements conversion to long. */
1527 valpy_long (PyObject
*self
)
1529 struct value
*value
= ((value_object
*) self
)->value
;
1530 struct type
*type
= value_type (value
);
1535 if (is_floating_value (value
))
1537 type
= builtin_type_pylong
;
1538 value
= value_cast (type
, value
);
1541 type
= check_typedef (type
);
1543 if (!is_integral_type (type
)
1544 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
1545 error (_("Cannot convert value to long."));
1547 l
= value_as_long (value
);
1549 CATCH (except
, RETURN_MASK_ALL
)
1551 GDB_PY_HANDLE_EXCEPTION (except
);
1555 if (TYPE_UNSIGNED (type
))
1556 return gdb_py_long_from_ulongest (l
);
1558 return gdb_py_long_from_longest (l
);
1561 /* Implements conversion to float. */
1563 valpy_float (PyObject
*self
)
1565 struct value
*value
= ((value_object
*) self
)->value
;
1566 struct type
*type
= value_type (value
);
1571 type
= check_typedef (type
);
1573 if (TYPE_CODE (type
) == TYPE_CODE_FLT
&& is_floating_value (value
))
1574 d
= target_float_to_host_double (value_contents (value
), type
);
1575 else if (TYPE_CODE (type
) == TYPE_CODE_INT
)
1577 /* Note that valpy_long accepts TYPE_CODE_PTR and some
1578 others here here -- but casting a pointer or bool to a
1579 float seems wrong. */
1580 d
= value_as_long (value
);
1583 error (_("Cannot convert value to float."));
1585 CATCH (except
, RETURN_MASK_ALL
)
1587 GDB_PY_HANDLE_EXCEPTION (except
);
1591 return PyFloat_FromDouble (d
);
1594 /* Returns an object for a value which is released from the all_values chain,
1595 so its lifetime is not bound to the execution of a command. */
1597 value_to_value_object (struct value
*val
)
1599 value_object
*val_obj
;
1601 val_obj
= PyObject_New (value_object
, &value_object_type
);
1602 if (val_obj
!= NULL
)
1604 val_obj
->value
= release_value (val
).release ();
1605 val_obj
->address
= NULL
;
1606 val_obj
->type
= NULL
;
1607 val_obj
->dynamic_type
= NULL
;
1608 note_value (val_obj
);
1611 return (PyObject
*) val_obj
;
1614 /* Returns a borrowed reference to the struct value corresponding to
1615 the given value object. */
1617 value_object_to_value (PyObject
*self
)
1621 if (! PyObject_TypeCheck (self
, &value_object_type
))
1623 real
= (value_object
*) self
;
1627 /* Try to convert a Python value to a gdb value. If the value cannot
1628 be converted, set a Python exception and return NULL. Returns a
1629 reference to a new value on the all_values chain. */
1632 convert_value_from_python (PyObject
*obj
)
1634 struct value
*value
= NULL
; /* -Wall */
1637 gdb_assert (obj
!= NULL
);
1641 if (PyBool_Check (obj
))
1643 cmp
= PyObject_IsTrue (obj
);
1645 value
= value_from_longest (builtin_type_pybool
, cmp
);
1647 /* Make a long logic check first. In Python 3.x, internally,
1648 all integers are represented as longs. In Python 2.x, there
1649 is still a differentiation internally between a PyInt and a
1650 PyLong. Explicitly do this long check conversion first. In
1651 GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
1652 to be done first to ensure we do not lose information in the
1653 conversion process. */
1654 else if (PyLong_Check (obj
))
1656 LONGEST l
= PyLong_AsLongLong (obj
);
1658 if (PyErr_Occurred ())
1660 /* If the error was an overflow, we can try converting to
1661 ULONGEST instead. */
1662 if (PyErr_ExceptionMatches (PyExc_OverflowError
))
1664 PyObject
*etype
, *evalue
, *etraceback
;
1666 PyErr_Fetch (&etype
, &evalue
, &etraceback
);
1667 gdbpy_ref
<> zero (PyInt_FromLong (0));
1669 /* Check whether obj is positive. */
1670 if (PyObject_RichCompareBool (obj
, zero
.get (), Py_GT
) > 0)
1674 ul
= PyLong_AsUnsignedLongLong (obj
);
1675 if (! PyErr_Occurred ())
1676 value
= value_from_ulongest (builtin_type_upylong
, ul
);
1679 /* There's nothing we can do. */
1680 PyErr_Restore (etype
, evalue
, etraceback
);
1684 value
= value_from_longest (builtin_type_pylong
, l
);
1686 #if PY_MAJOR_VERSION == 2
1687 else if (PyInt_Check (obj
))
1689 long l
= PyInt_AsLong (obj
);
1691 if (! PyErr_Occurred ())
1692 value
= value_from_longest (builtin_type_pyint
, l
);
1695 else if (PyFloat_Check (obj
))
1697 double d
= PyFloat_AsDouble (obj
);
1699 if (! PyErr_Occurred ())
1701 value
= allocate_value (builtin_type_pyfloat
);
1702 target_float_from_host_double (value_contents_raw (value
),
1703 value_type (value
), d
);
1706 else if (gdbpy_is_string (obj
))
1708 gdb::unique_xmalloc_ptr
<char> s
1709 = python_string_to_target_string (obj
);
1711 value
= value_cstring (s
.get (), strlen (s
.get ()),
1712 builtin_type_pychar
);
1714 else if (PyObject_TypeCheck (obj
, &value_object_type
))
1715 value
= value_copy (((value_object
*) obj
)->value
);
1716 else if (gdbpy_is_lazy_string (obj
))
1720 result
= PyObject_CallMethodObjArgs (obj
, gdbpy_value_cst
, NULL
);
1721 value
= value_copy (((value_object
*) result
)->value
);
1725 PyErr_Format (PyExc_TypeError
,
1726 _("Could not convert Python object: %S."), obj
);
1728 PyErr_Format (PyExc_TypeError
,
1729 _("Could not convert Python object: %s."),
1730 PyString_AsString (PyObject_Str (obj
)));
1733 CATCH (except
, RETURN_MASK_ALL
)
1735 gdbpy_convert_exception (except
);
1743 /* Returns value object in the ARGth position in GDB's history. */
1745 gdbpy_history (PyObject
*self
, PyObject
*args
)
1748 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
1750 if (!PyArg_ParseTuple (args
, "i", &i
))
1755 res_val
= access_value_history (i
);
1757 CATCH (except
, RETURN_MASK_ALL
)
1759 GDB_PY_HANDLE_EXCEPTION (except
);
1763 return value_to_value_object (res_val
);
1766 /* Return the value of a convenience variable. */
1768 gdbpy_convenience_variable (PyObject
*self
, PyObject
*args
)
1770 const char *varname
;
1771 struct value
*res_val
= NULL
;
1773 if (!PyArg_ParseTuple (args
, "s", &varname
))
1778 struct internalvar
*var
= lookup_only_internalvar (varname
);
1782 res_val
= value_of_internalvar (python_gdbarch
, var
);
1783 if (TYPE_CODE (value_type (res_val
)) == TYPE_CODE_VOID
)
1787 CATCH (except
, RETURN_MASK_ALL
)
1789 GDB_PY_HANDLE_EXCEPTION (except
);
1793 if (res_val
== NULL
)
1796 return value_to_value_object (res_val
);
1799 /* Set the value of a convenience variable. */
1801 gdbpy_set_convenience_variable (PyObject
*self
, PyObject
*args
)
1803 const char *varname
;
1804 PyObject
*value_obj
;
1805 struct value
*value
= NULL
;
1807 if (!PyArg_ParseTuple (args
, "sO", &varname
, &value_obj
))
1810 /* None means to clear the variable. */
1811 if (value_obj
!= Py_None
)
1813 value
= convert_value_from_python (value_obj
);
1822 struct internalvar
*var
= lookup_only_internalvar (varname
);
1825 clear_internalvar (var
);
1829 struct internalvar
*var
= lookup_internalvar (varname
);
1831 set_internalvar (var
, value
);
1834 CATCH (except
, RETURN_MASK_ALL
)
1836 GDB_PY_HANDLE_EXCEPTION (except
);
1843 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
1846 gdbpy_is_value_object (PyObject
*obj
)
1848 return PyObject_TypeCheck (obj
, &value_object_type
);
1852 gdbpy_initialize_values (void)
1854 if (PyType_Ready (&value_object_type
) < 0)
1857 return gdb_pymodule_addobject (gdb_module
, "Value",
1858 (PyObject
*) &value_object_type
);
1863 static gdb_PyGetSetDef value_object_getset
[] = {
1864 { "address", valpy_get_address
, NULL
, "The address of the value.",
1866 { "is_optimized_out", valpy_get_is_optimized_out
, NULL
,
1867 "Boolean telling whether the value is optimized "
1868 "out (i.e., not available).",
1870 { "type", valpy_get_type
, NULL
, "Type of the value.", NULL
},
1871 { "dynamic_type", valpy_get_dynamic_type
, NULL
,
1872 "Dynamic type of the value.", NULL
},
1873 { "is_lazy", valpy_get_is_lazy
, NULL
,
1874 "Boolean telling whether the value is lazy (not fetched yet\n\
1875 from the inferior). A lazy value is fetched when needed, or when\n\
1876 the \"fetch_lazy()\" method is called.", NULL
},
1877 {NULL
} /* Sentinel */
1880 static PyMethodDef value_object_methods
[] = {
1881 { "cast", valpy_cast
, METH_VARARGS
, "Cast the value to the supplied type." },
1882 { "dynamic_cast", valpy_dynamic_cast
, METH_VARARGS
,
1883 "dynamic_cast (gdb.Type) -> gdb.Value\n\
1884 Cast the value to the supplied type, as if by the C++ dynamic_cast operator."
1886 { "reinterpret_cast", valpy_reinterpret_cast
, METH_VARARGS
,
1887 "reinterpret_cast (gdb.Type) -> gdb.Value\n\
1888 Cast the value to the supplied type, as if by the C++\n\
1889 reinterpret_cast operator."
1891 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
1892 { "referenced_value", valpy_referenced_value
, METH_NOARGS
,
1893 "Return the value referenced by a TYPE_CODE_REF or TYPE_CODE_PTR value." },
1894 { "reference_value", valpy_lvalue_reference_value
, METH_NOARGS
,
1895 "Return a value of type TYPE_CODE_REF referencing this value." },
1896 { "rvalue_reference_value", valpy_rvalue_reference_value
, METH_NOARGS
,
1897 "Return a value of type TYPE_CODE_RVALUE_REF referencing this value." },
1898 { "const_value", valpy_const_value
, METH_NOARGS
,
1899 "Return a 'const' qualied version of the same value." },
1900 { "lazy_string", (PyCFunction
) valpy_lazy_string
,
1901 METH_VARARGS
| METH_KEYWORDS
,
1902 "lazy_string ([encoding] [, length]) -> lazy_string\n\
1903 Return a lazy string representation of the value." },
1904 { "string", (PyCFunction
) valpy_string
, METH_VARARGS
| METH_KEYWORDS
,
1905 "string ([encoding] [, errors] [, length]) -> string\n\
1906 Return Unicode string representation of the value." },
1907 { "fetch_lazy", valpy_fetch_lazy
, METH_NOARGS
,
1908 "Fetches the value from the inferior, if it was lazy." },
1909 {NULL
} /* Sentinel */
1912 static PyNumberMethods value_object_as_number
= {
1920 NULL
, /* nb_divmod */
1921 valpy_power
, /* nb_power */
1922 valpy_negative
, /* nb_negative */
1923 valpy_positive
, /* nb_positive */
1924 valpy_absolute
, /* nb_absolute */
1925 valpy_nonzero
, /* nb_nonzero */
1926 valpy_invert
, /* nb_invert */
1927 valpy_lsh
, /* nb_lshift */
1928 valpy_rsh
, /* nb_rshift */
1929 valpy_and
, /* nb_and */
1930 valpy_xor
, /* nb_xor */
1931 valpy_or
, /* nb_or */
1933 valpy_long
, /* nb_int */
1934 NULL
, /* reserved */
1936 NULL
, /* nb_coerce */
1937 valpy_int
, /* nb_int */
1938 valpy_long
, /* nb_long */
1940 valpy_float
, /* nb_float */
1945 NULL
, /* nb_inplace_add */
1946 NULL
, /* nb_inplace_subtract */
1947 NULL
, /* nb_inplace_multiply */
1949 NULL
, /* nb_inplace_divide */
1951 NULL
, /* nb_inplace_remainder */
1952 NULL
, /* nb_inplace_power */
1953 NULL
, /* nb_inplace_lshift */
1954 NULL
, /* nb_inplace_rshift */
1955 NULL
, /* nb_inplace_and */
1956 NULL
, /* nb_inplace_xor */
1957 NULL
, /* nb_inplace_or */
1958 NULL
, /* nb_floor_divide */
1959 valpy_divide
, /* nb_true_divide */
1960 NULL
, /* nb_inplace_floor_divide */
1961 NULL
, /* nb_inplace_true_divide */
1962 #ifndef HAVE_LIBPYTHON2_4
1963 /* This was added in Python 2.5. */
1964 valpy_long
, /* nb_index */
1965 #endif /* HAVE_LIBPYTHON2_4 */
1968 static PyMappingMethods value_object_as_mapping
= {
1974 PyTypeObject value_object_type
= {
1975 PyVarObject_HEAD_INIT (NULL
, 0)
1976 "gdb.Value", /*tp_name*/
1977 sizeof (value_object
), /*tp_basicsize*/
1979 valpy_dealloc
, /*tp_dealloc*/
1985 &value_object_as_number
, /*tp_as_number*/
1986 0, /*tp_as_sequence*/
1987 &value_object_as_mapping
, /*tp_as_mapping*/
1988 valpy_hash
, /*tp_hash*/
1989 valpy_call
, /*tp_call*/
1990 valpy_str
, /*tp_str*/
1994 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
1995 | Py_TPFLAGS_BASETYPE
, /*tp_flags*/
1996 "GDB value object", /* tp_doc */
1997 0, /* tp_traverse */
1999 valpy_richcompare
, /* tp_richcompare */
2000 0, /* tp_weaklistoffset */
2002 0, /* tp_iternext */
2003 value_object_methods
, /* tp_methods */
2005 value_object_getset
, /* tp_getset */
2008 0, /* tp_descr_get */
2009 0, /* tp_descr_set */
2010 0, /* tp_dictoffset */
2013 valpy_new
/* tp_new */