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 #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
;
213 obj
->type
= type_to_type_object (value_type (obj
->value
));
217 Py_INCREF (obj
->type
);
220 Py_INCREF (obj
->type
);
224 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
225 string. Return a PyObject representing a lazy_string_object type.
226 A lazy string is a pointer to a string with an optional encoding and
227 length. If ENCODING is not given, encoding is set to None. If an
228 ENCODING is provided the encoding parameter is set to ENCODING, but
229 the string is not encoded. If LENGTH is provided then the length
230 parameter is set to LENGTH, otherwise length will be set to -1 (first
231 null of appropriate with). */
233 valpy_lazy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
236 struct value
*value
= ((value_object
*) self
)->value
;
237 const char *user_encoding
= NULL
;
238 static char *keywords
[] = { "encoding", "length", NULL
};
241 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|si", keywords
,
242 &user_encoding
, &length
))
245 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_PTR
)
246 value
= value_ind (value
);
248 str_obj
= gdbpy_create_lazy_string_object (value_address (value
), length
,
249 user_encoding
, value_type (value
));
251 return (PyObject
*) str_obj
;
254 /* Implementation of gdb.Value.string ([encoding] [, errors]
255 [, length]) -> string. Return Unicode string with value contents.
256 If ENCODING is not given, the string is assumed to be encoded in
257 the target's charset. If LENGTH is provided, only fetch string to
258 the length provided. */
261 valpy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
265 struct value
*value
= ((value_object
*) self
)->value
;
266 volatile struct gdb_exception except
;
268 const char *encoding
= NULL
;
269 const char *errors
= NULL
;
270 const char *user_encoding
= NULL
;
271 const char *la_encoding
= NULL
;
272 struct type
*char_type
;
273 static char *keywords
[] = { "encoding", "errors", "length", NULL
};
275 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|ssi", keywords
,
276 &user_encoding
, &errors
, &length
))
279 TRY_CATCH (except
, RETURN_MASK_ALL
)
281 LA_GET_STRING (value
, &buffer
, &length
, &char_type
, &la_encoding
);
283 GDB_PY_HANDLE_EXCEPTION (except
);
285 encoding
= (user_encoding
&& *user_encoding
) ? user_encoding
: la_encoding
;
286 unicode
= PyUnicode_Decode (buffer
, length
* TYPE_LENGTH (char_type
),
293 /* Cast a value to a given type. */
295 valpy_cast (PyObject
*self
, PyObject
*args
)
299 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
300 volatile struct gdb_exception except
;
302 if (! PyArg_ParseTuple (args
, "O", &type_obj
))
305 type
= type_object_to_type (type_obj
);
308 PyErr_SetString (PyExc_RuntimeError
,
309 _("Argument must be a type."));
313 TRY_CATCH (except
, RETURN_MASK_ALL
)
315 res_val
= value_cast (type
, ((value_object
*) self
)->value
);
317 GDB_PY_HANDLE_EXCEPTION (except
);
319 return value_to_value_object (res_val
);
323 valpy_length (PyObject
*self
)
325 /* We don't support getting the number of elements in a struct / class. */
326 PyErr_SetString (PyExc_NotImplementedError
,
327 _("Invalid operation on gdb.Value."));
331 /* Given string name of an element inside structure, return its value
334 valpy_getitem (PyObject
*self
, PyObject
*key
)
336 value_object
*self_value
= (value_object
*) self
;
338 struct value
*res_val
= NULL
;
339 volatile struct gdb_exception except
;
341 if (gdbpy_is_string (key
))
343 field
= python_string_to_host_string (key
);
348 TRY_CATCH (except
, RETURN_MASK_ALL
)
350 struct value
*tmp
= self_value
->value
;
353 res_val
= value_struct_elt (&tmp
, NULL
, field
, 0, NULL
);
356 /* Assume we are attempting an array access, and let the
357 value code throw an exception if the index has an invalid
359 struct value
*idx
= convert_value_from_python (key
);
363 /* Check the value's type is something that can be accessed via
367 tmp
= coerce_ref (tmp
);
368 type
= check_typedef (value_type (tmp
));
369 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
370 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
371 error( _("Cannot subscript requested type."));
373 res_val
= value_subscript (tmp
, value_as_long (idx
));
379 GDB_PY_HANDLE_EXCEPTION (except
);
381 return res_val
? value_to_value_object (res_val
) : NULL
;
385 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
387 PyErr_Format (PyExc_NotImplementedError
,
388 _("Setting of struct elements is not currently supported."));
392 /* Called by the Python interpreter to obtain string representation
395 valpy_str (PyObject
*self
)
399 struct cleanup
*old_chain
;
401 struct value_print_options opts
;
402 volatile struct gdb_exception except
;
404 get_user_print_options (&opts
);
407 stb
= mem_fileopen ();
408 old_chain
= make_cleanup_ui_file_delete (stb
);
410 TRY_CATCH (except
, RETURN_MASK_ALL
)
412 common_val_print (((value_object
*) self
)->value
, stb
, 0,
413 &opts
, python_language
);
414 s
= ui_file_xstrdup (stb
, NULL
);
416 GDB_PY_HANDLE_EXCEPTION (except
);
418 do_cleanups (old_chain
);
420 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
426 /* Implements gdb.Value.is_optimized_out. */
428 valpy_get_is_optimized_out (PyObject
*self
, void *closure
)
430 struct value
*value
= ((value_object
*) self
)->value
;
432 if (value_optimized_out (value
))
438 /* Calculate and return the address of the PyObject as the value of
439 the builtin __hash__ call. */
441 valpy_hash (PyObject
*self
)
443 return (long) (intptr_t) self
;
461 /* If TYPE is a reference, return the target; otherwise return TYPE. */
462 #define STRIP_REFERENCE(TYPE) \
463 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
465 /* Returns a value object which is the result of applying the operation
466 specified by OPCODE to the given arguments. */
468 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
470 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
471 volatile struct gdb_exception except
;
473 TRY_CATCH (except
, RETURN_MASK_ALL
)
475 struct value
*arg1
, *arg2
;
477 /* If the gdb.Value object is the second operand, then it will be passed
478 to us as the OTHER argument, and SELF will be an entirely different
479 kind of object, altogether. Because of this, we can't assume self is
480 a gdb.Value object and need to convert it from python as well. */
481 arg1
= convert_value_from_python (self
);
485 arg2
= convert_value_from_python (other
);
493 struct type
*ltype
= value_type (arg1
);
494 struct type
*rtype
= value_type (arg2
);
496 CHECK_TYPEDEF (ltype
);
497 ltype
= STRIP_REFERENCE (ltype
);
498 CHECK_TYPEDEF (rtype
);
499 rtype
= STRIP_REFERENCE (rtype
);
501 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
502 && is_integral_type (rtype
))
503 res_val
= value_ptradd (arg1
, value_as_long (arg2
));
504 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
505 && is_integral_type (ltype
))
506 res_val
= value_ptradd (arg2
, value_as_long (arg1
));
508 res_val
= value_binop (arg1
, arg2
, BINOP_ADD
);
513 struct type
*ltype
= value_type (arg1
);
514 struct type
*rtype
= value_type (arg2
);
516 CHECK_TYPEDEF (ltype
);
517 ltype
= STRIP_REFERENCE (ltype
);
518 CHECK_TYPEDEF (rtype
);
519 rtype
= STRIP_REFERENCE (rtype
);
521 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
522 && TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
523 /* A ptrdiff_t for the target would be preferable here. */
524 res_val
= value_from_longest (builtin_type_pyint
,
525 value_ptrdiff (arg1
, arg2
));
526 else if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
527 && is_integral_type (rtype
))
528 res_val
= value_ptradd (arg1
, - value_as_long (arg2
));
530 res_val
= value_binop (arg1
, arg2
, BINOP_SUB
);
534 res_val
= value_binop (arg1
, arg2
, BINOP_MUL
);
537 res_val
= value_binop (arg1
, arg2
, BINOP_DIV
);
540 res_val
= value_binop (arg1
, arg2
, BINOP_REM
);
543 res_val
= value_binop (arg1
, arg2
, BINOP_EXP
);
546 res_val
= value_binop (arg1
, arg2
, BINOP_LSH
);
549 res_val
= value_binop (arg1
, arg2
, BINOP_RSH
);
552 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_AND
);
555 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_IOR
);
558 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_XOR
);
562 GDB_PY_HANDLE_EXCEPTION (except
);
564 return res_val
? value_to_value_object (res_val
) : NULL
;
568 valpy_add (PyObject
*self
, PyObject
*other
)
570 return valpy_binop (VALPY_ADD
, self
, other
);
574 valpy_subtract (PyObject
*self
, PyObject
*other
)
576 return valpy_binop (VALPY_SUB
, self
, other
);
580 valpy_multiply (PyObject
*self
, PyObject
*other
)
582 return valpy_binop (VALPY_MUL
, self
, other
);
586 valpy_divide (PyObject
*self
, PyObject
*other
)
588 return valpy_binop (VALPY_DIV
, self
, other
);
592 valpy_remainder (PyObject
*self
, PyObject
*other
)
594 return valpy_binop (VALPY_REM
, self
, other
);
598 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
600 /* We don't support the ternary form of pow. I don't know how to express
601 that, so let's just throw NotImplementedError to at least do something
603 if (unused
!= Py_None
)
605 PyErr_SetString (PyExc_NotImplementedError
,
606 "Invalid operation on gdb.Value.");
610 return valpy_binop (VALPY_POW
, self
, other
);
614 valpy_negative (PyObject
*self
)
616 struct value
*val
= NULL
;
617 volatile struct gdb_exception except
;
619 TRY_CATCH (except
, RETURN_MASK_ALL
)
621 val
= value_neg (((value_object
*) self
)->value
);
623 GDB_PY_HANDLE_EXCEPTION (except
);
625 return value_to_value_object (val
);
629 valpy_positive (PyObject
*self
)
631 return value_to_value_object (((value_object
*) self
)->value
);
635 valpy_absolute (PyObject
*self
)
637 struct value
*value
= ((value_object
*) self
)->value
;
639 if (value_less (value
, value_zero (value_type (value
), not_lval
)))
640 return valpy_negative (self
);
642 return valpy_positive (self
);
645 /* Implements boolean evaluation of gdb.Value. */
647 valpy_nonzero (PyObject
*self
)
649 value_object
*self_value
= (value_object
*) self
;
652 type
= check_typedef (value_type (self_value
->value
));
654 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
655 return !!value_as_long (self_value
->value
);
656 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
657 return value_as_double (self_value
->value
) != 0;
658 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
659 return !decimal_is_zero (value_contents (self_value
->value
),
661 gdbarch_byte_order (get_type_arch (type
)));
664 PyErr_SetString (PyExc_TypeError
, _("Attempted truth testing on invalid "
670 /* Implements ~ for value objects. */
672 valpy_invert (PyObject
*self
)
674 struct value
*val
= NULL
;
675 volatile struct gdb_exception except
;
677 TRY_CATCH (except
, RETURN_MASK_ALL
)
679 val
= value_complement (((value_object
*) self
)->value
);
681 GDB_PY_HANDLE_EXCEPTION (except
);
683 return value_to_value_object (val
);
686 /* Implements left shift for value objects. */
688 valpy_lsh (PyObject
*self
, PyObject
*other
)
690 return valpy_binop (VALPY_LSH
, self
, other
);
693 /* Implements right shift for value objects. */
695 valpy_rsh (PyObject
*self
, PyObject
*other
)
697 return valpy_binop (VALPY_RSH
, self
, other
);
700 /* Implements bitwise and for value objects. */
702 valpy_and (PyObject
*self
, PyObject
*other
)
704 return valpy_binop (VALPY_BITAND
, self
, other
);
707 /* Implements bitwise or for value objects. */
709 valpy_or (PyObject
*self
, PyObject
*other
)
711 return valpy_binop (VALPY_BITOR
, self
, other
);
714 /* Implements bitwise xor for value objects. */
716 valpy_xor (PyObject
*self
, PyObject
*other
)
718 return valpy_binop (VALPY_BITXOR
, self
, other
);
721 /* Implements comparison operations for value objects. */
723 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
726 struct value
*value_other
;
727 volatile struct gdb_exception except
;
729 if (other
== Py_None
)
730 /* Comparing with None is special. From what I can tell, in Python
731 None is smaller than anything else. */
743 PyErr_SetString (PyExc_NotImplementedError
,
744 _("Invalid operation on gdb.Value."));
748 TRY_CATCH (except
, RETURN_MASK_ALL
)
750 value_other
= convert_value_from_python (other
);
751 if (value_other
== NULL
)
759 result
= value_less (((value_object
*) self
)->value
, value_other
);
762 result
= value_less (((value_object
*) self
)->value
, value_other
)
763 || value_equal (((value_object
*) self
)->value
, value_other
);
766 result
= value_equal (((value_object
*) self
)->value
, value_other
);
769 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
772 result
= value_less (value_other
, ((value_object
*) self
)->value
);
775 result
= value_less (value_other
, ((value_object
*) self
)->value
)
776 || value_equal (((value_object
*) self
)->value
, value_other
);
780 PyErr_SetString (PyExc_NotImplementedError
,
781 _("Invalid operation on gdb.Value."));
786 GDB_PY_HANDLE_EXCEPTION (except
);
788 /* In this case, the Python exception has already been set. */
798 /* Helper function to determine if a type is "int-like". */
800 is_intlike (struct type
*type
, int ptr_ok
)
802 CHECK_TYPEDEF (type
);
803 return (TYPE_CODE (type
) == TYPE_CODE_INT
804 || TYPE_CODE (type
) == TYPE_CODE_ENUM
805 || TYPE_CODE (type
) == TYPE_CODE_BOOL
806 || TYPE_CODE (type
) == TYPE_CODE_CHAR
807 || (ptr_ok
&& TYPE_CODE (type
) == TYPE_CODE_PTR
));
810 /* Implements conversion to int. */
812 valpy_int (PyObject
*self
)
814 struct value
*value
= ((value_object
*) self
)->value
;
815 struct type
*type
= value_type (value
);
817 volatile struct gdb_exception except
;
819 CHECK_TYPEDEF (type
);
820 if (!is_intlike (type
, 0))
822 PyErr_SetString (PyExc_RuntimeError
,
823 _("Cannot convert value to int."));
827 TRY_CATCH (except
, RETURN_MASK_ALL
)
829 l
= value_as_long (value
);
831 GDB_PY_HANDLE_EXCEPTION (except
);
833 #ifdef HAVE_LONG_LONG /* Defined by Python. */
834 /* If we have 'long long', and the value overflows a 'long', use a
835 Python Long; otherwise use a Python Int. */
836 if (sizeof (l
) > sizeof (long) && (l
> PyInt_GetMax ()
837 || l
< (- (LONGEST
) PyInt_GetMax ()) - 1))
838 return PyLong_FromLongLong (l
);
840 return PyInt_FromLong (l
);
843 /* Implements conversion to long. */
845 valpy_long (PyObject
*self
)
847 struct value
*value
= ((value_object
*) self
)->value
;
848 struct type
*type
= value_type (value
);
850 volatile struct gdb_exception except
;
852 if (!is_intlike (type
, 1))
854 PyErr_SetString (PyExc_RuntimeError
,
855 _("Cannot convert value to long."));
859 TRY_CATCH (except
, RETURN_MASK_ALL
)
861 l
= value_as_long (value
);
863 GDB_PY_HANDLE_EXCEPTION (except
);
865 #ifdef HAVE_LONG_LONG /* Defined by Python. */
866 return PyLong_FromLongLong (l
);
868 return PyLong_FromLong (l
);
872 /* Implements conversion to float. */
874 valpy_float (PyObject
*self
)
876 struct value
*value
= ((value_object
*) self
)->value
;
877 struct type
*type
= value_type (value
);
879 volatile struct gdb_exception except
;
881 CHECK_TYPEDEF (type
);
882 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
884 PyErr_SetString (PyExc_RuntimeError
,
885 _("Cannot convert value to float."));
889 TRY_CATCH (except
, RETURN_MASK_ALL
)
891 d
= value_as_double (value
);
893 GDB_PY_HANDLE_EXCEPTION (except
);
895 return PyFloat_FromDouble (d
);
898 /* Returns an object for a value which is released from the all_values chain,
899 so its lifetime is not bound to the execution of a command. */
901 value_to_value_object (struct value
*val
)
903 value_object
*val_obj
;
905 val_obj
= PyObject_New (value_object
, &value_object_type
);
908 val_obj
->value
= val
;
910 val_obj
->address
= NULL
;
911 val_obj
->type
= NULL
;
912 note_value (val_obj
);
915 return (PyObject
*) val_obj
;
918 /* Returns a borrowed reference to the struct value corresponding to
919 the given value object. */
921 value_object_to_value (PyObject
*self
)
925 if (! PyObject_TypeCheck (self
, &value_object_type
))
927 real
= (value_object
*) self
;
931 /* Try to convert a Python value to a gdb value. If the value cannot
932 be converted, set a Python exception and return NULL. Returns a
933 reference to a new value on the all_values chain. */
936 convert_value_from_python (PyObject
*obj
)
938 struct value
*value
= NULL
; /* -Wall */
940 volatile struct gdb_exception except
;
943 gdb_assert (obj
!= NULL
);
945 TRY_CATCH (except
, RETURN_MASK_ALL
)
947 if (PyBool_Check (obj
))
949 cmp
= PyObject_IsTrue (obj
);
951 value
= value_from_longest (builtin_type_pybool
, cmp
);
953 else if (PyInt_Check (obj
))
955 long l
= PyInt_AsLong (obj
);
957 if (! PyErr_Occurred ())
958 value
= value_from_longest (builtin_type_pyint
, l
);
960 else if (PyLong_Check (obj
))
962 LONGEST l
= PyLong_AsLongLong (obj
);
964 if (! PyErr_Occurred ())
965 value
= value_from_longest (builtin_type_pylong
, l
);
967 else if (PyFloat_Check (obj
))
969 double d
= PyFloat_AsDouble (obj
);
971 if (! PyErr_Occurred ())
972 value
= value_from_double (builtin_type_pyfloat
, d
);
974 else if (gdbpy_is_string (obj
))
978 s
= python_string_to_target_string (obj
);
981 old
= make_cleanup (xfree
, s
);
982 value
= value_cstring (s
, strlen (s
), builtin_type_pychar
);
986 else if (PyObject_TypeCheck (obj
, &value_object_type
))
987 value
= value_copy (((value_object
*) obj
)->value
);
988 else if (gdbpy_is_lazy_string (obj
))
991 PyObject
*function
= PyString_FromString ("value");
993 result
= PyObject_CallMethodObjArgs (obj
, function
, NULL
);
994 value
= value_copy (((value_object
*) result
)->value
);
997 PyErr_Format (PyExc_TypeError
, _("Could not convert Python object: %s."),
998 PyString_AsString (PyObject_Str (obj
)));
1000 if (except
.reason
< 0)
1002 PyErr_Format (except
.reason
== RETURN_QUIT
1003 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
1004 "%s", except
.message
);
1011 /* Returns value object in the ARGth position in GDB's history. */
1013 gdbpy_history (PyObject
*self
, PyObject
*args
)
1016 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
1017 volatile struct gdb_exception except
;
1019 if (!PyArg_ParseTuple (args
, "i", &i
))
1022 TRY_CATCH (except
, RETURN_MASK_ALL
)
1024 res_val
= access_value_history (i
);
1026 GDB_PY_HANDLE_EXCEPTION (except
);
1028 return value_to_value_object (res_val
);
1032 gdbpy_initialize_values (void)
1034 if (PyType_Ready (&value_object_type
) < 0)
1037 Py_INCREF (&value_object_type
);
1038 PyModule_AddObject (gdb_module
, "Value", (PyObject
*) &value_object_type
);
1040 values_in_python
= NULL
;
1045 static PyGetSetDef value_object_getset
[] = {
1046 { "address", valpy_get_address
, NULL
, "The address of the value.",
1048 { "is_optimized_out", valpy_get_is_optimized_out
, NULL
,
1049 "Boolean telling whether the value is optimized out (i.e., not available).",
1051 { "type", valpy_get_type
, NULL
, "Type of the value.", NULL
},
1052 {NULL
} /* Sentinel */
1055 static PyMethodDef value_object_methods
[] = {
1056 { "cast", valpy_cast
, METH_VARARGS
, "Cast the value to the supplied type." },
1057 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
1058 { "lazy_string", (PyCFunction
) valpy_lazy_string
, METH_VARARGS
| METH_KEYWORDS
,
1059 "lazy_string ([encoding] [, length]) -> lazy_string\n\
1060 Return a lazy string representation of the value." },
1061 { "string", (PyCFunction
) valpy_string
, METH_VARARGS
| METH_KEYWORDS
,
1062 "string ([encoding] [, errors] [, length]) -> string\n\
1063 Return Unicode string representation of the value." },
1064 {NULL
} /* Sentinel */
1067 static PyNumberMethods value_object_as_number
= {
1073 NULL
, /* nb_divmod */
1074 valpy_power
, /* nb_power */
1075 valpy_negative
, /* nb_negative */
1076 valpy_positive
, /* nb_positive */
1077 valpy_absolute
, /* nb_absolute */
1078 valpy_nonzero
, /* nb_nonzero */
1079 valpy_invert
, /* nb_invert */
1080 valpy_lsh
, /* nb_lshift */
1081 valpy_rsh
, /* nb_rshift */
1082 valpy_and
, /* nb_and */
1083 valpy_xor
, /* nb_xor */
1084 valpy_or
, /* nb_or */
1085 NULL
, /* nb_coerce */
1086 valpy_int
, /* nb_int */
1087 valpy_long
, /* nb_long */
1088 valpy_float
, /* nb_float */
1093 static PyMappingMethods value_object_as_mapping
= {
1099 PyTypeObject value_object_type
= {
1100 PyObject_HEAD_INIT (NULL
)
1102 "gdb.Value", /*tp_name*/
1103 sizeof (value_object
), /*tp_basicsize*/
1105 valpy_dealloc
, /*tp_dealloc*/
1111 &value_object_as_number
, /*tp_as_number*/
1112 0, /*tp_as_sequence*/
1113 &value_object_as_mapping
, /*tp_as_mapping*/
1114 valpy_hash
, /*tp_hash*/
1116 valpy_str
, /*tp_str*/
1120 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
, /*tp_flags*/
1121 "GDB value object", /* tp_doc */
1122 0, /* tp_traverse */
1124 valpy_richcompare
, /* tp_richcompare */
1125 0, /* tp_weaklistoffset */
1127 0, /* tp_iternext */
1128 value_object_methods
, /* tp_methods */
1130 value_object_getset
, /* tp_getset */
1133 0, /* tp_descr_get */
1134 0, /* tp_descr_set */
1135 0, /* tp_dictoffset */
1138 valpy_new
/* tp_new */
1144 preserve_python_values (struct objfile
*objfile
, htab_t copied_types
)
1149 #endif /* HAVE_PYTHON */