]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-param.c
* python/py-evts.c (gdbpy_initialize_py_events): Don't
[thirdparty/binutils-gdb.git] / gdb / python / py-param.c
CommitLineData
d7b32ed3
PM
1/* GDB parameters implemented in Python
2
28e7fd62 3 Copyright (C) 2008-2013 Free Software Foundation, Inc.
d7b32ed3
PM
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
21#include "defs.h"
22#include "value.h"
23#include "exceptions.h"
24#include "python-internal.h"
25#include "charset.h"
26#include "gdbcmd.h"
27#include "cli/cli-decode.h"
28#include "completer.h"
ecec24e6
PM
29#include "language.h"
30#include "arch-utils.h"
d7b32ed3
PM
31
32/* Parameter constants and their values. */
33struct parm_constant
34{
35 char *name;
36 int value;
37};
38
39struct parm_constant parm_constants[] =
40{
3c0ee1a4 41 { "PARAM_BOOLEAN", var_boolean }, /* ARI: var_boolean */
d7b32ed3
PM
42 { "PARAM_AUTO_BOOLEAN", var_auto_boolean },
43 { "PARAM_UINTEGER", var_uinteger },
44 { "PARAM_INTEGER", var_integer },
45 { "PARAM_STRING", var_string },
46 { "PARAM_STRING_NOESCAPE", var_string_noescape },
47 { "PARAM_OPTIONAL_FILENAME", var_optional_filename },
48 { "PARAM_FILENAME", var_filename },
49 { "PARAM_ZINTEGER", var_zinteger },
50 { "PARAM_ENUM", var_enum },
51 { NULL, 0 }
52};
53
54/* A union that can hold anything described by enum var_types. */
55union parmpy_variable
56{
57 /* Hold an integer value, for boolean and integer types. */
58 int intval;
59
60 /* Hold an auto_boolean. */
61 enum auto_boolean autoboolval;
62
63 /* Hold an unsigned integer value, for uinteger. */
64 unsigned int uintval;
65
66 /* Hold a string, for the various string types. */
67 char *stringval;
68
69 /* Hold a string, for enums. */
70 const char *cstringval;
71};
72
73/* A GDB parameter. */
74struct parmpy_object
75{
76 PyObject_HEAD
77
78 /* The type of the parameter. */
79 enum var_types type;
80
81 /* The value of the parameter. */
82 union parmpy_variable value;
83
84 /* For an enum command, the possible values. The vector is
85 allocated with xmalloc, as is each element. It is
86 NULL-terminated. */
87 const char **enumeration;
88};
89
90typedef struct parmpy_object parmpy_object;
91
62eec1a5
TT
92static PyTypeObject parmpy_object_type
93 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("parmpy_object");
d7b32ed3
PM
94
95/* Some handy string constants. */
96static PyObject *set_doc_cst;
97static PyObject *show_doc_cst;
98
99\f
100
101/* Get an attribute. */
102static PyObject *
103get_attr (PyObject *obj, PyObject *attr_name)
104{
105 if (PyString_Check (attr_name)
9a27f2c6
PK
106#ifdef IS_PY3K
107 && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
108#else
d7b32ed3 109 && ! strcmp (PyString_AsString (attr_name), "value"))
9a27f2c6 110#endif
d7b32ed3
PM
111 {
112 parmpy_object *self = (parmpy_object *) obj;
d59b6f6c 113
d7b32ed3
PM
114 return gdbpy_parameter_value (self->type, &self->value);
115 }
116
117 return PyObject_GenericGetAttr (obj, attr_name);
118}
119
8dc78533
JK
120/* Set a parameter value from a Python value. Return 0 on success. Returns
121 -1 on error, with a python exception set. */
d7b32ed3
PM
122static int
123set_parameter_value (parmpy_object *self, PyObject *value)
124{
125 int cmp;
126
127 switch (self->type)
128 {
129 case var_string:
130 case var_string_noescape:
131 case var_optional_filename:
132 case var_filename:
133 if (! gdbpy_is_string (value)
134 && (self->type == var_filename
135 || value != Py_None))
136 {
137 PyErr_SetString (PyExc_RuntimeError,
138 _("String required for filename."));
139
140 return -1;
141 }
d7b32ed3
PM
142 if (value == Py_None)
143 {
8dc78533 144 xfree (self->value.stringval);
d7b32ed3
PM
145 if (self->type == var_optional_filename)
146 self->value.stringval = xstrdup ("");
147 else
148 self->value.stringval = NULL;
149 }
150 else
8dc78533
JK
151 {
152 char *string;
153
154 string = python_string_to_host_string (value);
155 if (string == NULL)
156 return -1;
157
158 xfree (self->value.stringval);
159 self->value.stringval = string;
160 }
d7b32ed3
PM
161 break;
162
163 case var_enum:
164 {
165 int i;
166 char *str;
167
168 if (! gdbpy_is_string (value))
169 {
170 PyErr_SetString (PyExc_RuntimeError,
171 _("ENUM arguments must be a string."));
172 return -1;
173 }
174
175 str = python_string_to_host_string (value);
8dc78533
JK
176 if (str == NULL)
177 return -1;
d7b32ed3
PM
178 for (i = 0; self->enumeration[i]; ++i)
179 if (! strcmp (self->enumeration[i], str))
180 break;
181 xfree (str);
182 if (! self->enumeration[i])
183 {
184 PyErr_SetString (PyExc_RuntimeError,
185 _("The value must be member of an enumeration."));
186 return -1;
187 }
188 self->value.cstringval = self->enumeration[i];
189 break;
190 }
191
192 case var_boolean:
193 if (! PyBool_Check (value))
194 {
195 PyErr_SetString (PyExc_RuntimeError,
196 _("A boolean argument is required."));
197 return -1;
198 }
199 cmp = PyObject_IsTrue (value);
200 if (cmp < 0)
201 return -1;
202 self->value.intval = cmp;
203 break;
204
205 case var_auto_boolean:
206 if (! PyBool_Check (value) && value != Py_None)
207 {
208 PyErr_SetString (PyExc_RuntimeError,
209 _("A boolean or None is required"));
210 return -1;
211 }
212
213 if (value == Py_None)
214 self->value.autoboolval = AUTO_BOOLEAN_AUTO;
215 else
216 {
217 cmp = PyObject_IsTrue (value);
218 if (cmp < 0 )
219 return -1;
220 if (cmp == 1)
221 self->value.autoboolval = AUTO_BOOLEAN_TRUE;
222 else
223 self->value.autoboolval = AUTO_BOOLEAN_FALSE;
d7b32ed3 224 }
92e96192 225 break;
d7b32ed3
PM
226
227 case var_integer:
228 case var_zinteger:
229 case var_uinteger:
230 {
231 long l;
232 int ok;
233
234 if (! PyInt_Check (value))
235 {
236 PyErr_SetString (PyExc_RuntimeError,
237 _("The value must be integer."));
238 return -1;
239 }
240
74aedc46
TT
241 if (! gdb_py_int_as_long (value, &l))
242 return -1;
243
d7b32ed3
PM
244 if (self->type == var_uinteger)
245 {
246 ok = (l >= 0 && l <= UINT_MAX);
247 if (l == 0)
248 l = UINT_MAX;
249 }
250 else if (self->type == var_integer)
251 {
252 ok = (l >= INT_MIN && l <= INT_MAX);
253 if (l == 0)
254 l = INT_MAX;
255 }
256 else
257 ok = (l >= INT_MIN && l <= INT_MAX);
258
259 if (! ok)
260 {
261 PyErr_SetString (PyExc_RuntimeError,
262 _("Range exceeded."));
263 return -1;
264 }
265
266 self->value.intval = (int) l;
267 break;
268 }
269
270 default:
271 PyErr_SetString (PyExc_RuntimeError,
272 _("Unhandled type in parameter value."));
273 return -1;
274 }
275
276 return 0;
277}
278
8dc78533 279/* Set an attribute. Returns -1 on error, with a python exception set. */
d7b32ed3
PM
280static int
281set_attr (PyObject *obj, PyObject *attr_name, PyObject *val)
282{
283 if (PyString_Check (attr_name)
9a27f2c6
PK
284#ifdef IS_PY3K
285 && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
286#else
d7b32ed3 287 && ! strcmp (PyString_AsString (attr_name), "value"))
9a27f2c6 288#endif
d7b32ed3
PM
289 {
290 if (!val)
291 {
292 PyErr_SetString (PyExc_RuntimeError,
293 _("Cannot delete a parameter's value."));
294 return -1;
295 }
296 return set_parameter_value ((parmpy_object *) obj, val);
297 }
298
299 return PyObject_GenericSetAttr (obj, attr_name, val);
300}
301
ecec24e6
PM
302/* A helper function which returns a documentation string for an
303 object. */
304
305static char *
306get_doc_string (PyObject *object, PyObject *attr)
307{
308 char *result = NULL;
309
310 if (PyObject_HasAttr (object, attr))
311 {
312 PyObject *ds_obj = PyObject_GetAttr (object, attr);
313
314 if (ds_obj && gdbpy_is_string (ds_obj))
315 {
316 result = python_string_to_host_string (ds_obj);
317 if (result == NULL)
318 gdbpy_print_stack ();
319 }
320 Py_XDECREF (ds_obj);
321 }
322 if (! result)
323 result = xstrdup (_("This command is not documented."));
324 return result;
325}
326
327/* Helper function which will execute a METHOD in OBJ passing the
328 argument ARG. ARG can be NULL. METHOD should return a Python
329 string. If this function returns NULL, there has been an error and
330 the appropriate exception set. */
331static char *
332call_doc_function (PyObject *obj, PyObject *method, PyObject *arg)
333{
334 char *data = NULL;
335 PyObject *result = PyObject_CallMethodObjArgs (obj, method, arg, NULL);
336
337 if (! result)
338 return NULL;
339
340 if (gdbpy_is_string (result))
341 {
342 data = python_string_to_host_string (result);
8432bc41 343 Py_DECREF (result);
ecec24e6
PM
344 if (! data)
345 return NULL;
346 }
347 else
348 {
349 PyErr_SetString (PyExc_RuntimeError,
350 _("Parameter must return a string value."));
8432bc41 351 Py_DECREF (result);
ecec24e6
PM
352 return NULL;
353 }
354
355 return data;
356}
357
358/* A callback function that is registered against the respective
359 add_setshow_* set_doc prototype. This function will either call
360 the Python function "get_set_string" or extract the Python
361 attribute "set_doc" and return the contents as a string. If
362 neither exist, insert a string indicating the Parameter is not
363 documented. */
364static void
365get_set_value (char *args, int from_tty,
366 struct cmd_list_element *c)
367{
368 PyObject *obj = (PyObject *) get_cmd_context (c);
369 char *set_doc_string;
370 struct cleanup *cleanup = ensure_python_env (get_current_arch (),
371 current_language);
372 PyObject *set_doc_func = PyString_FromString ("get_set_string");
373
374 if (! set_doc_func)
375 goto error;
376
377 make_cleanup_py_decref (set_doc_func);
378
379 if (PyObject_HasAttr (obj, set_doc_func))
380 {
381 set_doc_string = call_doc_function (obj, set_doc_func, NULL);
382 if (! set_doc_string)
383 goto error;
384 }
385 else
386 {
387 /* We have to preserve the existing < GDB 7.3 API. If a
388 callback function does not exist, then attempt to read the
389 set_doc attribute. */
390 set_doc_string = get_doc_string (obj, set_doc_cst);
391 }
392
393 make_cleanup (xfree, set_doc_string);
394 fprintf_filtered (gdb_stdout, "%s\n", set_doc_string);
395
396 do_cleanups (cleanup);
397 return;
398
399 error:
400 gdbpy_print_stack ();
401 do_cleanups (cleanup);
402 return;
403}
404
405/* A callback function that is registered against the respective
406 add_setshow_* show_doc prototype. This function will either call
407 the Python function "get_show_string" or extract the Python
408 attribute "show_doc" and return the contents as a string. If
409 neither exist, insert a string indicating the Parameter is not
410 documented. */
411static void
412get_show_value (struct ui_file *file, int from_tty,
413 struct cmd_list_element *c,
414 const char *value)
415{
416 PyObject *obj = (PyObject *) get_cmd_context (c);
417 char *show_doc_string = NULL;
418 struct cleanup *cleanup = ensure_python_env (get_current_arch (),
419 current_language);
420 PyObject *show_doc_func = PyString_FromString ("get_show_string");
421
422 if (! show_doc_func)
423 goto error;
424
425 make_cleanup_py_decref (show_doc_func);
426
427 if (PyObject_HasAttr (obj, show_doc_func))
428 {
429 PyObject *val_obj = PyString_FromString (value);
430
431 if (! val_obj)
432 goto error;
433
434 make_cleanup_py_decref (val_obj);
435
436 show_doc_string = call_doc_function (obj, show_doc_func, val_obj);
437 if (! show_doc_string)
438 goto error;
439
440 make_cleanup (xfree, show_doc_string);
441
442 fprintf_filtered (file, "%s\n", show_doc_string);
443 }
444 else
445 {
446 /* We have to preserve the existing < GDB 7.3 API. If a
447 callback function does not exist, then attempt to read the
448 show_doc attribute. */
449 show_doc_string = get_doc_string (obj, show_doc_cst);
450 make_cleanup (xfree, show_doc_string);
451 fprintf_filtered (file, "%s %s\n", show_doc_string, value);
452 }
453
454 do_cleanups (cleanup);
455 return;
456
457 error:
458 gdbpy_print_stack ();
459 do_cleanups (cleanup);
460 return;
461}
d7b32ed3
PM
462\f
463
464/* A helper function that dispatches to the appropriate add_setshow
465 function. */
466static void
467add_setshow_generic (int parmclass, enum command_class cmdclass,
468 char *cmd_name, parmpy_object *self,
469 char *set_doc, char *show_doc, char *help_doc,
470 struct cmd_list_element **set_list,
471 struct cmd_list_element **show_list)
472{
ecec24e6 473 struct cmd_list_element *param = NULL;
6f937416 474 const char *tmp_name = NULL;
ecec24e6 475
d7b32ed3
PM
476 switch (parmclass)
477 {
478 case var_boolean:
ecec24e6
PM
479
480 add_setshow_boolean_cmd (cmd_name, cmdclass,
481 &self->value.intval, set_doc, show_doc,
482 help_doc, get_set_value, get_show_value,
483 set_list, show_list);
484
d7b32ed3
PM
485 break;
486
487 case var_auto_boolean:
488 add_setshow_auto_boolean_cmd (cmd_name, cmdclass,
489 &self->value.autoboolval,
490 set_doc, show_doc, help_doc,
ecec24e6
PM
491 get_set_value, get_show_value,
492 set_list, show_list);
d7b32ed3
PM
493 break;
494
495 case var_uinteger:
ecec24e6
PM
496 add_setshow_uinteger_cmd (cmd_name, cmdclass,
497 &self->value.uintval, set_doc, show_doc,
498 help_doc, get_set_value, get_show_value,
499 set_list, show_list);
d7b32ed3
PM
500 break;
501
502 case var_integer:
ecec24e6
PM
503 add_setshow_integer_cmd (cmd_name, cmdclass,
504 &self->value.intval, set_doc, show_doc,
505 help_doc, get_set_value, get_show_value,
506 set_list, show_list); break;
d7b32ed3
PM
507
508 case var_string:
ecec24e6
PM
509 add_setshow_string_cmd (cmd_name, cmdclass,
510 &self->value.stringval, set_doc, show_doc,
511 help_doc, get_set_value, get_show_value,
512 set_list, show_list); break;
d7b32ed3
PM
513
514 case var_string_noescape:
515 add_setshow_string_noescape_cmd (cmd_name, cmdclass,
516 &self->value.stringval,
517 set_doc, show_doc, help_doc,
ecec24e6
PM
518 get_set_value, get_show_value,
519 set_list, show_list);
520
d7b32ed3
PM
521 break;
522
523 case var_optional_filename:
524 add_setshow_optional_filename_cmd (cmd_name, cmdclass,
ecec24e6
PM
525 &self->value.stringval, set_doc,
526 show_doc, help_doc, get_set_value,
527 get_show_value, set_list,
528 show_list);
d7b32ed3
PM
529 break;
530
531 case var_filename:
ecec24e6
PM
532 add_setshow_filename_cmd (cmd_name, cmdclass,
533 &self->value.stringval, set_doc, show_doc,
534 help_doc, get_set_value, get_show_value,
535 set_list, show_list); break;
d7b32ed3
PM
536
537 case var_zinteger:
ecec24e6
PM
538 add_setshow_zinteger_cmd (cmd_name, cmdclass,
539 &self->value.intval, set_doc, show_doc,
540 help_doc, get_set_value, get_show_value,
541 set_list, show_list);
d7b32ed3
PM
542 break;
543
544 case var_enum:
545 add_setshow_enum_cmd (cmd_name, cmdclass, self->enumeration,
ecec24e6
PM
546 &self->value.cstringval, set_doc, show_doc,
547 help_doc, get_set_value, get_show_value,
548 set_list, show_list);
d7b32ed3
PM
549 /* Initialize the value, just in case. */
550 self->value.cstringval = self->enumeration[0];
551 break;
552 }
ecec24e6
PM
553
554 /* Lookup created parameter, and register Python object against the
555 parameter context. Perform this task against both lists. */
556 tmp_name = cmd_name;
557 param = lookup_cmd (&tmp_name, *show_list, "", 0, 1);
558 if (param)
559 set_cmd_context (param, self);
560
561 tmp_name = cmd_name;
562 param = lookup_cmd (&tmp_name, *set_list, "", 0, 1);
563 if (param)
564 set_cmd_context (param, self);
d7b32ed3
PM
565}
566
8dc78533
JK
567/* A helper which computes enum values. Returns 1 on success. Returns 0 on
568 error, with a python exception set. */
d7b32ed3
PM
569static int
570compute_enum_values (parmpy_object *self, PyObject *enum_values)
571{
572 Py_ssize_t size, i;
8dc78533 573 struct cleanup *back_to;
d7b32ed3
PM
574
575 if (! enum_values)
576 {
577 PyErr_SetString (PyExc_RuntimeError,
578 _("An enumeration is required for PARAM_ENUM."));
579 return 0;
580 }
581
582 if (! PySequence_Check (enum_values))
583 {
584 PyErr_SetString (PyExc_RuntimeError,
585 _("The enumeration is not a sequence."));
586 return 0;
587 }
588
589 size = PySequence_Size (enum_values);
590 if (size < 0)
591 return 0;
592 if (size == 0)
593 {
594 PyErr_SetString (PyExc_RuntimeError,
595 _("The enumeration is empty."));
596 return 0;
597 }
598
599 self->enumeration = xmalloc ((size + 1) * sizeof (char *));
8dc78533 600 back_to = make_cleanup (free_current_contents, &self->enumeration);
d7b32ed3
PM
601 memset (self->enumeration, 0, (size + 1) * sizeof (char *));
602
603 for (i = 0; i < size; ++i)
604 {
605 PyObject *item = PySequence_GetItem (enum_values, i);
d59b6f6c 606
d7b32ed3 607 if (! item)
8dc78533
JK
608 {
609 do_cleanups (back_to);
610 return 0;
611 }
d7b32ed3
PM
612 if (! gdbpy_is_string (item))
613 {
fcb49fc8 614 Py_DECREF (item);
8dc78533 615 do_cleanups (back_to);
d7b32ed3
PM
616 PyErr_SetString (PyExc_RuntimeError,
617 _("The enumeration item not a string."));
618 return 0;
619 }
620 self->enumeration[i] = python_string_to_host_string (item);
fcb49fc8 621 Py_DECREF (item);
8dc78533
JK
622 if (self->enumeration[i] == NULL)
623 {
624 do_cleanups (back_to);
625 return 0;
626 }
627 make_cleanup (xfree, (char *) self->enumeration[i]);
d7b32ed3
PM
628 }
629
8dc78533 630 discard_cleanups (back_to);
d7b32ed3
PM
631 return 1;
632}
633
d7b32ed3
PM
634/* Object initializer; sets up gdb-side structures for command.
635
636 Use: __init__(NAME, CMDCLASS, PARMCLASS, [ENUM])
637
638 NAME is the name of the parameter. It may consist of multiple
639 words, in which case the final word is the name of the new command,
640 and earlier words must be prefix commands.
641
642 CMDCLASS is the kind of command. It should be one of the COMMAND_*
643 constants defined in the gdb module.
644
645 PARMCLASS is the type of the parameter. It should be one of the
646 PARAM_* constants defined in the gdb module.
647
648 If PARMCLASS is PARAM_ENUM, then the final argument should be a
649 collection of strings. These strings are the valid values for this
650 parameter.
651
652 The documentation for the parameter is taken from the doc string
653 for the python class.
8dc78533
JK
654
655 Returns -1 on error, with a python exception set. */
656
d7b32ed3
PM
657static int
658parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
659{
660 parmpy_object *obj = (parmpy_object *) self;
ddd49eee 661 const char *name;
d7b32ed3
PM
662 char *set_doc, *show_doc, *doc;
663 char *cmd_name;
664 int parmclass, cmdtype;
665 PyObject *enum_values = NULL;
d7b32ed3
PM
666 struct cmd_list_element **set_list, **show_list;
667 volatile struct gdb_exception except;
668
669 if (! PyArg_ParseTuple (args, "sii|O", &name, &cmdtype, &parmclass,
670 &enum_values))
671 return -1;
672
673 if (cmdtype != no_class && cmdtype != class_run
674 && cmdtype != class_vars && cmdtype != class_stack
675 && cmdtype != class_files && cmdtype != class_support
676 && cmdtype != class_info && cmdtype != class_breakpoint
677 && cmdtype != class_trace && cmdtype != class_obscure
678 && cmdtype != class_maintenance)
679 {
680 PyErr_Format (PyExc_RuntimeError, _("Invalid command class argument."));
681 return -1;
682 }
683
3c0ee1a4
PM
684 if (parmclass != var_boolean /* ARI: var_boolean */
685 && parmclass != var_auto_boolean
d7b32ed3
PM
686 && parmclass != var_uinteger && parmclass != var_integer
687 && parmclass != var_string && parmclass != var_string_noescape
688 && parmclass != var_optional_filename && parmclass != var_filename
689 && parmclass != var_zinteger && parmclass != var_enum)
690 {
9a2b4c1b
MS
691 PyErr_SetString (PyExc_RuntimeError,
692 _("Invalid parameter class argument."));
d7b32ed3
PM
693 return -1;
694 }
695
696 if (enum_values && parmclass != var_enum)
697 {
698 PyErr_SetString (PyExc_RuntimeError,
699 _("Only PARAM_ENUM accepts a fourth argument."));
700 return -1;
701 }
702 if (parmclass == var_enum)
703 {
704 if (! compute_enum_values (obj, enum_values))
705 return -1;
706 }
707 else
708 obj->enumeration = NULL;
709 obj->type = (enum var_types) parmclass;
710 memset (&obj->value, 0, sizeof (obj->value));
711
63d97a20 712 cmd_name = gdbpy_parse_command_name (name, &set_list,
d7b32ed3
PM
713 &setlist);
714
715 if (! cmd_name)
63d97a20 716 return -1;
d7b32ed3 717 xfree (cmd_name);
63d97a20 718 cmd_name = gdbpy_parse_command_name (name, &show_list,
d7b32ed3
PM
719 &showlist);
720 if (! cmd_name)
721 return -1;
722
723 set_doc = get_doc_string (self, set_doc_cst);
724 show_doc = get_doc_string (self, show_doc_cst);
725 doc = get_doc_string (self, gdbpy_doc_cst);
726
727 Py_INCREF (self);
728
729 TRY_CATCH (except, RETURN_MASK_ALL)
730 {
731 add_setshow_generic (parmclass, (enum command_class) cmdtype,
732 cmd_name, obj,
733 set_doc, show_doc,
734 doc, set_list, show_list);
735 }
736 if (except.reason < 0)
737 {
738 xfree (cmd_name);
739 xfree (set_doc);
740 xfree (show_doc);
741 xfree (doc);
742 Py_DECREF (self);
743 PyErr_Format (except.reason == RETURN_QUIT
744 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
745 "%s", except.message);
746 return -1;
747 }
748 return 0;
749}
750
751\f
752
753/* Initialize the 'parameters' module. */
999633ed 754int
d7b32ed3
PM
755gdbpy_initialize_parameters (void)
756{
757 int i;
758
6a1b1664 759 parmpy_object_type.tp_new = PyType_GenericNew;
d7b32ed3 760 if (PyType_Ready (&parmpy_object_type) < 0)
999633ed 761 return -1;
d7b32ed3
PM
762
763 set_doc_cst = PyString_FromString ("set_doc");
764 if (! set_doc_cst)
999633ed 765 return -1;
d7b32ed3
PM
766 show_doc_cst = PyString_FromString ("show_doc");
767 if (! show_doc_cst)
999633ed 768 return -1;
d7b32ed3
PM
769
770 for (i = 0; parm_constants[i].name; ++i)
771 {
772 if (PyModule_AddIntConstant (gdb_module,
773 parm_constants[i].name,
774 parm_constants[i].value) < 0)
999633ed 775 return -1;
d7b32ed3
PM
776 }
777
778 Py_INCREF (&parmpy_object_type);
999633ed
TT
779 return PyModule_AddObject (gdb_module, "Parameter",
780 (PyObject *) &parmpy_object_type);
d7b32ed3
PM
781}
782
783\f
784
785static PyTypeObject parmpy_object_type =
786{
9a27f2c6 787 PyVarObject_HEAD_INIT (NULL, 0)
d7b32ed3
PM
788 "gdb.Parameter", /*tp_name*/
789 sizeof (parmpy_object), /*tp_basicsize*/
790 0, /*tp_itemsize*/
791 0, /*tp_dealloc*/
792 0, /*tp_print*/
793 0, /*tp_getattr*/
794 0, /*tp_setattr*/
795 0, /*tp_compare*/
796 0, /*tp_repr*/
797 0, /*tp_as_number*/
798 0, /*tp_as_sequence*/
799 0, /*tp_as_mapping*/
800 0, /*tp_hash */
801 0, /*tp_call*/
802 0, /*tp_str*/
803 get_attr, /*tp_getattro*/
804 set_attr, /*tp_setattro*/
805 0, /*tp_as_buffer*/
806 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
807 "GDB parameter object", /* tp_doc */
808 0, /* tp_traverse */
809 0, /* tp_clear */
810 0, /* tp_richcompare */
811 0, /* tp_weaklistoffset */
812 0, /* tp_iter */
813 0, /* tp_iternext */
814 0, /* tp_methods */
815 0, /* tp_members */
816 0, /* tp_getset */
817 0, /* tp_base */
818 0, /* tp_dict */
819 0, /* tp_descr_get */
820 0, /* tp_descr_set */
821 0, /* tp_dictoffset */
822 parmpy_init, /* tp_init */
823 0, /* tp_alloc */
d7b32ed3 824};