gdb/
[binutils-gdb.git] / gdb / python / python-value.c
1 /* Python interface to values.
2
3 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "gdb_assert.h"
22 #include "charset.h"
23 #include "value.h"
24 #include "exceptions.h"
25 #include "language.h"
26 #include "dfp.h"
27 #include "valprint.h"
28
29 /* List of all values which are currently exposed to Python. It is
30 maintained so that when an objfile is discarded, preserve_values
31 can copy the values' types if needed. This is declared
32 unconditionally to reduce the number of uses of HAVE_PYTHON in the
33 generic code. */
34 /* This variable is unnecessarily initialized to NULL in order to
35 work around a linker bug on MacOS. */
36 struct value *values_in_python = NULL;
37
38 #ifdef HAVE_PYTHON
39
40 #include "python-internal.h"
41
42 /* Even though Python scalar types directly map to host types, we use
43 target types here to remain consistent with the the values system in
44 GDB (which uses target arithmetic). */
45
46 /* Python's integer type corresponds to C's long type. */
47 #define builtin_type_pyint builtin_type (current_gdbarch)->builtin_long
48
49 /* Python's float type corresponds to C's double type. */
50 #define builtin_type_pyfloat builtin_type (current_gdbarch)->builtin_double
51
52 /* Python's long type corresponds to C's long long type. */
53 #define builtin_type_pylong builtin_type (current_gdbarch)->builtin_long_long
54
55 #define builtin_type_pybool \
56 language_bool_type (current_language, current_gdbarch)
57
58 typedef struct {
59 PyObject_HEAD
60 struct value *value;
61 int owned_by_gdb;
62 } value_object;
63
64 /* Called by the Python interpreter when deallocating a value object. */
65 static void
66 valpy_dealloc (PyObject *obj)
67 {
68 value_object *self = (value_object *) obj;
69
70 value_remove_from_list (&values_in_python, self->value);
71
72 if (!self->owned_by_gdb)
73 value_free (self->value);
74 self->ob_type->tp_free (self);
75 }
76
77 /* Called when a new gdb.Value object needs to be allocated. */
78 static PyObject *
79 valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords)
80 {
81 struct value *value = NULL; /* Initialize to appease gcc warning. */
82 value_object *value_obj;
83
84 if (PyTuple_Size (args) != 1)
85 {
86 PyErr_SetString (PyExc_TypeError, _("Value object creation takes only "
87 "1 argument"));
88 return NULL;
89 }
90
91 value_obj = (value_object *) subtype->tp_alloc (subtype, 1);
92 if (value_obj == NULL)
93 {
94 PyErr_SetString (PyExc_MemoryError, _("Could not allocate memory to "
95 "create Value object."));
96 return NULL;
97 }
98
99 value = convert_value_from_python (PyTuple_GetItem (args, 0));
100 if (value == NULL)
101 {
102 subtype->tp_free (value_obj);
103 return NULL;
104 }
105
106 value_obj->value = value;
107 value_obj->owned_by_gdb = 0;
108 release_value (value);
109 value_prepend_to_list (&values_in_python, value);
110
111 return (PyObject *) value_obj;
112 }
113
114 /* Given a value of a pointer type, apply the C unary * operator to it. */
115 static PyObject *
116 valpy_dereference (PyObject *self, PyObject *args)
117 {
118 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
119 volatile struct gdb_exception except;
120
121 TRY_CATCH (except, RETURN_MASK_ALL)
122 {
123 res_val = value_ind (((value_object *) self)->value);
124 }
125 GDB_PY_HANDLE_EXCEPTION (except);
126
127 return value_to_value_object (res_val);
128 }
129
130 /* Return "&value". */
131 static PyObject *
132 valpy_address (PyObject *self, PyObject *args)
133 {
134 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
135 volatile struct gdb_exception except;
136
137 TRY_CATCH (except, RETURN_MASK_ALL)
138 {
139 res_val = value_addr (((value_object *) self)->value);
140 }
141 GDB_PY_HANDLE_EXCEPTION (except);
142
143 return value_to_value_object (res_val);
144 }
145
146 /* Return Unicode string with value contents (assumed to be encoded in the
147 target's charset). */
148 static PyObject *
149 valpy_string (PyObject *self, PyObject *args)
150 {
151 int length, ret = 0;
152 gdb_byte *buffer;
153 struct value *value = ((value_object *) self)->value;
154 volatile struct gdb_exception except;
155 PyObject *unicode;
156 const char *encoding = NULL;
157 const char *errors = NULL;
158 const char *user_encoding = NULL;
159 const char *la_encoding = NULL;
160
161 if (!PyArg_ParseTuple (args, "|ss", &user_encoding, &errors))
162 return NULL;
163
164 TRY_CATCH (except, RETURN_MASK_ALL)
165 {
166 LA_GET_STRING (value, &buffer, &length, &la_encoding);
167 }
168 GDB_PY_HANDLE_EXCEPTION (except);
169
170 encoding = (user_encoding && *user_encoding) ? user_encoding : la_encoding;
171 unicode = PyUnicode_Decode (buffer, length, encoding, errors);
172 xfree (buffer);
173
174 return unicode;
175 }
176
177 static Py_ssize_t
178 valpy_length (PyObject *self)
179 {
180 /* We don't support getting the number of elements in a struct / class. */
181 PyErr_SetString (PyExc_NotImplementedError,
182 "Invalid operation on gdb.Value.");
183 return -1;
184 }
185
186 /* Given string name of an element inside structure, return its value
187 object. */
188 static PyObject *
189 valpy_getitem (PyObject *self, PyObject *key)
190 {
191 value_object *self_value = (value_object *) self;
192 char *field = NULL;
193 struct value *idx = NULL;
194 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
195 volatile struct gdb_exception except;
196
197 if (gdbpy_is_string (key))
198 {
199 field = python_string_to_host_string (key);
200 if (field == NULL)
201 return NULL;
202 }
203
204 TRY_CATCH (except, RETURN_MASK_ALL)
205 {
206 struct value *tmp = self_value->value;
207
208 if (field)
209 res_val = value_struct_elt (&tmp, NULL, field, 0, NULL);
210 else
211 {
212 /* Assume we are attempting an array access, and let the
213 value code throw an exception if the index has an invalid
214 type. */
215 struct value *idx = convert_value_from_python (key);
216 if (idx == NULL)
217 return NULL;
218
219 res_val = value_subscript (tmp, idx);
220 }
221 }
222 if (field)
223 xfree (field);
224 GDB_PY_HANDLE_EXCEPTION (except);
225
226 return value_to_value_object (res_val);
227 }
228
229 static int
230 valpy_setitem (PyObject *self, PyObject *key, PyObject *value)
231 {
232 PyErr_Format (PyExc_NotImplementedError,
233 _("Setting of struct elements is not currently supported."));
234 return -1;
235 }
236
237 /* Called by the Python interpreter to obtain string representation
238 of the object. */
239 static PyObject *
240 valpy_str (PyObject *self)
241 {
242 char *s = NULL;
243 long dummy;
244 struct ui_file *stb;
245 struct cleanup *old_chain;
246 PyObject *result;
247 struct value_print_options opts;
248 volatile struct gdb_exception except;
249
250 get_user_print_options (&opts);
251 opts.deref_ref = 0;
252
253 stb = mem_fileopen ();
254 old_chain = make_cleanup_ui_file_delete (stb);
255
256 TRY_CATCH (except, RETURN_MASK_ALL)
257 {
258 common_val_print (((value_object *) self)->value, stb, 0,
259 &opts, current_language);
260 s = ui_file_xstrdup (stb, &dummy);
261 }
262 GDB_PY_HANDLE_EXCEPTION (except);
263
264 do_cleanups (old_chain);
265
266 result = PyUnicode_Decode (s, strlen (s), host_charset (), NULL);
267 xfree (s);
268
269 return result;
270 }
271
272 enum valpy_opcode
273 {
274 VALPY_ADD,
275 VALPY_SUB,
276 VALPY_MUL,
277 VALPY_DIV,
278 VALPY_REM,
279 VALPY_POW,
280 VALPY_LSH,
281 VALPY_RSH,
282 VALPY_BITAND,
283 VALPY_BITOR,
284 VALPY_BITXOR
285 };
286
287 /* If TYPE is a reference, return the target; otherwise return TYPE. */
288 #define STRIP_REFERENCE(TYPE) \
289 ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE))
290
291 /* Returns a value object which is the result of applying the operation
292 specified by OPCODE to the given arguments. */
293 static PyObject *
294 valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other)
295 {
296 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
297 volatile struct gdb_exception except;
298
299 TRY_CATCH (except, RETURN_MASK_ALL)
300 {
301 struct value *arg1, *arg2;
302
303 /* If the gdb.Value object is the second operand, then it will be passed
304 to us as the OTHER argument, and SELF will be an entirely different
305 kind of object, altogether. Because of this, we can't assume self is
306 a gdb.Value object and need to convert it from python as well. */
307 arg1 = convert_value_from_python (self);
308 if (arg1 == NULL)
309 return NULL;
310
311 arg2 = convert_value_from_python (other);
312 if (arg2 == NULL)
313 return NULL;
314
315 switch (opcode)
316 {
317 case VALPY_ADD:
318 {
319 struct type *ltype = value_type (arg1);
320 struct type *rtype = value_type (arg2);
321
322 CHECK_TYPEDEF (ltype);
323 ltype = STRIP_REFERENCE (ltype);
324 CHECK_TYPEDEF (rtype);
325 rtype = STRIP_REFERENCE (rtype);
326
327 if (TYPE_CODE (ltype) == TYPE_CODE_PTR)
328 res_val = value_ptradd (arg1, arg2);
329 else if (TYPE_CODE (rtype) == TYPE_CODE_PTR)
330 res_val = value_ptradd (arg2, arg1);
331 else
332 res_val = value_binop (arg1, arg2, BINOP_ADD);
333 }
334 break;
335 case VALPY_SUB:
336 {
337 struct type *ltype = value_type (arg1);
338 struct type *rtype = value_type (arg2);
339
340 CHECK_TYPEDEF (ltype);
341 ltype = STRIP_REFERENCE (ltype);
342 CHECK_TYPEDEF (rtype);
343 rtype = STRIP_REFERENCE (rtype);
344
345 if (TYPE_CODE (ltype) == TYPE_CODE_PTR)
346 {
347 if (TYPE_CODE (rtype) == TYPE_CODE_PTR)
348 /* A ptrdiff_t for the target would be preferable
349 here. */
350 res_val = value_from_longest (builtin_type_pyint,
351 value_ptrdiff (arg1, arg2));
352 else
353 res_val = value_ptrsub (arg1, arg2);
354 }
355 else
356 res_val = value_binop (arg1, arg2, BINOP_SUB);
357 }
358 break;
359 case VALPY_MUL:
360 res_val = value_binop (arg1, arg2, BINOP_MUL);
361 break;
362 case VALPY_DIV:
363 res_val = value_binop (arg1, arg2, BINOP_DIV);
364 break;
365 case VALPY_REM:
366 res_val = value_binop (arg1, arg2, BINOP_REM);
367 break;
368 case VALPY_POW:
369 res_val = value_binop (arg1, arg2, BINOP_EXP);
370 break;
371 case VALPY_LSH:
372 res_val = value_binop (arg1, arg2, BINOP_LSH);
373 break;
374 case VALPY_RSH:
375 res_val = value_binop (arg1, arg2, BINOP_RSH);
376 break;
377 case VALPY_BITAND:
378 res_val = value_binop (arg1, arg2, BINOP_BITWISE_AND);
379 break;
380 case VALPY_BITOR:
381 res_val = value_binop (arg1, arg2, BINOP_BITWISE_IOR);
382 break;
383 case VALPY_BITXOR:
384 res_val = value_binop (arg1, arg2, BINOP_BITWISE_XOR);
385 break;
386 }
387 }
388 GDB_PY_HANDLE_EXCEPTION (except);
389
390 return value_to_value_object (res_val);
391 }
392
393 static PyObject *
394 valpy_add (PyObject *self, PyObject *other)
395 {
396 return valpy_binop (VALPY_ADD, self, other);
397 }
398
399 static PyObject *
400 valpy_subtract (PyObject *self, PyObject *other)
401 {
402 return valpy_binop (VALPY_SUB, self, other);
403 }
404
405 static PyObject *
406 valpy_multiply (PyObject *self, PyObject *other)
407 {
408 return valpy_binop (VALPY_MUL, self, other);
409 }
410
411 static PyObject *
412 valpy_divide (PyObject *self, PyObject *other)
413 {
414 return valpy_binop (VALPY_DIV, self, other);
415 }
416
417 static PyObject *
418 valpy_remainder (PyObject *self, PyObject *other)
419 {
420 return valpy_binop (VALPY_REM, self, other);
421 }
422
423 static PyObject *
424 valpy_power (PyObject *self, PyObject *other, PyObject *unused)
425 {
426 /* We don't support the ternary form of pow. I don't know how to express
427 that, so let's just throw NotImplementedError to at least do something
428 about it. */
429 if (unused != Py_None)
430 {
431 PyErr_SetString (PyExc_NotImplementedError,
432 "Invalid operation on gdb.Value.");
433 return NULL;
434 }
435
436 return valpy_binop (VALPY_POW, self, other);
437 }
438
439 static PyObject *
440 valpy_negative (PyObject *self)
441 {
442 struct value *val = NULL;
443 volatile struct gdb_exception except;
444
445 TRY_CATCH (except, RETURN_MASK_ALL)
446 {
447 val = value_neg (((value_object *) self)->value);
448 }
449 GDB_PY_HANDLE_EXCEPTION (except);
450
451 return value_to_value_object (val);
452 }
453
454 static PyObject *
455 valpy_positive (PyObject *self)
456 {
457 struct value *copy = value_copy (((value_object *) self)->value);
458
459 return value_to_value_object (copy);
460 }
461
462 static PyObject *
463 valpy_absolute (PyObject *self)
464 {
465 if (value_less (((value_object *) self)->value,
466 value_from_longest (builtin_type_int8, 0)))
467 return valpy_negative (self);
468 else
469 return valpy_positive (self);
470 }
471
472 /* Implements boolean evaluation of gdb.Value. */
473 static int
474 valpy_nonzero (PyObject *self)
475 {
476 value_object *self_value = (value_object *) self;
477 struct type *type;
478
479 type = check_typedef (value_type (self_value->value));
480
481 if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR)
482 return !!value_as_long (self_value->value);
483 else if (TYPE_CODE (type) == TYPE_CODE_FLT)
484 return value_as_double (self_value->value) != 0;
485 else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
486 return !decimal_is_zero (value_contents (self_value->value),
487 TYPE_LENGTH (type));
488 else
489 {
490 PyErr_SetString (PyExc_TypeError, _("Attempted truth testing on invalid "
491 "gdb.Value type."));
492 return 0;
493 }
494 }
495
496 /* Implements ~ for value objects. */
497 static PyObject *
498 valpy_invert (PyObject *self)
499 {
500 struct value *val = NULL;
501 volatile struct gdb_exception except;
502
503 TRY_CATCH (except, RETURN_MASK_ALL)
504 {
505 val = value_complement (((value_object *) self)->value);
506 }
507 GDB_PY_HANDLE_EXCEPTION (except);
508
509 return value_to_value_object (val);
510 }
511
512 /* Implements left shift for value objects. */
513 static PyObject *
514 valpy_lsh (PyObject *self, PyObject *other)
515 {
516 return valpy_binop (VALPY_LSH, self, other);
517 }
518
519 /* Implements right shift for value objects. */
520 static PyObject *
521 valpy_rsh (PyObject *self, PyObject *other)
522 {
523 return valpy_binop (VALPY_RSH, self, other);
524 }
525
526 /* Implements bitwise and for value objects. */
527 static PyObject *
528 valpy_and (PyObject *self, PyObject *other)
529 {
530 return valpy_binop (VALPY_BITAND, self, other);
531 }
532
533 /* Implements bitwise or for value objects. */
534 static PyObject *
535 valpy_or (PyObject *self, PyObject *other)
536 {
537 return valpy_binop (VALPY_BITOR, self, other);
538 }
539
540 /* Implements bitwise xor for value objects. */
541 static PyObject *
542 valpy_xor (PyObject *self, PyObject *other)
543 {
544 return valpy_binop (VALPY_BITXOR, self, other);
545 }
546
547 /* Implements comparison operations for value objects. */
548 static PyObject *
549 valpy_richcompare (PyObject *self, PyObject *other, int op)
550 {
551 int result = 0;
552 struct value *value_other;
553 volatile struct gdb_exception except;
554
555 if (other == Py_None)
556 /* Comparing with None is special. From what I can tell, in Python
557 None is smaller than anything else. */
558 switch (op) {
559 case Py_LT:
560 case Py_LE:
561 case Py_EQ:
562 Py_RETURN_FALSE;
563 case Py_NE:
564 case Py_GT:
565 case Py_GE:
566 Py_RETURN_TRUE;
567 default:
568 /* Can't happen. */
569 PyErr_SetString (PyExc_NotImplementedError,
570 "Invalid operation on gdb.Value.");
571 return NULL;
572 }
573
574 TRY_CATCH (except, RETURN_MASK_ALL)
575 {
576 value_other = convert_value_from_python (other);
577 if (value_other == NULL)
578 return NULL;
579
580 switch (op) {
581 case Py_LT:
582 result = value_less (((value_object *) self)->value, value_other);
583 break;
584 case Py_LE:
585 result = value_less (((value_object *) self)->value, value_other)
586 || value_equal (((value_object *) self)->value, value_other);
587 break;
588 case Py_EQ:
589 result = value_equal (((value_object *) self)->value, value_other);
590 break;
591 case Py_NE:
592 result = !value_equal (((value_object *) self)->value, value_other);
593 break;
594 case Py_GT:
595 result = value_less (value_other, ((value_object *) self)->value);
596 break;
597 case Py_GE:
598 result = value_less (value_other, ((value_object *) self)->value)
599 || value_equal (((value_object *) self)->value, value_other);
600 break;
601 default:
602 /* Can't happen. */
603 PyErr_SetString (PyExc_NotImplementedError,
604 "Invalid operation on gdb.Value.");
605 return NULL;
606 }
607 }
608 GDB_PY_HANDLE_EXCEPTION (except);
609
610 if (result == 1)
611 Py_RETURN_TRUE;
612
613 Py_RETURN_FALSE;
614 }
615
616 /* Helper function to determine if a type is "int-like". */
617 static int
618 is_intlike (struct type *type, int ptr_ok)
619 {
620 CHECK_TYPEDEF (type);
621 return (TYPE_CODE (type) == TYPE_CODE_INT
622 || TYPE_CODE (type) == TYPE_CODE_ENUM
623 || TYPE_CODE (type) == TYPE_CODE_BOOL
624 || TYPE_CODE (type) == TYPE_CODE_CHAR
625 || (ptr_ok && TYPE_CODE (type) == TYPE_CODE_PTR));
626 }
627
628 /* Implements conversion to int. */
629 static PyObject *
630 valpy_int (PyObject *self)
631 {
632 struct value *value = ((value_object *) self)->value;
633 struct type *type = value_type (value);
634 LONGEST l = 0;
635 volatile struct gdb_exception except;
636
637 CHECK_TYPEDEF (type);
638 if (!is_intlike (type, 0))
639 {
640 PyErr_SetString (PyExc_RuntimeError, "cannot convert value to int");
641 return NULL;
642 }
643
644 TRY_CATCH (except, RETURN_MASK_ALL)
645 {
646 l = value_as_long (value);
647 }
648 GDB_PY_HANDLE_EXCEPTION (except);
649
650 return PyInt_FromLong (l);
651 }
652
653 /* Implements conversion to long. */
654 static PyObject *
655 valpy_long (PyObject *self)
656 {
657 struct value *value = ((value_object *) self)->value;
658 struct type *type = value_type (value);
659 LONGEST l = 0;
660 volatile struct gdb_exception except;
661
662 if (!is_intlike (type, 1))
663 {
664 PyErr_SetString (PyExc_RuntimeError, "cannot convert value to long");
665 return NULL;
666 }
667
668 TRY_CATCH (except, RETURN_MASK_ALL)
669 {
670 l = value_as_long (value);
671 }
672 GDB_PY_HANDLE_EXCEPTION (except);
673
674 return PyLong_FromLong (l);
675 }
676
677 /* Implements conversion to float. */
678 static PyObject *
679 valpy_float (PyObject *self)
680 {
681 struct value *value = ((value_object *) self)->value;
682 struct type *type = value_type (value);
683 double d = 0;
684 volatile struct gdb_exception except;
685
686 CHECK_TYPEDEF (type);
687 if (TYPE_CODE (type) != TYPE_CODE_FLT)
688 {
689 PyErr_SetString (PyExc_RuntimeError, "cannot convert value to float");
690 return NULL;
691 }
692
693 TRY_CATCH (except, RETURN_MASK_ALL)
694 {
695 d = value_as_double (value);
696 }
697 GDB_PY_HANDLE_EXCEPTION (except);
698
699 return PyFloat_FromDouble (d);
700 }
701
702 /* Returns an object for a value which is released from the all_values chain,
703 so its lifetime is not bound to the execution of a command. */
704 PyObject *
705 value_to_value_object (struct value *val)
706 {
707 value_object *val_obj;
708
709 val_obj = PyObject_New (value_object, &value_object_type);
710 if (val_obj != NULL)
711 {
712 val_obj->value = val;
713 val_obj->owned_by_gdb = 0;
714 release_value (val);
715 value_prepend_to_list (&values_in_python, val);
716 }
717
718 return (PyObject *) val_obj;
719 }
720
721 /* Try to convert a Python value to a gdb value. If the value cannot
722 be converted, set a Python exception and return NULL. */
723
724 struct value *
725 convert_value_from_python (PyObject *obj)
726 {
727 struct value *value = NULL; /* -Wall */
728 PyObject *target_str, *unicode_str;
729 struct cleanup *old;
730 volatile struct gdb_exception except;
731 int cmp;
732
733 gdb_assert (obj != NULL);
734
735 TRY_CATCH (except, RETURN_MASK_ALL)
736 {
737 if (PyBool_Check (obj))
738 {
739 cmp = PyObject_IsTrue (obj);
740 if (cmp >= 0)
741 value = value_from_longest (builtin_type_pybool, cmp);
742 }
743 else if (PyInt_Check (obj))
744 {
745 long l = PyInt_AsLong (obj);
746
747 if (! PyErr_Occurred ())
748 value = value_from_longest (builtin_type_pyint, l);
749 }
750 else if (PyLong_Check (obj))
751 {
752 LONGEST l = PyLong_AsLongLong (obj);
753
754 if (! PyErr_Occurred ())
755 value = value_from_longest (builtin_type_pylong, l);
756 }
757 else if (PyFloat_Check (obj))
758 {
759 double d = PyFloat_AsDouble (obj);
760
761 if (! PyErr_Occurred ())
762 value = value_from_double (builtin_type_pyfloat, d);
763 }
764 else if (gdbpy_is_string (obj))
765 {
766 char *s;
767
768 s = python_string_to_target_string (obj);
769 if (s != NULL)
770 {
771 old = make_cleanup (xfree, s);
772 value = value_from_string (s);
773 do_cleanups (old);
774 }
775 }
776 else if (PyObject_TypeCheck (obj, &value_object_type))
777 value = ((value_object *) obj)->value;
778 else
779 PyErr_Format (PyExc_TypeError, _("Could not convert Python object: %s"),
780 PyString_AsString (PyObject_Str (obj)));
781 }
782 if (except.reason < 0)
783 {
784 PyErr_Format (except.reason == RETURN_QUIT
785 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
786 "%s", except.message);
787 return NULL;
788 }
789
790 return value;
791 }
792
793 /* Returns value object in the ARGth position in GDB's history. */
794 PyObject *
795 gdbpy_history (PyObject *self, PyObject *args)
796 {
797 int i;
798 struct value *res_val = NULL; /* Initialize to appease gcc warning. */
799 volatile struct gdb_exception except;
800
801 if (!PyArg_ParseTuple (args, "i", &i))
802 return NULL;
803
804 TRY_CATCH (except, RETURN_MASK_ALL)
805 {
806 res_val = access_value_history (i);
807 }
808 GDB_PY_HANDLE_EXCEPTION (except);
809
810 return value_to_value_object (res_val);
811 }
812
813 void
814 gdbpy_initialize_values (void)
815 {
816 if (PyType_Ready (&value_object_type) < 0)
817 return;
818
819 Py_INCREF (&value_object_type);
820 PyModule_AddObject (gdb_module, "Value", (PyObject *) &value_object_type);
821
822 values_in_python = NULL;
823 }
824
825 static PyMethodDef value_object_methods[] = {
826 { "address", valpy_address, METH_NOARGS, "Return the address of the value." },
827 { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." },
828 { "string", valpy_string, METH_VARARGS,
829 "Return Unicode string representation of the value." },
830 {NULL} /* Sentinel */
831 };
832
833 static PyNumberMethods value_object_as_number = {
834 valpy_add,
835 valpy_subtract,
836 valpy_multiply,
837 valpy_divide,
838 valpy_remainder,
839 NULL, /* nb_divmod */
840 valpy_power, /* nb_power */
841 valpy_negative, /* nb_negative */
842 valpy_positive, /* nb_positive */
843 valpy_absolute, /* nb_absolute */
844 valpy_nonzero, /* nb_nonzero */
845 valpy_invert, /* nb_invert */
846 valpy_lsh, /* nb_lshift */
847 valpy_rsh, /* nb_rshift */
848 valpy_and, /* nb_and */
849 valpy_xor, /* nb_xor */
850 valpy_or, /* nb_or */
851 NULL, /* nb_coerce */
852 valpy_int, /* nb_int */
853 valpy_long, /* nb_long */
854 valpy_float, /* nb_float */
855 NULL, /* nb_oct */
856 NULL /* nb_hex */
857 };
858
859 static PyMappingMethods value_object_as_mapping = {
860 valpy_length,
861 valpy_getitem,
862 valpy_setitem
863 };
864
865 PyTypeObject value_object_type = {
866 PyObject_HEAD_INIT (NULL)
867 0, /*ob_size*/
868 "gdb.Value", /*tp_name*/
869 sizeof (value_object), /*tp_basicsize*/
870 0, /*tp_itemsize*/
871 valpy_dealloc, /*tp_dealloc*/
872 0, /*tp_print*/
873 0, /*tp_getattr*/
874 0, /*tp_setattr*/
875 0, /*tp_compare*/
876 0, /*tp_repr*/
877 &value_object_as_number, /*tp_as_number*/
878 0, /*tp_as_sequence*/
879 &value_object_as_mapping, /*tp_as_mapping*/
880 0, /*tp_hash */
881 0, /*tp_call*/
882 valpy_str, /*tp_str*/
883 0, /*tp_getattro*/
884 0, /*tp_setattro*/
885 0, /*tp_as_buffer*/
886 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/
887 "GDB value object", /* tp_doc */
888 0, /* tp_traverse */
889 0, /* tp_clear */
890 valpy_richcompare, /* tp_richcompare */
891 0, /* tp_weaklistoffset */
892 0, /* tp_iter */
893 0, /* tp_iternext */
894 value_object_methods, /* tp_methods */
895 0, /* tp_members */
896 0, /* tp_getset */
897 0, /* tp_base */
898 0, /* tp_dict */
899 0, /* tp_descr_get */
900 0, /* tp_descr_set */
901 0, /* tp_dictoffset */
902 0, /* tp_init */
903 0, /* tp_alloc */
904 valpy_new /* tp_new */
905 };
906
907 #endif /* HAVE_PYTHON */