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"
32 #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 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
;
66 /* List of all values which are currently exposed to Python. It is
67 maintained so that when an objfile is discarded, preserve_values
68 can copy the values' types if needed. */
69 /* This variable is unnecessarily initialized to NULL in order to
70 work around a linker bug on MacOS. */
71 static value_object
*values_in_python
= NULL
;
73 /* Called by the Python interpreter when deallocating a value object. */
75 valpy_dealloc (PyObject
*obj
)
77 value_object
*self
= (value_object
*) obj
;
79 /* Remove SELF from the global list. */
81 self
->prev
->next
= self
->next
;
84 gdb_assert (values_in_python
== self
);
85 values_in_python
= self
->next
;
88 self
->next
->prev
= self
->prev
;
90 value_free (self
->value
);
93 /* Use braces to appease gcc warning. *sigh* */
95 Py_DECREF (self
->address
);
100 Py_DECREF (self
->type
);
103 self
->ob_type
->tp_free (self
);
106 /* Helper to push a Value object on the global list. */
108 note_value (value_object
*value_obj
)
110 value_obj
->next
= values_in_python
;
112 value_obj
->next
->prev
= value_obj
;
113 value_obj
->prev
= NULL
;
114 values_in_python
= value_obj
;
117 /* Called when a new gdb.Value object needs to be allocated. */
119 valpy_new (PyTypeObject
*subtype
, PyObject
*args
, PyObject
*keywords
)
121 struct value
*value
= NULL
; /* Initialize to appease gcc warning. */
122 value_object
*value_obj
;
124 if (PyTuple_Size (args
) != 1)
126 PyErr_SetString (PyExc_TypeError
, _("Value object creation takes only "
131 value_obj
= (value_object
*) subtype
->tp_alloc (subtype
, 1);
132 if (value_obj
== NULL
)
134 PyErr_SetString (PyExc_MemoryError
, _("Could not allocate memory to "
135 "create Value object."));
139 value
= convert_value_from_python (PyTuple_GetItem (args
, 0));
142 subtype
->tp_free (value_obj
);
146 value_obj
->value
= value
;
147 value_incref (value
);
148 value_obj
->address
= NULL
;
149 value_obj
->type
= NULL
;
150 note_value (value_obj
);
152 return (PyObject
*) value_obj
;
155 /* Iterate over all the Value objects, calling preserve_one_value on
158 preserve_python_values (struct objfile
*objfile
, htab_t copied_types
)
162 for (iter
= values_in_python
; iter
; iter
= iter
->next
)
163 preserve_one_value (iter
->value
, objfile
, copied_types
);
166 /* Given a value of a pointer type, apply the C unary * operator to it. */
168 valpy_dereference (PyObject
*self
, PyObject
*args
)
170 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
171 volatile struct gdb_exception except
;
173 TRY_CATCH (except
, RETURN_MASK_ALL
)
175 res_val
= value_ind (((value_object
*) self
)->value
);
177 GDB_PY_HANDLE_EXCEPTION (except
);
179 return value_to_value_object (res_val
);
182 /* Return "&value". */
184 valpy_get_address (PyObject
*self
, void *closure
)
186 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
187 value_object
*val_obj
= (value_object
*) self
;
188 volatile struct gdb_exception except
;
190 if (!val_obj
->address
)
192 TRY_CATCH (except
, RETURN_MASK_ALL
)
194 res_val
= value_addr (val_obj
->value
);
196 if (except
.reason
< 0)
198 val_obj
->address
= Py_None
;
202 val_obj
->address
= value_to_value_object (res_val
);
205 Py_INCREF (val_obj
->address
);
207 return val_obj
->address
;
210 /* Return type of the value. */
212 valpy_get_type (PyObject
*self
, void *closure
)
214 value_object
*obj
= (value_object
*) self
;
218 obj
->type
= type_to_type_object (value_type (obj
->value
));
222 Py_INCREF (obj
->type
);
225 Py_INCREF (obj
->type
);
229 /* Implementation of gdb.Value.lazy_string ([encoding] [, length]) ->
230 string. Return a PyObject representing a lazy_string_object type.
231 A lazy string is a pointer to a string with an optional encoding and
232 length. If ENCODING is not given, encoding is set to None. If an
233 ENCODING is provided the encoding parameter is set to ENCODING, but
234 the string is not encoded. If LENGTH is provided then the length
235 parameter is set to LENGTH, otherwise length will be set to -1 (first
236 null of appropriate with). */
238 valpy_lazy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
241 struct value
*value
= ((value_object
*) self
)->value
;
242 const char *user_encoding
= NULL
;
243 static char *keywords
[] = { "encoding", "length", NULL
};
246 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|si", keywords
,
247 &user_encoding
, &length
))
250 if (TYPE_CODE (value_type (value
)) == TYPE_CODE_PTR
)
251 value
= value_ind (value
);
253 str_obj
= gdbpy_create_lazy_string_object (value_address (value
), length
,
254 user_encoding
, value_type (value
));
256 return (PyObject
*) str_obj
;
259 /* Implementation of gdb.Value.string ([encoding] [, errors]
260 [, length]) -> string. Return Unicode string with value contents.
261 If ENCODING is not given, the string is assumed to be encoded in
262 the target's charset. If LENGTH is provided, only fetch string to
263 the length provided. */
266 valpy_string (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
270 struct value
*value
= ((value_object
*) self
)->value
;
271 volatile struct gdb_exception except
;
273 const char *encoding
= NULL
;
274 const char *errors
= NULL
;
275 const char *user_encoding
= NULL
;
276 const char *la_encoding
= NULL
;
277 struct type
*char_type
;
278 static char *keywords
[] = { "encoding", "errors", "length", NULL
};
280 if (!PyArg_ParseTupleAndKeywords (args
, kw
, "|ssi", keywords
,
281 &user_encoding
, &errors
, &length
))
284 TRY_CATCH (except
, RETURN_MASK_ALL
)
286 LA_GET_STRING (value
, &buffer
, &length
, &char_type
, &la_encoding
);
288 GDB_PY_HANDLE_EXCEPTION (except
);
290 encoding
= (user_encoding
&& *user_encoding
) ? user_encoding
: la_encoding
;
291 unicode
= PyUnicode_Decode (buffer
, length
* TYPE_LENGTH (char_type
),
298 /* Cast a value to a given type. */
300 valpy_cast (PyObject
*self
, PyObject
*args
)
304 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
305 volatile struct gdb_exception except
;
307 if (! PyArg_ParseTuple (args
, "O", &type_obj
))
310 type
= type_object_to_type (type_obj
);
313 PyErr_SetString (PyExc_RuntimeError
,
314 _("Argument must be a type."));
318 TRY_CATCH (except
, RETURN_MASK_ALL
)
320 res_val
= value_cast (type
, ((value_object
*) self
)->value
);
322 GDB_PY_HANDLE_EXCEPTION (except
);
324 return value_to_value_object (res_val
);
328 valpy_length (PyObject
*self
)
330 /* We don't support getting the number of elements in a struct / class. */
331 PyErr_SetString (PyExc_NotImplementedError
,
332 _("Invalid operation on gdb.Value."));
336 /* Given string name of an element inside structure, return its value
339 valpy_getitem (PyObject
*self
, PyObject
*key
)
341 value_object
*self_value
= (value_object
*) self
;
343 struct value
*res_val
= NULL
;
344 volatile struct gdb_exception except
;
346 if (gdbpy_is_string (key
))
348 field
= python_string_to_host_string (key
);
353 TRY_CATCH (except
, RETURN_MASK_ALL
)
355 struct value
*tmp
= self_value
->value
;
358 res_val
= value_struct_elt (&tmp
, NULL
, field
, 0, NULL
);
361 /* Assume we are attempting an array access, and let the
362 value code throw an exception if the index has an invalid
364 struct value
*idx
= convert_value_from_python (key
);
368 /* Check the value's type is something that can be accessed via
372 tmp
= coerce_ref (tmp
);
373 type
= check_typedef (value_type (tmp
));
374 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
375 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
376 error( _("Cannot subscript requested type."));
378 res_val
= value_subscript (tmp
, value_as_long (idx
));
384 GDB_PY_HANDLE_EXCEPTION (except
);
386 return res_val
? value_to_value_object (res_val
) : NULL
;
390 valpy_setitem (PyObject
*self
, PyObject
*key
, PyObject
*value
)
392 PyErr_Format (PyExc_NotImplementedError
,
393 _("Setting of struct elements is not currently supported."));
397 /* Called by the Python interpreter to perform an inferior function
398 call on the value. */
400 valpy_call (PyObject
*self
, PyObject
*args
, PyObject
*keywords
)
402 struct value
*return_value
= NULL
;
403 Py_ssize_t args_count
;
404 volatile struct gdb_exception except
;
405 struct value
*function
= ((value_object
*) self
)->value
;
406 struct value
**vargs
= NULL
;
407 struct type
*ftype
= check_typedef (value_type (function
));
409 if (TYPE_CODE (ftype
) != TYPE_CODE_FUNC
)
411 PyErr_SetString (PyExc_RuntimeError
,
412 _("Value is not callable (not TYPE_CODE_FUNC)."));
416 args_count
= PyTuple_Size (args
);
421 vargs
= alloca (sizeof (struct value
*) * args_count
);
422 for (i
= 0; i
< args_count
; i
++)
424 PyObject
*item
= PyTuple_GetItem (args
, i
);
429 vargs
[i
] = convert_value_from_python (item
);
430 if (vargs
[i
] == NULL
)
435 TRY_CATCH (except
, RETURN_MASK_ALL
)
437 return_value
= call_function_by_hand (function
, args_count
, vargs
);
439 GDB_PY_HANDLE_EXCEPTION (except
);
441 return value_to_value_object (return_value
);
444 /* Called by the Python interpreter to obtain string representation
447 valpy_str (PyObject
*self
)
451 struct cleanup
*old_chain
;
453 struct value_print_options opts
;
454 volatile struct gdb_exception except
;
456 get_user_print_options (&opts
);
459 stb
= mem_fileopen ();
460 old_chain
= make_cleanup_ui_file_delete (stb
);
462 TRY_CATCH (except
, RETURN_MASK_ALL
)
464 common_val_print (((value_object
*) self
)->value
, stb
, 0,
465 &opts
, python_language
);
466 s
= ui_file_xstrdup (stb
, NULL
);
468 GDB_PY_HANDLE_EXCEPTION (except
);
470 do_cleanups (old_chain
);
472 result
= PyUnicode_Decode (s
, strlen (s
), host_charset (), NULL
);
478 /* Implements gdb.Value.is_optimized_out. */
480 valpy_get_is_optimized_out (PyObject
*self
, void *closure
)
482 struct value
*value
= ((value_object
*) self
)->value
;
484 if (value_optimized_out (value
))
490 /* Calculate and return the address of the PyObject as the value of
491 the builtin __hash__ call. */
493 valpy_hash (PyObject
*self
)
495 return (long) (intptr_t) self
;
513 /* If TYPE is a reference, return the target; otherwise return TYPE. */
514 #define STRIP_REFERENCE(TYPE) \
515 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
517 /* Returns a value object which is the result of applying the operation
518 specified by OPCODE to the given arguments. */
520 valpy_binop (enum valpy_opcode opcode
, PyObject
*self
, PyObject
*other
)
522 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
523 volatile struct gdb_exception except
;
525 TRY_CATCH (except
, RETURN_MASK_ALL
)
527 struct value
*arg1
, *arg2
;
529 /* If the gdb.Value object is the second operand, then it will be passed
530 to us as the OTHER argument, and SELF will be an entirely different
531 kind of object, altogether. Because of this, we can't assume self is
532 a gdb.Value object and need to convert it from python as well. */
533 arg1
= convert_value_from_python (self
);
537 arg2
= convert_value_from_python (other
);
545 struct type
*ltype
= value_type (arg1
);
546 struct type
*rtype
= value_type (arg2
);
548 CHECK_TYPEDEF (ltype
);
549 ltype
= STRIP_REFERENCE (ltype
);
550 CHECK_TYPEDEF (rtype
);
551 rtype
= STRIP_REFERENCE (rtype
);
553 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
554 && is_integral_type (rtype
))
555 res_val
= value_ptradd (arg1
, value_as_long (arg2
));
556 else if (TYPE_CODE (rtype
) == TYPE_CODE_PTR
557 && is_integral_type (ltype
))
558 res_val
= value_ptradd (arg2
, value_as_long (arg1
));
560 res_val
= value_binop (arg1
, arg2
, BINOP_ADD
);
565 struct type
*ltype
= value_type (arg1
);
566 struct type
*rtype
= value_type (arg2
);
568 CHECK_TYPEDEF (ltype
);
569 ltype
= STRIP_REFERENCE (ltype
);
570 CHECK_TYPEDEF (rtype
);
571 rtype
= STRIP_REFERENCE (rtype
);
573 if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
574 && TYPE_CODE (rtype
) == TYPE_CODE_PTR
)
575 /* A ptrdiff_t for the target would be preferable here. */
576 res_val
= value_from_longest (builtin_type_pyint
,
577 value_ptrdiff (arg1
, arg2
));
578 else if (TYPE_CODE (ltype
) == TYPE_CODE_PTR
579 && is_integral_type (rtype
))
580 res_val
= value_ptradd (arg1
, - value_as_long (arg2
));
582 res_val
= value_binop (arg1
, arg2
, BINOP_SUB
);
586 res_val
= value_binop (arg1
, arg2
, BINOP_MUL
);
589 res_val
= value_binop (arg1
, arg2
, BINOP_DIV
);
592 res_val
= value_binop (arg1
, arg2
, BINOP_REM
);
595 res_val
= value_binop (arg1
, arg2
, BINOP_EXP
);
598 res_val
= value_binop (arg1
, arg2
, BINOP_LSH
);
601 res_val
= value_binop (arg1
, arg2
, BINOP_RSH
);
604 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_AND
);
607 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_IOR
);
610 res_val
= value_binop (arg1
, arg2
, BINOP_BITWISE_XOR
);
614 GDB_PY_HANDLE_EXCEPTION (except
);
616 return res_val
? value_to_value_object (res_val
) : NULL
;
620 valpy_add (PyObject
*self
, PyObject
*other
)
622 return valpy_binop (VALPY_ADD
, self
, other
);
626 valpy_subtract (PyObject
*self
, PyObject
*other
)
628 return valpy_binop (VALPY_SUB
, self
, other
);
632 valpy_multiply (PyObject
*self
, PyObject
*other
)
634 return valpy_binop (VALPY_MUL
, self
, other
);
638 valpy_divide (PyObject
*self
, PyObject
*other
)
640 return valpy_binop (VALPY_DIV
, self
, other
);
644 valpy_remainder (PyObject
*self
, PyObject
*other
)
646 return valpy_binop (VALPY_REM
, self
, other
);
650 valpy_power (PyObject
*self
, PyObject
*other
, PyObject
*unused
)
652 /* We don't support the ternary form of pow. I don't know how to express
653 that, so let's just throw NotImplementedError to at least do something
655 if (unused
!= Py_None
)
657 PyErr_SetString (PyExc_NotImplementedError
,
658 "Invalid operation on gdb.Value.");
662 return valpy_binop (VALPY_POW
, self
, other
);
666 valpy_negative (PyObject
*self
)
668 struct value
*val
= NULL
;
669 volatile struct gdb_exception except
;
671 TRY_CATCH (except
, RETURN_MASK_ALL
)
673 val
= value_neg (((value_object
*) self
)->value
);
675 GDB_PY_HANDLE_EXCEPTION (except
);
677 return value_to_value_object (val
);
681 valpy_positive (PyObject
*self
)
683 return value_to_value_object (((value_object
*) self
)->value
);
687 valpy_absolute (PyObject
*self
)
689 struct value
*value
= ((value_object
*) self
)->value
;
691 if (value_less (value
, value_zero (value_type (value
), not_lval
)))
692 return valpy_negative (self
);
694 return valpy_positive (self
);
697 /* Implements boolean evaluation of gdb.Value. */
699 valpy_nonzero (PyObject
*self
)
701 value_object
*self_value
= (value_object
*) self
;
704 type
= check_typedef (value_type (self_value
->value
));
706 if (is_integral_type (type
) || TYPE_CODE (type
) == TYPE_CODE_PTR
)
707 return !!value_as_long (self_value
->value
);
708 else if (TYPE_CODE (type
) == TYPE_CODE_FLT
)
709 return value_as_double (self_value
->value
) != 0;
710 else if (TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
711 return !decimal_is_zero (value_contents (self_value
->value
),
713 gdbarch_byte_order (get_type_arch (type
)));
716 PyErr_SetString (PyExc_TypeError
, _("Attempted truth testing on invalid "
722 /* Implements ~ for value objects. */
724 valpy_invert (PyObject
*self
)
726 struct value
*val
= NULL
;
727 volatile struct gdb_exception except
;
729 TRY_CATCH (except
, RETURN_MASK_ALL
)
731 val
= value_complement (((value_object
*) self
)->value
);
733 GDB_PY_HANDLE_EXCEPTION (except
);
735 return value_to_value_object (val
);
738 /* Implements left shift for value objects. */
740 valpy_lsh (PyObject
*self
, PyObject
*other
)
742 return valpy_binop (VALPY_LSH
, self
, other
);
745 /* Implements right shift for value objects. */
747 valpy_rsh (PyObject
*self
, PyObject
*other
)
749 return valpy_binop (VALPY_RSH
, self
, other
);
752 /* Implements bitwise and for value objects. */
754 valpy_and (PyObject
*self
, PyObject
*other
)
756 return valpy_binop (VALPY_BITAND
, self
, other
);
759 /* Implements bitwise or for value objects. */
761 valpy_or (PyObject
*self
, PyObject
*other
)
763 return valpy_binop (VALPY_BITOR
, self
, other
);
766 /* Implements bitwise xor for value objects. */
768 valpy_xor (PyObject
*self
, PyObject
*other
)
770 return valpy_binop (VALPY_BITXOR
, self
, other
);
773 /* Implements comparison operations for value objects. */
775 valpy_richcompare (PyObject
*self
, PyObject
*other
, int op
)
778 struct value
*value_other
;
779 volatile struct gdb_exception except
;
781 if (other
== Py_None
)
782 /* Comparing with None is special. From what I can tell, in Python
783 None is smaller than anything else. */
795 PyErr_SetString (PyExc_NotImplementedError
,
796 _("Invalid operation on gdb.Value."));
800 TRY_CATCH (except
, RETURN_MASK_ALL
)
802 value_other
= convert_value_from_python (other
);
803 if (value_other
== NULL
)
811 result
= value_less (((value_object
*) self
)->value
, value_other
);
814 result
= value_less (((value_object
*) self
)->value
, value_other
)
815 || value_equal (((value_object
*) self
)->value
, value_other
);
818 result
= value_equal (((value_object
*) self
)->value
, value_other
);
821 result
= !value_equal (((value_object
*) self
)->value
, value_other
);
824 result
= value_less (value_other
, ((value_object
*) self
)->value
);
827 result
= value_less (value_other
, ((value_object
*) self
)->value
)
828 || value_equal (((value_object
*) self
)->value
, value_other
);
832 PyErr_SetString (PyExc_NotImplementedError
,
833 _("Invalid operation on gdb.Value."));
838 GDB_PY_HANDLE_EXCEPTION (except
);
840 /* In this case, the Python exception has already been set. */
850 /* Helper function to determine if a type is "int-like". */
852 is_intlike (struct type
*type
, int ptr_ok
)
854 CHECK_TYPEDEF (type
);
855 return (TYPE_CODE (type
) == TYPE_CODE_INT
856 || TYPE_CODE (type
) == TYPE_CODE_ENUM
857 || TYPE_CODE (type
) == TYPE_CODE_BOOL
858 || TYPE_CODE (type
) == TYPE_CODE_CHAR
859 || (ptr_ok
&& TYPE_CODE (type
) == TYPE_CODE_PTR
));
862 /* Implements conversion to int. */
864 valpy_int (PyObject
*self
)
866 struct value
*value
= ((value_object
*) self
)->value
;
867 struct type
*type
= value_type (value
);
869 volatile struct gdb_exception except
;
871 CHECK_TYPEDEF (type
);
872 if (!is_intlike (type
, 0))
874 PyErr_SetString (PyExc_RuntimeError
,
875 _("Cannot convert value to int."));
879 TRY_CATCH (except
, RETURN_MASK_ALL
)
881 l
= value_as_long (value
);
883 GDB_PY_HANDLE_EXCEPTION (except
);
885 #ifdef HAVE_LONG_LONG /* Defined by Python. */
886 /* If we have 'long long', and the value overflows a 'long', use a
887 Python Long; otherwise use a Python Int. */
888 if (sizeof (l
) > sizeof (long) && (l
> PyInt_GetMax ()
889 || l
< (- (LONGEST
) PyInt_GetMax ()) - 1))
890 return PyLong_FromLongLong (l
);
892 return PyInt_FromLong (l
);
895 /* Implements conversion to long. */
897 valpy_long (PyObject
*self
)
899 struct value
*value
= ((value_object
*) self
)->value
;
900 struct type
*type
= value_type (value
);
902 volatile struct gdb_exception except
;
904 if (!is_intlike (type
, 1))
906 PyErr_SetString (PyExc_RuntimeError
,
907 _("Cannot convert value to long."));
911 TRY_CATCH (except
, RETURN_MASK_ALL
)
913 l
= value_as_long (value
);
915 GDB_PY_HANDLE_EXCEPTION (except
);
917 #ifdef HAVE_LONG_LONG /* Defined by Python. */
918 return PyLong_FromLongLong (l
);
920 return PyLong_FromLong (l
);
924 /* Implements conversion to float. */
926 valpy_float (PyObject
*self
)
928 struct value
*value
= ((value_object
*) self
)->value
;
929 struct type
*type
= value_type (value
);
931 volatile struct gdb_exception except
;
933 CHECK_TYPEDEF (type
);
934 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
936 PyErr_SetString (PyExc_RuntimeError
,
937 _("Cannot convert value to float."));
941 TRY_CATCH (except
, RETURN_MASK_ALL
)
943 d
= value_as_double (value
);
945 GDB_PY_HANDLE_EXCEPTION (except
);
947 return PyFloat_FromDouble (d
);
950 /* Returns an object for a value which is released from the all_values chain,
951 so its lifetime is not bound to the execution of a command. */
953 value_to_value_object (struct value
*val
)
955 value_object
*val_obj
;
957 val_obj
= PyObject_New (value_object
, &value_object_type
);
960 val_obj
->value
= val
;
962 val_obj
->address
= NULL
;
963 val_obj
->type
= NULL
;
964 note_value (val_obj
);
967 return (PyObject
*) val_obj
;
970 /* Returns a borrowed reference to the struct value corresponding to
971 the given value object. */
973 value_object_to_value (PyObject
*self
)
977 if (! PyObject_TypeCheck (self
, &value_object_type
))
979 real
= (value_object
*) self
;
983 /* Try to convert a Python value to a gdb value. If the value cannot
984 be converted, set a Python exception and return NULL. Returns a
985 reference to a new value on the all_values chain. */
988 convert_value_from_python (PyObject
*obj
)
990 struct value
*value
= NULL
; /* -Wall */
992 volatile struct gdb_exception except
;
995 gdb_assert (obj
!= NULL
);
997 TRY_CATCH (except
, RETURN_MASK_ALL
)
999 if (PyBool_Check (obj
))
1001 cmp
= PyObject_IsTrue (obj
);
1003 value
= value_from_longest (builtin_type_pybool
, cmp
);
1005 else if (PyInt_Check (obj
))
1007 long l
= PyInt_AsLong (obj
);
1009 if (! PyErr_Occurred ())
1010 value
= value_from_longest (builtin_type_pyint
, l
);
1012 else if (PyLong_Check (obj
))
1014 LONGEST l
= PyLong_AsLongLong (obj
);
1016 if (PyErr_Occurred ())
1018 /* If the error was an overflow, we can try converting to
1019 ULONGEST instead. */
1020 if (PyErr_ExceptionMatches (PyExc_OverflowError
))
1022 PyObject
*etype
, *evalue
, *etraceback
, *zero
;
1024 PyErr_Fetch (&etype
, &evalue
, &etraceback
);
1025 zero
= PyInt_FromLong (0);
1027 /* Check whether obj is positive. */
1028 if (PyObject_RichCompareBool (obj
, zero
, Py_GT
) > 0)
1032 ul
= PyLong_AsUnsignedLongLong (obj
);
1033 if (! PyErr_Occurred ())
1034 value
= value_from_ulongest (builtin_type_upylong
, ul
);
1037 /* There's nothing we can do. */
1038 PyErr_Restore (etype
, evalue
, etraceback
);
1044 value
= value_from_longest (builtin_type_pylong
, l
);
1046 else if (PyFloat_Check (obj
))
1048 double d
= PyFloat_AsDouble (obj
);
1050 if (! PyErr_Occurred ())
1051 value
= value_from_double (builtin_type_pyfloat
, d
);
1053 else if (gdbpy_is_string (obj
))
1057 s
= python_string_to_target_string (obj
);
1060 old
= make_cleanup (xfree
, s
);
1061 value
= value_cstring (s
, strlen (s
), builtin_type_pychar
);
1065 else if (PyObject_TypeCheck (obj
, &value_object_type
))
1066 value
= value_copy (((value_object
*) obj
)->value
);
1067 else if (gdbpy_is_lazy_string (obj
))
1070 PyObject
*function
= PyString_FromString ("value");
1072 result
= PyObject_CallMethodObjArgs (obj
, function
, NULL
);
1073 value
= value_copy (((value_object
*) result
)->value
);
1076 PyErr_Format (PyExc_TypeError
, _("Could not convert Python object: %s."),
1077 PyString_AsString (PyObject_Str (obj
)));
1079 if (except
.reason
< 0)
1081 PyErr_Format (except
.reason
== RETURN_QUIT
1082 ? PyExc_KeyboardInterrupt
: PyExc_RuntimeError
,
1083 "%s", except
.message
);
1090 /* Returns value object in the ARGth position in GDB's history. */
1092 gdbpy_history (PyObject
*self
, PyObject
*args
)
1095 struct value
*res_val
= NULL
; /* Initialize to appease gcc warning. */
1096 volatile struct gdb_exception except
;
1098 if (!PyArg_ParseTuple (args
, "i", &i
))
1101 TRY_CATCH (except
, RETURN_MASK_ALL
)
1103 res_val
= access_value_history (i
);
1105 GDB_PY_HANDLE_EXCEPTION (except
);
1107 return value_to_value_object (res_val
);
1110 /* Returns 1 in OBJ is a gdb.Value object, 0 otherwise. */
1113 gdbpy_is_value_object (PyObject
*obj
)
1115 return PyObject_TypeCheck (obj
, &value_object_type
);
1119 gdbpy_initialize_values (void)
1121 if (PyType_Ready (&value_object_type
) < 0)
1124 Py_INCREF (&value_object_type
);
1125 PyModule_AddObject (gdb_module
, "Value", (PyObject
*) &value_object_type
);
1127 values_in_python
= NULL
;
1132 static PyGetSetDef value_object_getset
[] = {
1133 { "address", valpy_get_address
, NULL
, "The address of the value.",
1135 { "is_optimized_out", valpy_get_is_optimized_out
, NULL
,
1136 "Boolean telling whether the value is optimized out (i.e., not available).",
1138 { "type", valpy_get_type
, NULL
, "Type of the value.", NULL
},
1139 {NULL
} /* Sentinel */
1142 static PyMethodDef value_object_methods
[] = {
1143 { "cast", valpy_cast
, METH_VARARGS
, "Cast the value to the supplied type." },
1144 { "dereference", valpy_dereference
, METH_NOARGS
, "Dereferences the value." },
1145 { "lazy_string", (PyCFunction
) valpy_lazy_string
, METH_VARARGS
| METH_KEYWORDS
,
1146 "lazy_string ([encoding] [, length]) -> lazy_string\n\
1147 Return a lazy string representation of the value." },
1148 { "string", (PyCFunction
) valpy_string
, METH_VARARGS
| METH_KEYWORDS
,
1149 "string ([encoding] [, errors] [, length]) -> string\n\
1150 Return Unicode string representation of the value." },
1151 {NULL
} /* Sentinel */
1154 static PyNumberMethods value_object_as_number
= {
1160 NULL
, /* nb_divmod */
1161 valpy_power
, /* nb_power */
1162 valpy_negative
, /* nb_negative */
1163 valpy_positive
, /* nb_positive */
1164 valpy_absolute
, /* nb_absolute */
1165 valpy_nonzero
, /* nb_nonzero */
1166 valpy_invert
, /* nb_invert */
1167 valpy_lsh
, /* nb_lshift */
1168 valpy_rsh
, /* nb_rshift */
1169 valpy_and
, /* nb_and */
1170 valpy_xor
, /* nb_xor */
1171 valpy_or
, /* nb_or */
1172 NULL
, /* nb_coerce */
1173 valpy_int
, /* nb_int */
1174 valpy_long
, /* nb_long */
1175 valpy_float
, /* nb_float */
1180 static PyMappingMethods value_object_as_mapping
= {
1186 PyTypeObject value_object_type
= {
1187 PyObject_HEAD_INIT (NULL
)
1189 "gdb.Value", /*tp_name*/
1190 sizeof (value_object
), /*tp_basicsize*/
1192 valpy_dealloc
, /*tp_dealloc*/
1198 &value_object_as_number
, /*tp_as_number*/
1199 0, /*tp_as_sequence*/
1200 &value_object_as_mapping
, /*tp_as_mapping*/
1201 valpy_hash
, /*tp_hash*/
1202 valpy_call
, /*tp_call*/
1203 valpy_str
, /*tp_str*/
1207 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
, /*tp_flags*/
1208 "GDB value object", /* tp_doc */
1209 0, /* tp_traverse */
1211 valpy_richcompare
, /* tp_richcompare */
1212 0, /* tp_weaklistoffset */
1214 0, /* tp_iternext */
1215 value_object_methods
, /* tp_methods */
1217 value_object_getset
, /* tp_getset */
1220 0, /* tp_descr_get */
1221 0, /* tp_descr_set */
1222 0, /* tp_dictoffset */
1225 valpy_new
/* tp_new */
1231 preserve_python_values (struct objfile
*objfile
, htab_t copied_types
)
1236 #endif /* HAVE_PYTHON */