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