gdb/python: Return None from Progspace.block_for_pc on error
[deliverable/binutils-gdb.git] / gdb / python / py-event.c
index 7d4ff14457668643b10d6fd6db2870df363ceb9c..6816f5314f78eb78004eaa27b83c91952c70db7d 100644 (file)
@@ -1,6 +1,6 @@
 /* Python interface to inferior events.
 
-   Copyright (C) 2009-2012 Free Software Foundation, Inc.
+   Copyright (C) 2009-2019 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+#include "defs.h"
 #include "py-event.h"
 
 void
 evpy_dealloc (PyObject *self)
 {
   Py_XDECREF (((event_object *) self)->dict);
-  self->ob_type->tp_free (self);
+  Py_TYPE (self)->tp_free (self);
 }
 
-PyObject *
+gdbpy_ref<>
 create_event_object (PyTypeObject *py_type)
 {
-  event_object *event_obj;
-
-  event_obj = PyObject_New (event_object, py_type);
-  if (!event_obj)
-    goto fail;
+  gdbpy_ref<event_object> event_obj (PyObject_New (event_object, py_type));
+  if (event_obj == NULL)
+    return NULL;
 
   event_obj->dict = PyDict_New ();
   if (!event_obj->dict)
-    goto fail;
-
-  return (PyObject*) event_obj;
+    return NULL;
 
- fail:
-  Py_XDECREF (event_obj);
-  return NULL;
+  return gdbpy_ref<> ((PyObject *) event_obj.release ());
 }
 
 /* Add the attribute ATTR to the event object EVENT.  In
    python this attribute will be accessible by the name NAME.
-   returns 0 if the operation succeeds and -1 otherwise.  */
+   returns 0 if the operation succeeds and -1 otherwise.  This
+   function acquires a new reference to ATTR.  */
 
 int
-evpy_add_attribute (PyObject *event, char *name, PyObject *attr)
+evpy_add_attribute (PyObject *event, const char *name, PyObject *attr)
 {
   return PyObject_SetAttrString (event, name, attr);
 }
 
 /* Initialize the Python event code.  */
 
-void
+int
 gdbpy_initialize_event (void)
 {
-  gdbpy_initialize_event_generic (&event_object_type,
-                                  "Event");
+  return gdbpy_initialize_event_generic (&event_object_type,
+                                        "Event");
 }
 
 /* Initialize the given event type.  If BASE is not NULL it will
@@ -71,20 +67,12 @@ gdbpy_initialize_event (void)
 
 int
 gdbpy_initialize_event_generic (PyTypeObject *type,
-                                char *name)
+                                const char *name)
 {
   if (PyType_Ready (type) < 0)
-    goto fail;
-
-  Py_INCREF (type);
-  if (PyModule_AddObject (gdb_module, name, (PyObject *) type) < 0)
-    goto fail;
-
-  return 0;
-
-  fail:
-    Py_XDECREF (type);
     return -1;
+
+  return gdb_pymodule_addobject (gdb_module, name, (PyObject *) type);
 }
 
 
@@ -95,25 +83,27 @@ int
 evpy_emit_event (PyObject *event,
                  eventregistry_object *registry)
 {
-  PyObject *callback_list_copy = NULL;
   Py_ssize_t i;
 
   /* Create a copy of call back list and use that for
      notifying listeners to avoid skipping callbacks
      in the case of a callback being disconnected during
      a notification.  */
-  callback_list_copy = PySequence_List (registry->callbacks);
-  if (!callback_list_copy)
-    goto fail;
+  gdbpy_ref<> callback_list_copy (PySequence_List (registry->callbacks));
+  if (callback_list_copy == NULL)
+    return -1;
 
-  for (i = 0; i < PyList_Size (callback_list_copy); i++)
+  for (i = 0; i < PyList_Size (callback_list_copy.get ()); i++)
     {
-      PyObject *func = PyList_GetItem (callback_list_copy, i);
+      PyObject *func = PyList_GetItem (callback_list_copy.get (), i);
 
       if (func == NULL)
-       goto fail;
+       return -1;
 
-      if (!PyObject_CallFunctionObjArgs (func, event, NULL))
+      gdbpy_ref<> func_result (PyObject_CallFunctionObjArgs (func, event,
+                                                            NULL));
+
+      if (func_result == NULL)
        {
          /* Print the trace here, but keep going -- we want to try to
             call all of the callbacks even if one is broken.  */
@@ -121,21 +111,19 @@ evpy_emit_event (PyObject *event,
        }
     }
 
-  Py_XDECREF (callback_list_copy);
-  Py_XDECREF (event);
   return 0;
-
- fail:
-  gdbpy_print_stack ();
-  Py_XDECREF (callback_list_copy);
-  Py_XDECREF (event);
-  return -1;
 }
 
+static gdb_PyGetSetDef event_object_getset[] =
+{
+  { "__dict__", gdb_py_generic_dict, NULL,
+    "The __dict__ for this event.", &event_object_type },
+  { NULL }
+};
+
 PyTypeObject event_object_type =
 {
-  PyObject_HEAD_INIT (NULL)
-  0,                                          /* ob_size */
+  PyVarObject_HEAD_INIT (NULL, 0)
   "gdb.Event",                                /* tp_name */
   sizeof (event_object),                      /* tp_basicsize */
   0,                                          /* tp_itemsize */
@@ -164,7 +152,7 @@ PyTypeObject event_object_type =
   0,                                          /* tp_iternext */
   0,                                          /* tp_methods */
   0,                                          /* tp_members */
-  0,                                          /* tp_getset */
+  event_object_getset,                       /* tp_getset */
   0,                                          /* tp_base */
   0,                                          /* tp_dict */
   0,                                          /* tp_descr_get */
This page took 0.026955 seconds and 4 git commands to generate.