Use gdbpy_enter in py-cmd.c
[deliverable/binutils-gdb.git] / gdb / python / py-cmd.c
index b11fc3283de11a4223c8e9ff1893160d468bf659..9afa7e4838050690b555adc242fb4080910f53d5 100644 (file)
@@ -1,6 +1,6 @@
 /* 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.
 
@@ -27,6 +27,7 @@
 #include "cli/cli-decode.h"
 #include "completer.h"
 #include "language.h"
+#include "py-ref.h"
 
 /* Struct representing built-in completion types.  */
 struct cmdpy_completer
@@ -95,9 +96,8 @@ static void
 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;
@@ -109,8 +109,6 @@ cmdpy_destroyer (struct cmd_list_element *self, void *context)
   xfree ((char *) self->name);
   xfree ((char *) self->doc);
   xfree ((char *) self->prefixname);
-
-  do_cleanups (cleanup);
 }
 
 /* Called by gdb to invoke the command.  */
@@ -156,7 +154,6 @@ cmdpy_function (struct cmd_list_element *command, char *args, int from_tty)
   if (! result)
     {
       PyObject *ptype, *pvalue, *ptraceback;
-      char *msg;
 
       PyErr_Fetch (&ptype, &pvalue, &ptraceback);
 
@@ -164,8 +161,8 @@ cmdpy_function (struct cmd_list_element *command, char *args, int from_tty)
         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)
        {
@@ -190,7 +187,7 @@ cmdpy_function (struct cmd_list_element *command, char *args, int from_tty)
          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."));
        }
@@ -199,7 +196,7 @@ cmdpy_function (struct cmd_list_element *command, char *args, int from_tty)
          Py_XDECREF (ptype);
          Py_XDECREF (pvalue);
          Py_XDECREF (ptraceback);
-         error ("%s", msg);
+         error ("%s", msg.get ());
        }
     }
 
@@ -287,9 +284,8 @@ cmdpy_completer_handle_brkchars (struct cmd_list_element *command,
                                 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.  */
@@ -324,7 +320,6 @@ cmdpy_completer_handle_brkchars (struct cmd_list_element *command,
  done:
 
   Py_XDECREF (resultobj);
-  do_cleanups (cleanup);
 }
 
 /* Called by gdb for command completion.  */
@@ -335,9 +330,8 @@ cmdpy_completer (struct cmd_list_element *command,
 {
   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.  */
@@ -374,7 +368,6 @@ cmdpy_completer (struct cmd_list_element *command,
 
       while ((elt = PyIter_Next (iter)) != NULL)
        {
-         char *item;
 
          if (! gdbpy_is_string (elt))
            {
@@ -382,7 +375,8 @@ cmdpy_completer (struct cmd_list_element *command,
              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)
            {
@@ -390,7 +384,7 @@ cmdpy_completer (struct cmd_list_element *command,
              PyErr_Clear ();
              continue;
            }
-         VEC_safe_push (char_ptr, result, item);
+         VEC_safe_push (char_ptr, result, item.release ());
        }
 
       Py_DECREF (iter);
@@ -404,7 +398,6 @@ cmdpy_completer (struct cmd_list_element *command,
  done:
 
   Py_XDECREF (resultobj);
-  do_cleanups (cleanup);
 
   return result;
 }
@@ -452,7 +445,7 @@ gdbpy_parse_command_name (const char *name,
                   || 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';
 
@@ -465,7 +458,7 @@ gdbpy_parse_command_name (const char *name,
       return result;
     }
 
-  prefix_text = xmalloc (i + 2);
+  prefix_text = (char *) xmalloc (i + 2);
   memcpy (prefix_text, name, i + 1);
   prefix_text[i + 1] = '\0';
 
@@ -574,7 +567,7 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
          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;
@@ -604,7 +597,7 @@ cmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
 
       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);
@@ -785,13 +778,12 @@ PyTypeObject cmdpy_object_type =
 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;
 
@@ -806,21 +798,18 @@ gdbpy_string_to_argv (PyObject *self, PyObject *args)
 
       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 ();
 }
This page took 0.026685 seconds and 4 git commands to generate.