/* gdb commands implemented in Python
- Copyright (C) 2008-2015 Free Software Foundation, Inc.
+ Copyright (C) 2008-2017 Free Software Foundation, Inc.
This file is part of GDB.
#include "cli/cli-decode.h"
#include "completer.h"
#include "language.h"
+#include "py-ref.h"
/* Struct representing built-in completion types. */
struct cmdpy_completer
cmdpy_destroyer (struct cmd_list_element *self, void *context)
{
cmdpy_object *cmd;
- struct cleanup *cleanup;
- cleanup = ensure_python_env (get_current_arch (), current_language);
+ gdbpy_enter enter_py (get_current_arch (), current_language);
/* Release our hold on the command object. */
cmd = (cmdpy_object *) context;
xfree ((char *) self->name);
xfree ((char *) self->doc);
xfree ((char *) self->prefixname);
-
- do_cleanups (cleanup);
}
/* Called by gdb to invoke the command. */
if (! result)
{
PyObject *ptype, *pvalue, *ptraceback;
- char *msg;
PyErr_Fetch (&ptype, &pvalue, &ptraceback);
When fetching the error message we need to make our own copy,
we no longer own ptype, pvalue after the call to PyErr_Restore. */
- msg = gdbpy_exception_to_string (ptype, pvalue);
- make_cleanup (xfree, msg);
+ gdb::unique_xmalloc_ptr<char>
+ msg (gdbpy_exception_to_string (ptype, pvalue));
if (msg == NULL)
{
PyErr_Restore (ptype, pvalue, ptraceback);
gdbpy_print_stack ();
if (msg != NULL && *msg != '\0')
- error (_("Error occurred in Python command: %s"), msg);
+ error (_("Error occurred in Python command: %s"), msg.get ());
else
error (_("Error occurred in Python command."));
}
Py_XDECREF (ptype);
Py_XDECREF (pvalue);
Py_XDECREF (ptraceback);
- error ("%s", msg);
+ error ("%s", msg.get ());
}
}
const char *text, const char *word)
{
PyObject *resultobj = NULL;
- struct cleanup *cleanup;
- cleanup = ensure_python_env (get_current_arch (), current_language);
+ gdbpy_enter enter_py (get_current_arch (), current_language);
/* Calling our helper to obtain the PyObject of the Python
function. */
done:
Py_XDECREF (resultobj);
- do_cleanups (cleanup);
}
/* Called by gdb for command completion. */
{
PyObject *resultobj = NULL;
VEC (char_ptr) *result = NULL;
- struct cleanup *cleanup;
- cleanup = ensure_python_env (get_current_arch (), current_language);
+ gdbpy_enter enter_py (get_current_arch (), current_language);
/* Calling our helper to obtain the PyObject of the Python
function. */
while ((elt = PyIter_Next (iter)) != NULL)
{
- char *item;
if (! gdbpy_is_string (elt))
{
Py_DECREF (elt);
continue;
}
- item = python_string_to_host_string (elt);
+ gdb::unique_xmalloc_ptr<char>
+ item (python_string_to_host_string (elt));
Py_DECREF (elt);
if (item == NULL)
{
PyErr_Clear ();
continue;
}
- VEC_safe_push (char_ptr, result, item);
+ VEC_safe_push (char_ptr, result, item.release ());
}
Py_DECREF (iter);
done:
Py_XDECREF (resultobj);
- do_cleanups (cleanup);
return result;
}
|| name[i - 1] == '_');
--i)
;
- result = xmalloc (lastchar - i + 2);
+ result = (char *) xmalloc (lastchar - i + 2);
memcpy (result, &name[i], lastchar - i + 1);
result[lastchar - i + 1] = '\0';
return result;
}
- prefix_text = xmalloc (i + 2);
+ prefix_text = (char *) xmalloc (i + 2);
memcpy (prefix_text, name, i + 1);
prefix_text[i + 1] = '\0';
int i, out;
/* Make a normalized form of the command name. */
- pfx_name = xmalloc (strlen (name) + 2);
+ pfx_name = (char *) xmalloc (strlen (name) + 2);
i = 0;
out = 0;
if (ds_obj && gdbpy_is_string (ds_obj))
{
- docstring = python_string_to_host_string (ds_obj);
+ docstring = python_string_to_host_string (ds_obj).release ();
if (docstring == NULL)
{
xfree (cmd_name);
PyObject *
gdbpy_string_to_argv (PyObject *self, PyObject *args)
{
- PyObject *py_argv;
const char *input;
if (!PyArg_ParseTuple (args, "s", &input))
return NULL;
- py_argv = PyList_New (0);
+ gdbpy_ref py_argv (PyList_New (0));
if (py_argv == NULL)
return NULL;
for (i = 0; c_argv[i] != NULL; ++i)
{
- PyObject *argp = PyString_FromString (c_argv[i]);
+ gdbpy_ref argp (PyString_FromString (c_argv[i]));
if (argp == NULL
- || PyList_Append (py_argv, argp) < 0)
+ || PyList_Append (py_argv.get (), argp.get ()) < 0)
{
- Py_XDECREF (argp);
- Py_DECREF (py_argv);
freeargv (c_argv);
return NULL;
}
- Py_DECREF (argp);
}
freeargv (c_argv);
}
- return py_argv;
+ return py_argv.release ();
}