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