Commit | Line | Data |
---|---|---|
7843261b TJB |
1 | /* Python interface to values. |
2 | ||
0fb0cc75 | 3 | Copyright (C) 2008, 2009 Free Software Foundation, Inc. |
7843261b TJB |
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 "charset.h" | |
22 | #include "value.h" | |
23 | #include "exceptions.h" | |
24 | #include "language.h" | |
25 | #include "dfp.h" | |
79a45b7d | 26 | #include "valprint.h" |
7843261b TJB |
27 | |
28 | /* List of all values which are currently exposed to Python. It is | |
29 | maintained so that when an objfile is discarded, preserve_values | |
30 | can copy the values' types if needed. This is declared | |
31 | unconditionally to reduce the number of uses of HAVE_PYTHON in the | |
32 | generic code. */ | |
4be15b7a JB |
33 | /* This variable is unnecessarily initialized to NULL in order to |
34 | work around a linker bug on MacOS. */ | |
35 | struct value *values_in_python = NULL; | |
7843261b TJB |
36 | |
37 | #ifdef HAVE_PYTHON | |
38 | ||
39 | #include "python-internal.h" | |
40 | ||
41 | /* Even though Python scalar types directly map to host types, we use | |
42 | target types here to remain consistent with the the values system in | |
43 | GDB (which uses target arithmetic). */ | |
44 | ||
45 | /* Python's integer type corresponds to C's long type. */ | |
46 | #define builtin_type_pyint builtin_type (current_gdbarch)->builtin_long | |
47 | ||
48 | /* Python's float type corresponds to C's double type. */ | |
49 | #define builtin_type_pyfloat builtin_type (current_gdbarch)->builtin_double | |
50 | ||
51 | /* Python's long type corresponds to C's long long type. */ | |
52 | #define builtin_type_pylong builtin_type (current_gdbarch)->builtin_long_long | |
53 | ||
54 | #define builtin_type_pybool \ | |
55 | language_bool_type (current_language, current_gdbarch) | |
56 | ||
57 | typedef struct { | |
58 | PyObject_HEAD | |
59 | struct value *value; | |
60 | int owned_by_gdb; | |
61 | } value_object; | |
62 | ||
7843261b TJB |
63 | /* Called by the Python interpreter when deallocating a value object. */ |
64 | static void | |
65 | valpy_dealloc (PyObject *obj) | |
66 | { | |
67 | value_object *self = (value_object *) obj; | |
68 | ||
69 | value_remove_from_list (&values_in_python, self->value); | |
70 | ||
71 | if (!self->owned_by_gdb) | |
72 | value_free (self->value); | |
73 | self->ob_type->tp_free (self); | |
74 | } | |
75 | ||
76 | /* Called when a new gdb.Value object needs to be allocated. */ | |
77 | static PyObject * | |
78 | valpy_new (PyTypeObject *subtype, PyObject *args, PyObject *keywords) | |
79 | { | |
80 | struct value *value = NULL; /* Initialize to appease gcc warning. */ | |
81 | value_object *value_obj; | |
82 | volatile struct gdb_exception except; | |
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 | TRY_CATCH (except, RETURN_MASK_ALL) | |
100 | { | |
101 | value = convert_value_from_python (PyTuple_GetItem (args, 0)); | |
102 | } | |
103 | if (except.reason < 0) | |
104 | { | |
105 | subtype->tp_free (value_obj); | |
106 | return PyErr_Format (except.reason == RETURN_QUIT | |
107 | ? PyExc_KeyboardInterrupt : PyExc_TypeError, | |
108 | "%s", except.message); | |
109 | } | |
110 | ||
111 | value_obj->value = value; | |
997339e8 | 112 | value_obj->owned_by_gdb = 0; |
7843261b TJB |
113 | release_value (value); |
114 | value_prepend_to_list (&values_in_python, value); | |
115 | ||
116 | return (PyObject *) value_obj; | |
117 | } | |
118 | ||
119 | /* Given a value of a pointer type, apply the C unary * operator to it. */ | |
120 | static PyObject * | |
121 | valpy_dereference (PyObject *self, PyObject *args) | |
122 | { | |
123 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
124 | volatile struct gdb_exception except; | |
125 | ||
126 | TRY_CATCH (except, RETURN_MASK_ALL) | |
127 | { | |
128 | res_val = value_ind (((value_object *) self)->value); | |
129 | } | |
130 | GDB_PY_HANDLE_EXCEPTION (except); | |
131 | ||
132 | return value_to_value_object (res_val); | |
133 | } | |
134 | ||
135 | static Py_ssize_t | |
136 | valpy_length (PyObject *self) | |
137 | { | |
138 | /* We don't support getting the number of elements in a struct / class. */ | |
139 | PyErr_SetString (PyExc_NotImplementedError, | |
140 | "Invalid operation on gdb.Value."); | |
141 | return -1; | |
142 | } | |
143 | ||
144 | /* Given string name of an element inside structure, return its value | |
145 | object. */ | |
146 | static PyObject * | |
147 | valpy_getitem (PyObject *self, PyObject *key) | |
148 | { | |
149 | value_object *self_value = (value_object *) self; | |
150 | char *field; | |
151 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
152 | struct cleanup *old; | |
153 | volatile struct gdb_exception except; | |
154 | ||
155 | field = python_string_to_target_string (key); | |
156 | if (field == NULL) | |
157 | return NULL; | |
158 | ||
159 | old = make_cleanup (xfree, field); | |
160 | ||
161 | TRY_CATCH (except, RETURN_MASK_ALL) | |
162 | { | |
3c0ed299 PP |
163 | struct value *tmp = self_value->value; |
164 | res_val = value_struct_elt (&tmp, NULL, field, 0, NULL); | |
7843261b TJB |
165 | } |
166 | GDB_PY_HANDLE_EXCEPTION (except); | |
167 | ||
168 | do_cleanups (old); | |
169 | ||
170 | return value_to_value_object (res_val); | |
171 | } | |
172 | ||
173 | static int | |
174 | valpy_setitem (PyObject *self, PyObject *key, PyObject *value) | |
175 | { | |
176 | PyErr_Format (PyExc_NotImplementedError, | |
177 | _("Setting of struct elements is not currently supported.")); | |
178 | return -1; | |
179 | } | |
180 | ||
181 | /* Called by the Python interpreter to obtain string representation | |
182 | of the object. */ | |
183 | static PyObject * | |
184 | valpy_str (PyObject *self) | |
185 | { | |
186 | char *s = NULL; | |
187 | long dummy; | |
188 | struct ui_file *stb; | |
189 | struct cleanup *old_chain; | |
190 | PyObject *result; | |
79a45b7d | 191 | struct value_print_options opts; |
7843261b TJB |
192 | volatile struct gdb_exception except; |
193 | ||
79a45b7d TT |
194 | get_user_print_options (&opts); |
195 | opts.deref_ref = 0; | |
196 | ||
7843261b TJB |
197 | stb = mem_fileopen (); |
198 | old_chain = make_cleanup_ui_file_delete (stb); | |
199 | ||
200 | TRY_CATCH (except, RETURN_MASK_ALL) | |
201 | { | |
79a45b7d TT |
202 | common_val_print (((value_object *) self)->value, stb, 0, |
203 | &opts, current_language); | |
7843261b TJB |
204 | s = ui_file_xstrdup (stb, &dummy); |
205 | } | |
206 | GDB_PY_HANDLE_EXCEPTION (except); | |
207 | ||
208 | do_cleanups (old_chain); | |
209 | ||
210 | result = PyUnicode_Decode (s, strlen (s), host_charset (), NULL); | |
211 | xfree (s); | |
212 | ||
213 | return result; | |
214 | } | |
215 | ||
216 | enum valpy_opcode | |
217 | { | |
218 | VALPY_ADD, | |
219 | VALPY_SUB, | |
220 | VALPY_MUL, | |
221 | VALPY_DIV, | |
222 | VALPY_REM, | |
223 | VALPY_POW | |
224 | }; | |
225 | ||
226 | /* If TYPE is a reference, return the target; otherwise return TYPE. */ | |
227 | #define STRIP_REFERENCE(TYPE) \ | |
228 | ((TYPE_CODE (TYPE) == TYPE_CODE_REF) ? (TYPE_TARGET_TYPE (TYPE)) : (TYPE)) | |
229 | ||
230 | /* Returns a value object which is the result of applying the operation | |
231 | specified by OPCODE to the given arguments. */ | |
232 | static PyObject * | |
233 | valpy_binop (enum valpy_opcode opcode, PyObject *self, PyObject *other) | |
234 | { | |
235 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
236 | volatile struct gdb_exception except; | |
237 | ||
238 | TRY_CATCH (except, RETURN_MASK_ALL) | |
239 | { | |
240 | struct value *arg1, *arg2; | |
241 | ||
242 | /* If the gdb.Value object is the second operand, then it will be passed | |
243 | to us as the OTHER argument, and SELF will be an entirely different | |
244 | kind of object, altogether. Because of this, we can't assume self is | |
245 | a gdb.Value object and need to convert it from python as well. */ | |
246 | arg1 = convert_value_from_python (self); | |
247 | arg2 = convert_value_from_python (other); | |
248 | ||
249 | switch (opcode) | |
250 | { | |
251 | case VALPY_ADD: | |
252 | { | |
253 | struct type *ltype = value_type (arg1); | |
254 | struct type *rtype = value_type (arg2); | |
255 | ||
256 | CHECK_TYPEDEF (ltype); | |
257 | ltype = STRIP_REFERENCE (ltype); | |
258 | CHECK_TYPEDEF (rtype); | |
259 | rtype = STRIP_REFERENCE (rtype); | |
260 | ||
261 | if (TYPE_CODE (ltype) == TYPE_CODE_PTR) | |
262 | res_val = value_ptradd (arg1, arg2); | |
263 | else if (TYPE_CODE (rtype) == TYPE_CODE_PTR) | |
264 | res_val = value_ptradd (arg2, arg1); | |
265 | else | |
266 | res_val = value_binop (arg1, arg2, BINOP_ADD); | |
267 | } | |
268 | break; | |
269 | case VALPY_SUB: | |
270 | { | |
271 | struct type *ltype = value_type (arg1); | |
272 | struct type *rtype = value_type (arg2); | |
273 | ||
274 | CHECK_TYPEDEF (ltype); | |
275 | ltype = STRIP_REFERENCE (ltype); | |
276 | CHECK_TYPEDEF (rtype); | |
277 | rtype = STRIP_REFERENCE (rtype); | |
278 | ||
279 | if (TYPE_CODE (ltype) == TYPE_CODE_PTR) | |
280 | { | |
281 | if (TYPE_CODE (rtype) == TYPE_CODE_PTR) | |
282 | /* A ptrdiff_t for the target would be preferable | |
283 | here. */ | |
284 | res_val = value_from_longest (builtin_type_pyint, | |
285 | value_ptrdiff (arg1, arg2)); | |
286 | else | |
287 | res_val = value_ptrsub (arg1, arg2); | |
288 | } | |
289 | else | |
290 | res_val = value_binop (arg1, arg2, BINOP_SUB); | |
291 | } | |
292 | break; | |
293 | case VALPY_MUL: | |
294 | res_val = value_binop (arg1, arg2, BINOP_MUL); | |
295 | break; | |
296 | case VALPY_DIV: | |
297 | res_val = value_binop (arg1, arg2, BINOP_DIV); | |
298 | break; | |
299 | case VALPY_REM: | |
300 | res_val = value_binop (arg1, arg2, BINOP_REM); | |
301 | break; | |
302 | case VALPY_POW: | |
303 | res_val = value_binop (arg1, arg2, BINOP_EXP); | |
304 | break; | |
305 | } | |
306 | } | |
307 | GDB_PY_HANDLE_EXCEPTION (except); | |
308 | ||
309 | return value_to_value_object (res_val); | |
310 | } | |
311 | ||
312 | static PyObject * | |
313 | valpy_add (PyObject *self, PyObject *other) | |
314 | { | |
315 | return valpy_binop (VALPY_ADD, self, other); | |
316 | } | |
317 | ||
318 | static PyObject * | |
319 | valpy_subtract (PyObject *self, PyObject *other) | |
320 | { | |
321 | return valpy_binop (VALPY_SUB, self, other); | |
322 | } | |
323 | ||
324 | static PyObject * | |
325 | valpy_multiply (PyObject *self, PyObject *other) | |
326 | { | |
327 | return valpy_binop (VALPY_MUL, self, other); | |
328 | } | |
329 | ||
330 | static PyObject * | |
331 | valpy_divide (PyObject *self, PyObject *other) | |
332 | { | |
333 | return valpy_binop (VALPY_DIV, self, other); | |
334 | } | |
335 | ||
336 | static PyObject * | |
337 | valpy_remainder (PyObject *self, PyObject *other) | |
338 | { | |
339 | return valpy_binop (VALPY_REM, self, other); | |
340 | } | |
341 | ||
342 | static PyObject * | |
343 | valpy_power (PyObject *self, PyObject *other, PyObject *unused) | |
344 | { | |
345 | /* We don't support the ternary form of pow. I don't know how to express | |
346 | that, so let's just throw NotImplementedError to at least do something | |
347 | about it. */ | |
348 | if (unused != Py_None) | |
349 | { | |
350 | PyErr_SetString (PyExc_NotImplementedError, | |
351 | "Invalid operation on gdb.Value."); | |
352 | return NULL; | |
353 | } | |
354 | ||
355 | return valpy_binop (VALPY_POW, self, other); | |
356 | } | |
357 | ||
358 | static PyObject * | |
359 | valpy_negative (PyObject *self) | |
360 | { | |
361 | struct value *val = NULL; | |
362 | volatile struct gdb_exception except; | |
363 | ||
364 | TRY_CATCH (except, RETURN_MASK_ALL) | |
365 | { | |
366 | val = value_neg (((value_object *) self)->value); | |
367 | } | |
368 | GDB_PY_HANDLE_EXCEPTION (except); | |
369 | ||
370 | return value_to_value_object (val); | |
371 | } | |
372 | ||
373 | static PyObject * | |
374 | valpy_positive (PyObject *self) | |
375 | { | |
376 | struct value *copy = value_copy (((value_object *) self)->value); | |
377 | ||
378 | return value_to_value_object (copy); | |
379 | } | |
380 | ||
381 | static PyObject * | |
382 | valpy_absolute (PyObject *self) | |
383 | { | |
384 | if (value_less (((value_object *) self)->value, | |
385 | value_from_longest (builtin_type_int8, 0))) | |
386 | return valpy_negative (self); | |
387 | else | |
388 | return valpy_positive (self); | |
389 | } | |
390 | ||
391 | /* Implements boolean evaluation of gdb.Value. */ | |
392 | static int | |
393 | valpy_nonzero (PyObject *self) | |
394 | { | |
395 | value_object *self_value = (value_object *) self; | |
396 | struct type *type; | |
397 | ||
398 | type = check_typedef (value_type (self_value->value)); | |
399 | ||
400 | if (is_integral_type (type) || TYPE_CODE (type) == TYPE_CODE_PTR) | |
401 | return !!value_as_long (self_value->value); | |
402 | else if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
403 | return value_as_double (self_value->value) != 0; | |
404 | else if (TYPE_CODE (type) == TYPE_CODE_DECFLOAT) | |
405 | return !decimal_is_zero (value_contents (self_value->value), | |
406 | TYPE_LENGTH (type)); | |
407 | else | |
408 | { | |
409 | PyErr_SetString (PyExc_TypeError, _("Attempted truth testing on invalid " | |
410 | "gdb.Value type.")); | |
411 | return 0; | |
412 | } | |
413 | } | |
414 | ||
415 | /* Implements comparison operations for value objects. */ | |
416 | static PyObject * | |
417 | valpy_richcompare (PyObject *self, PyObject *other, int op) | |
418 | { | |
419 | int result = 0; | |
420 | struct value *value_self, *value_other; | |
421 | volatile struct gdb_exception except; | |
422 | ||
423 | if (PyObject_TypeCheck (other, &value_object_type)) | |
424 | value_other = ((value_object *) other)->value; | |
425 | else if (PyInt_Check (other)) | |
426 | { | |
427 | LONGEST l; | |
428 | ||
429 | l = PyInt_AsLong (other); | |
430 | if (PyErr_Occurred ()) | |
431 | return NULL; | |
432 | ||
433 | value_other = value_from_longest (builtin_type_pyint, l); | |
434 | } | |
435 | else if (PyFloat_Check (other)) | |
436 | { | |
437 | DOUBLEST d; | |
438 | ||
439 | d = PyFloat_AsDouble (other); | |
440 | if (PyErr_Occurred ()) | |
441 | return NULL; | |
442 | ||
443 | value_other = value_from_double (builtin_type_pyfloat, d); | |
444 | } | |
445 | else if (PyString_Check (other) || PyUnicode_Check (other)) | |
446 | { | |
447 | char *str; | |
448 | ||
449 | str = python_string_to_target_string (other); | |
450 | value_other = value_from_string (str); | |
451 | xfree (str); | |
452 | } | |
453 | else if (other == Py_None) | |
454 | /* Comparing with None is special. From what I can tell, in Python | |
455 | None is smaller than anything else. */ | |
456 | switch (op) { | |
457 | case Py_LT: | |
458 | case Py_LE: | |
459 | case Py_EQ: | |
460 | Py_RETURN_FALSE; | |
461 | case Py_NE: | |
462 | case Py_GT: | |
463 | case Py_GE: | |
464 | Py_RETURN_TRUE; | |
465 | default: | |
466 | /* Can't happen. */ | |
467 | PyErr_SetString (PyExc_NotImplementedError, | |
468 | "Invalid operation on gdb.Value."); | |
469 | return NULL; | |
470 | } | |
471 | else | |
472 | { | |
473 | PyErr_SetString (PyExc_NotImplementedError, | |
474 | "Operation not supported on gdb.Value of this type."); | |
475 | return NULL; | |
476 | } | |
477 | ||
478 | TRY_CATCH (except, RETURN_MASK_ALL) | |
479 | { | |
480 | switch (op) { | |
481 | case Py_LT: | |
482 | result = value_less (((value_object *) self)->value, value_other); | |
483 | break; | |
484 | case Py_LE: | |
485 | result = value_less (((value_object *) self)->value, value_other) | |
486 | || value_equal (((value_object *) self)->value, value_other); | |
487 | break; | |
488 | case Py_EQ: | |
489 | result = value_equal (((value_object *) self)->value, value_other); | |
490 | break; | |
491 | case Py_NE: | |
492 | result = !value_equal (((value_object *) self)->value, value_other); | |
493 | break; | |
494 | case Py_GT: | |
495 | result = value_less (value_other, ((value_object *) self)->value); | |
496 | break; | |
497 | case Py_GE: | |
498 | result = value_less (value_other, ((value_object *) self)->value) | |
499 | || value_equal (((value_object *) self)->value, value_other); | |
500 | break; | |
501 | default: | |
502 | /* Can't happen. */ | |
503 | PyErr_SetString (PyExc_NotImplementedError, | |
504 | "Invalid operation on gdb.Value."); | |
505 | return NULL; | |
506 | } | |
507 | } | |
508 | GDB_PY_HANDLE_EXCEPTION (except); | |
509 | ||
510 | if (result == 1) | |
511 | Py_RETURN_TRUE; | |
512 | ||
513 | Py_RETURN_FALSE; | |
514 | } | |
515 | ||
516 | /* Returns an object for a value which is released from the all_values chain, | |
517 | so its lifetime is not bound to the execution of a command. */ | |
518 | PyObject * | |
519 | value_to_value_object (struct value *val) | |
520 | { | |
521 | value_object *val_obj; | |
522 | ||
523 | val_obj = PyObject_New (value_object, &value_object_type); | |
524 | if (val_obj != NULL) | |
525 | { | |
526 | val_obj->value = val; | |
997339e8 | 527 | val_obj->owned_by_gdb = 0; |
7843261b TJB |
528 | release_value (val); |
529 | value_prepend_to_list (&values_in_python, val); | |
530 | } | |
531 | ||
532 | return (PyObject *) val_obj; | |
533 | } | |
534 | ||
535 | /* Try to convert a Python value to a gdb value. If the value cannot | |
536 | be converted, throw a gdb exception. */ | |
537 | ||
538 | struct value * | |
539 | convert_value_from_python (PyObject *obj) | |
540 | { | |
541 | struct value *value = NULL; /* -Wall */ | |
542 | PyObject *target_str, *unicode_str; | |
543 | struct cleanup *old; | |
544 | ||
545 | if (! obj) | |
546 | error (_("Internal error while converting Python value.")); | |
547 | ||
548 | if (PyBool_Check (obj)) | |
549 | value = value_from_longest (builtin_type_pybool, obj == Py_True); | |
550 | else if (PyInt_Check (obj)) | |
551 | value = value_from_longest (builtin_type_pyint, PyInt_AsLong (obj)); | |
552 | else if (PyLong_Check (obj)) | |
553 | { | |
554 | LONGEST l = PyLong_AsLongLong (obj); | |
555 | if (! PyErr_Occurred ()) | |
556 | value = value_from_longest (builtin_type_pylong, l); | |
557 | } | |
558 | else if (PyFloat_Check (obj)) | |
559 | { | |
560 | double d = PyFloat_AsDouble (obj); | |
561 | if (! PyErr_Occurred ()) | |
562 | value = value_from_double (builtin_type_pyfloat, d); | |
563 | } | |
564 | else if (PyString_Check (obj) || PyUnicode_Check (obj)) | |
565 | { | |
566 | char *s; | |
567 | ||
568 | s = python_string_to_target_string (obj); | |
569 | if (s == NULL) | |
570 | return NULL; | |
571 | ||
572 | old = make_cleanup (xfree, s); | |
573 | value = value_from_string (s); | |
574 | do_cleanups (old); | |
575 | } | |
576 | else if (PyObject_TypeCheck (obj, &value_object_type)) | |
577 | value = ((value_object *) obj)->value; | |
578 | else | |
579 | error (_("Could not convert Python object: %s"), | |
580 | PyString_AsString (PyObject_Str (obj))); | |
581 | ||
582 | if (PyErr_Occurred ()) | |
583 | error (_("Error converting Python value.")); | |
584 | ||
585 | return value; | |
586 | } | |
587 | ||
588 | /* Returns value object in the ARGth position in GDB's history. */ | |
589 | PyObject * | |
590 | gdbpy_get_value_from_history (PyObject *self, PyObject *args) | |
591 | { | |
592 | int i; | |
593 | struct value *res_val = NULL; /* Initialize to appease gcc warning. */ | |
594 | volatile struct gdb_exception except; | |
595 | ||
596 | if (!PyArg_ParseTuple (args, "i", &i)) | |
597 | return NULL; | |
598 | ||
599 | TRY_CATCH (except, RETURN_MASK_ALL) | |
600 | { | |
601 | res_val = access_value_history (i); | |
602 | } | |
603 | GDB_PY_HANDLE_EXCEPTION (except); | |
604 | ||
605 | return value_to_value_object (res_val); | |
606 | } | |
607 | ||
608 | void | |
609 | gdbpy_initialize_values (void) | |
610 | { | |
611 | value_object_type.tp_new = valpy_new; | |
612 | if (PyType_Ready (&value_object_type) < 0) | |
613 | return; | |
614 | ||
615 | Py_INCREF (&value_object_type); | |
616 | PyModule_AddObject (gdb_module, "Value", (PyObject *) &value_object_type); | |
617 | ||
618 | values_in_python = NULL; | |
619 | } | |
620 | ||
f9176c46 PA |
621 | static PyMethodDef value_object_methods[] = { |
622 | { "dereference", valpy_dereference, METH_NOARGS, "Dereferences the value." }, | |
623 | {NULL} /* Sentinel */ | |
624 | }; | |
625 | ||
626 | static PyNumberMethods value_object_as_number = { | |
627 | valpy_add, | |
628 | valpy_subtract, | |
629 | valpy_multiply, | |
630 | valpy_divide, | |
631 | valpy_remainder, | |
632 | NULL, /* nb_divmod */ | |
633 | valpy_power, /* nb_power */ | |
634 | valpy_negative, /* nb_negative */ | |
635 | valpy_positive, /* nb_positive */ | |
636 | valpy_absolute, /* nb_absolute */ | |
637 | valpy_nonzero /* nb_nonzero */ | |
638 | }; | |
639 | ||
640 | static PyMappingMethods value_object_as_mapping = { | |
641 | valpy_length, | |
642 | valpy_getitem, | |
643 | valpy_setitem | |
644 | }; | |
645 | ||
646 | PyTypeObject value_object_type = { | |
647 | PyObject_HEAD_INIT (NULL) | |
648 | 0, /*ob_size*/ | |
649 | "gdb.Value", /*tp_name*/ | |
650 | sizeof (value_object), /*tp_basicsize*/ | |
651 | 0, /*tp_itemsize*/ | |
652 | valpy_dealloc, /*tp_dealloc*/ | |
653 | 0, /*tp_print*/ | |
654 | 0, /*tp_getattr*/ | |
655 | 0, /*tp_setattr*/ | |
656 | 0, /*tp_compare*/ | |
657 | 0, /*tp_repr*/ | |
658 | &value_object_as_number, /*tp_as_number*/ | |
659 | 0, /*tp_as_sequence*/ | |
660 | &value_object_as_mapping, /*tp_as_mapping*/ | |
661 | 0, /*tp_hash */ | |
662 | 0, /*tp_call*/ | |
663 | valpy_str, /*tp_str*/ | |
664 | 0, /*tp_getattro*/ | |
665 | 0, /*tp_setattro*/ | |
666 | 0, /*tp_as_buffer*/ | |
667 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/ | |
668 | "GDB value object", /* tp_doc */ | |
669 | 0, /* tp_traverse */ | |
670 | 0, /* tp_clear */ | |
671 | valpy_richcompare, /* tp_richcompare */ | |
672 | 0, /* tp_weaklistoffset */ | |
673 | 0, /* tp_iter */ | |
674 | 0, /* tp_iternext */ | |
675 | value_object_methods /* tp_methods */ | |
676 | }; | |
677 | ||
7843261b | 678 | #endif /* HAVE_PYTHON */ |