/* GDB parameters implemented in Python
- Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
+ Copyright (C) 2008-2016 Free Software Foundation, Inc.
This file is part of GDB.
#include "defs.h"
#include "value.h"
-#include "exceptions.h"
#include "python-internal.h"
#include "charset.h"
#include "gdbcmd.h"
typedef struct parmpy_object parmpy_object;
-static PyTypeObject parmpy_object_type;
+extern PyTypeObject parmpy_object_type
+ CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("parmpy_object");
/* Some handy string constants. */
static PyObject *set_doc_cst;
get_attr (PyObject *obj, PyObject *attr_name)
{
if (PyString_Check (attr_name)
+#ifdef IS_PY3K
+ && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
+#else
&& ! strcmp (PyString_AsString (attr_name), "value"))
+#endif
{
parmpy_object *self = (parmpy_object *) obj;
&& (self->type == var_filename
|| value != Py_None))
{
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("String required for filename."));
return -1;
}
else
{
- char *string;
-
- string = python_string_to_host_string (value);
+ gdb::unique_xmalloc_ptr<char>
+ string (python_string_to_host_string (value));
if (string == NULL)
return -1;
xfree (self->value.stringval);
- self->value.stringval = string;
+ self->value.stringval = string.release ();
}
break;
case var_enum:
{
int i;
- char *str;
if (! gdbpy_is_string (value))
{
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("ENUM arguments must be a string."));
return -1;
}
- str = python_string_to_host_string (value);
+ gdb::unique_xmalloc_ptr<char>
+ str (python_string_to_host_string (value));
if (str == NULL)
return -1;
for (i = 0; self->enumeration[i]; ++i)
- if (! strcmp (self->enumeration[i], str))
+ if (! strcmp (self->enumeration[i], str.get ()))
break;
- xfree (str);
if (! self->enumeration[i])
{
PyErr_SetString (PyExc_RuntimeError,
case var_boolean:
if (! PyBool_Check (value))
{
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("A boolean argument is required."));
return -1;
}
cmp = PyObject_IsTrue (value);
- if (cmp < 0)
+ if (cmp < 0)
return -1;
self->value.intval = cmp;
break;
{
cmp = PyObject_IsTrue (value);
if (cmp < 0 )
- return -1;
+ return -1;
if (cmp == 1)
self->value.autoboolval = AUTO_BOOLEAN_TRUE;
- else
+ else
self->value.autoboolval = AUTO_BOOLEAN_FALSE;
}
break;
if (! PyInt_Check (value))
{
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("The value must be integer."));
return -1;
}
if (! ok)
{
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("Range exceeded."));
return -1;
}
}
default:
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("Unhandled type in parameter value."));
return -1;
}
set_attr (PyObject *obj, PyObject *attr_name, PyObject *val)
{
if (PyString_Check (attr_name)
+#ifdef IS_PY3K
+ && ! PyUnicode_CompareWithASCIIString (attr_name, "value"))
+#else
&& ! strcmp (PyString_AsString (attr_name), "value"))
+#endif
{
if (!val)
{
/* A helper function which returns a documentation string for an
object. */
-static char *
+static gdb::unique_xmalloc_ptr<char>
get_doc_string (PyObject *object, PyObject *attr)
{
- char *result = NULL;
+ gdb::unique_xmalloc_ptr<char> result;
if (PyObject_HasAttr (object, attr))
{
Py_XDECREF (ds_obj);
}
if (! result)
- result = xstrdup (_("This command is not documented."));
+ result.reset (xstrdup (_("This command is not documented.")));
return result;
}
argument ARG. ARG can be NULL. METHOD should return a Python
string. If this function returns NULL, there has been an error and
the appropriate exception set. */
-static char *
+static gdb::unique_xmalloc_ptr<char>
call_doc_function (PyObject *obj, PyObject *method, PyObject *arg)
{
- char *data = NULL;
+ gdb::unique_xmalloc_ptr<char> data;
PyObject *result = PyObject_CallMethodObjArgs (obj, method, arg, NULL);
if (! result)
if (gdbpy_is_string (result))
{
data = python_string_to_host_string (result);
+ Py_DECREF (result);
if (! data)
return NULL;
}
{
PyErr_SetString (PyExc_RuntimeError,
_("Parameter must return a string value."));
+ Py_DECREF (result);
return NULL;
}
struct cmd_list_element *c)
{
PyObject *obj = (PyObject *) get_cmd_context (c);
- char *set_doc_string;
+ gdb::unique_xmalloc_ptr<char> set_doc_string;
struct cleanup *cleanup = ensure_python_env (get_current_arch (),
current_language);
PyObject *set_doc_func = PyString_FromString ("get_set_string");
if (! set_doc_func)
goto error;
- make_cleanup_py_decref (set_doc_func);
-
if (PyObject_HasAttr (obj, set_doc_func))
{
set_doc_string = call_doc_function (obj, set_doc_func, NULL);
set_doc_string = get_doc_string (obj, set_doc_cst);
}
- make_cleanup (xfree, set_doc_string);
- fprintf_filtered (gdb_stdout, "%s\n", set_doc_string);
+ fprintf_filtered (gdb_stdout, "%s\n", set_doc_string.get ());
+ Py_XDECREF (set_doc_func);
do_cleanups (cleanup);
return;
error:
+ Py_XDECREF (set_doc_func);
gdbpy_print_stack ();
do_cleanups (cleanup);
return;
const char *value)
{
PyObject *obj = (PyObject *) get_cmd_context (c);
- char *show_doc_string = NULL;
+ gdb::unique_xmalloc_ptr<char> show_doc_string;
struct cleanup *cleanup = ensure_python_env (get_current_arch (),
current_language);
PyObject *show_doc_func = PyString_FromString ("get_show_string");
if (! show_doc_func)
goto error;
- make_cleanup_py_decref (show_doc_func);
-
if (PyObject_HasAttr (obj, show_doc_func))
{
PyObject *val_obj = PyString_FromString (value);
if (! val_obj)
goto error;
- make_cleanup_py_decref (val_obj);
-
show_doc_string = call_doc_function (obj, show_doc_func, val_obj);
+ Py_DECREF (val_obj);
if (! show_doc_string)
goto error;
- make_cleanup (xfree, show_doc_string);
-
- fprintf_filtered (file, "%s\n", show_doc_string);
+ fprintf_filtered (file, "%s\n", show_doc_string.get ());
}
else
{
callback function does not exist, then attempt to read the
show_doc attribute. */
show_doc_string = get_doc_string (obj, show_doc_cst);
- make_cleanup (xfree, show_doc_string);
- fprintf_filtered (file, "%s %s\n", show_doc_string, value);
+ fprintf_filtered (file, "%s %s\n", show_doc_string.get (), value);
}
+ Py_XDECREF (show_doc_func);
do_cleanups (cleanup);
return;
error:
+ Py_XDECREF (show_doc_func);
gdbpy_print_stack ();
do_cleanups (cleanup);
return;
struct cmd_list_element **show_list)
{
struct cmd_list_element *param = NULL;
- char *tmp_name = NULL;
+ const char *tmp_name = NULL;
switch (parmclass)
{
if (! PySequence_Check (enum_values))
{
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("The enumeration is not a sequence."));
return 0;
}
return 0;
if (size == 0)
{
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("The enumeration is empty."));
return 0;
}
- self->enumeration = xmalloc ((size + 1) * sizeof (char *));
+ self->enumeration = XCNEWVEC (const char *, size + 1);
back_to = make_cleanup (free_current_contents, &self->enumeration);
- memset (self->enumeration, 0, (size + 1) * sizeof (char *));
for (i = 0; i < size; ++i)
{
}
if (! gdbpy_is_string (item))
{
+ Py_DECREF (item);
do_cleanups (back_to);
- PyErr_SetString (PyExc_RuntimeError,
+ PyErr_SetString (PyExc_RuntimeError,
_("The enumeration item not a string."));
return 0;
}
- self->enumeration[i] = python_string_to_host_string (item);
+ self->enumeration[i] = python_string_to_host_string (item).release ();
+ Py_DECREF (item);
if (self->enumeration[i] == NULL)
{
do_cleanups (back_to);
parmpy_init (PyObject *self, PyObject *args, PyObject *kwds)
{
parmpy_object *obj = (parmpy_object *) self;
- char *name;
+ const char *name;
char *set_doc, *show_doc, *doc;
char *cmd_name;
int parmclass, cmdtype;
PyObject *enum_values = NULL;
struct cmd_list_element **set_list, **show_list;
- volatile struct gdb_exception except;
if (! PyArg_ParseTuple (args, "sii|O", &name, &cmdtype, &parmclass,
&enum_values))
if (! cmd_name)
return -1;
- set_doc = get_doc_string (self, set_doc_cst);
- show_doc = get_doc_string (self, show_doc_cst);
- doc = get_doc_string (self, gdbpy_doc_cst);
+ set_doc = get_doc_string (self, set_doc_cst).release ();
+ show_doc = get_doc_string (self, show_doc_cst).release ();
+ doc = get_doc_string (self, gdbpy_doc_cst).release ();
Py_INCREF (self);
- TRY_CATCH (except, RETURN_MASK_ALL)
+ TRY
{
add_setshow_generic (parmclass, (enum command_class) cmdtype,
cmd_name, obj,
set_doc, show_doc,
doc, set_list, show_list);
}
- if (except.reason < 0)
+ CATCH (except, RETURN_MASK_ALL)
{
xfree (cmd_name);
xfree (set_doc);
"%s", except.message);
return -1;
}
+ END_CATCH
+
return 0;
}
\f
/* Initialize the 'parameters' module. */
-void
+int
gdbpy_initialize_parameters (void)
{
int i;
+ parmpy_object_type.tp_new = PyType_GenericNew;
if (PyType_Ready (&parmpy_object_type) < 0)
- return;
+ return -1;
set_doc_cst = PyString_FromString ("set_doc");
if (! set_doc_cst)
- return;
+ return -1;
show_doc_cst = PyString_FromString ("show_doc");
if (! show_doc_cst)
- return;
+ return -1;
for (i = 0; parm_constants[i].name; ++i)
{
if (PyModule_AddIntConstant (gdb_module,
parm_constants[i].name,
parm_constants[i].value) < 0)
- return;
+ return -1;
}
- Py_INCREF (&parmpy_object_type);
- PyModule_AddObject (gdb_module, "Parameter",
- (PyObject *) &parmpy_object_type);
+ return gdb_pymodule_addobject (gdb_module, "Parameter",
+ (PyObject *) &parmpy_object_type);
}
\f
-static PyTypeObject parmpy_object_type =
+PyTypeObject parmpy_object_type =
{
- PyObject_HEAD_INIT (NULL)
- 0, /*ob_size*/
+ PyVarObject_HEAD_INIT (NULL, 0)
"gdb.Parameter", /*tp_name*/
sizeof (parmpy_object), /*tp_basicsize*/
0, /*tp_itemsize*/
0, /* tp_dictoffset */
parmpy_init, /* tp_init */
0, /* tp_alloc */
- PyType_GenericNew /* tp_new */
};