This is the mail archive of the archer@sourceware.org mailing list for the Archer project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFC][2/5] Event and event registry


These two new files adds event registries and events to Python scripting.

An event registry contains a list of callbacks to be fired when an event occurs.

python-eventregistry.c:

evregpy_connect(): New function for adding a callable python object to
the callback list of an event.
evregpy_disconnect(): New function for deleting a callable python
object from the callback list of an event.

/* Python interface to inferior thread event registries.

   Copyright (C) 2009 Free Software Foundation, Inc.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   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 "command.h"
#include "python-internal.h"

static PyTypeObject eventregistry_object_type;

/* Implementation of EventRegistry.connect () -> NULL */
static PyObject *
evregpy_connect (PyObject *self, PyObject *function)
{
  PyObject *func;
  PyObject *callback_list = (PyObject *) (((eventregistry_object *)
self)->callbacks);

  if (!PyArg_ParseTuple (function, "O", &func))
    return NULL;

  if (!PyCallable_Check (func))
    {
      PyErr_SetString (PyExc_RuntimeError, "Function is not callable");
      return NULL;
    }

  Py_INCREF (func);

  PyList_Append (callback_list, func);

  Py_RETURN_NONE;
}

/* Implementation of EventRegistry.disconnect () -> NULL */
static PyObject *
evregpy_disconnect (PyObject *self, PyObject *function)
{
  PyObject *func;
  PyObject *callback_list = (PyObject *) (((eventregistry_object *)
self)->callbacks);

  if (!PyArg_ParseTuple (function, "O", &func))
    return NULL;

  if (!PyCallable_Check (func))
    {
      PyErr_SetString (PyExc_RuntimeError, "Function is not callable");
      return NULL;
    }

  Py_INCREF (func);

  PySequence_DelItem (callback_list, PySequence_Index (callback_list, func));

  Py_RETURN_NONE;
}

eventregistry_object *
create_eventregistry_object (void)
{
  eventregistry_object *eventregistry_obj;

  eventregistry_obj = PyObject_New (eventregistry_object,
&eventregistry_object_type);
  if (!eventregistry_obj)
    return NULL;

  eventregistry_obj->callbacks = (PyListObject *) PyList_New (0);

  return eventregistry_obj;
}

static void
evregpy_dealloc (PyObject *self)
{
  Py_DECREF (((eventregistry_object *) self)->callbacks);
  self->ob_type->tp_free (self);
}

/* Initialize the Python event registry code. */
void
gdbpy_initialize_eventregistry (void)
{
  if (PyType_Ready (&eventregistry_object_type) < 0)
    return;

  Py_INCREF (&eventregistry_object_type);
  PyModule_AddObject (gdb_module, "EventRegistry", (PyObject *)
&eventregistry_object_type);
}

static PyMethodDef eventregistry_object_methods[] =
{
  { "connect", evregpy_connect, METH_VARARGS, "Add function" },
  { "disconnect", evregpy_disconnect, METH_VARARGS, "Remove function" },
  { NULL } /* Sentinel. */
};

static PyTypeObject eventregistry_object_type =
{
  PyObject_HEAD_INIT (NULL)
  0,                                          /* ob_size */
  "gdb.EventRegistry",                        /* tp_name */
  sizeof (eventregistry_object),              /* tp_basicsize */
  0,                                          /* tp_itemsize */
  evregpy_dealloc,                            /* tp_dealloc */
  0,                                          /* tp_print */
  0,                                          /* tp_getattr */
  0,                                          /* tp_setattr */
  0,                                          /* tp_compare */
  0,                                          /* tp_repr */
  0,                                          /* tp_as_number */
  0,                                          /* tp_as_sequence */
  0,                                          /* tp_as_mapping */
  0,                                          /* tp_hash  */
  0,                                          /* tp_call */
  0,                                          /* tp_str */
  0,                                          /* tp_getattro */
  0,                                          /* tp_setattro */
  0,                                          /* tp_as_buffer */
  Py_TPFLAGS_DEFAULT,                         /* tp_flags */
  "GDB event registry object",                /* tp_doc */
  0,                                          /* tp_traverse */
  0,                                          /* tp_clear */
  0,                                          /* tp_richcompare */
  0,                                          /* tp_weaklistoffset */
  0,                                          /* tp_iter */
  0,                                          /* tp_iternext */
  eventregistry_object_methods,               /* tp_methods */
  0,                                          /* tp_members */
  0,                                          /* tp_getset */
  0,                                          /* tp_base */
  0,                                          /* tp_dict */
  0,                                          /* tp_descr_get */
  0,                                          /* tp_descr_set */
  0,                                          /* tp_dictoffset */
  0,                                          /* tp_init */
  0                                           /* tp_alloc */
};

--------

An event object contains the details of an occured event (exit code,
breakpoint number, stop reason etc.)

/* Python interface to inferior events.

   Copyright (C) 2009 Free Software Foundation, Inc.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   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 "command.h"
#include "python-internal.h"
#include "inferior.h"

event_object *
create_event_object (const char *event_type)
{
  event_object *event_obj;

  event_obj = PyObject_New (event_object, &event_object_type);
  if (!event_obj)
    return NULL;

  event_obj->inferior_thread = find_thread_object (inferior_ptid);
  Py_INCREF (event_obj->inferior_thread);
  event_obj->event_type = (PyStringObject *) PyString_FromString (event_type);

  return event_obj;
}

static void
evpy_dealloc (PyObject *self)
{
  Py_DECREF (((event_object *) self)->inferior_thread);
  self->ob_type->tp_free (self);
}

/* Python function to get the event's thread. */
static PyObject *
evpy_get_inferior_thread (PyObject *self, void *closure)
{
  event_object *event_obj = (event_object *) self;

  Py_INCREF (event_obj->inferior_thread);

  return (PyObject *) (event_obj->inferior_thread);
}

/* Python function to get the event's type. */
static PyObject *
evpy_get_event_type (PyObject *self, void *closure)
{
  event_object *event_obj = (event_object *) self;

  Py_INCREF (event_obj->event_type);

  return (PyObject *) (event_obj->event_type);
}

/* Initialize the Python event code. */
void
gdbpy_initialize_event (void)
{
  if (PyType_Ready (&event_object_type) < 0)
    return;

  Py_INCREF (&event_object_type);
  PyModule_AddObject (gdb_module, "Event", (PyObject *) &event_object_type);
}

static PyGetSetDef event_object_getset[] =
{
  { "inferior_thread", evpy_get_inferior_thread, NULL, "Inferior
thread.", NULL },
  { "event_type", evpy_get_event_type, NULL, "Event type.", NULL },

  { NULL } /* Sentinel. */
};

PyTypeObject event_object_type =
{
  PyObject_HEAD_INIT (NULL)
  0,                                          /* ob_size */
  "gdb.Event",                                /* tp_name */
  sizeof (event_object),                      /* tp_basicsize */
  0,                                          /* tp_itemsize */
  evpy_dealloc,                               /* tp_dealloc */
  0,                                          /* tp_print */
  0,                                          /* tp_getattr */
  0,                                          /* tp_setattr */
  0,                                          /* tp_compare */
  0,                                          /* tp_repr */
  0,                                          /* tp_as_number */
  0,                                          /* tp_as_sequence */
  0,                                          /* tp_as_mapping */
  0,                                          /* tp_hash  */
  0,                                          /* tp_call */
  0,                                          /* tp_str */
  0,                                          /* tp_getattro */
  0,                                          /* tp_setattro */
  0,                                          /* tp_as_buffer */
  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,   /* tp_flags */
  "GDB event object",                         /* tp_doc */
  0,                                          /* tp_traverse */
  0,                                          /* tp_clear */
  0,                                          /* tp_richcompare */
  0,                                          /* tp_weaklistoffset */
  0,                                          /* tp_iter */
  0,                                          /* tp_iternext */
  0,                                          /* tp_methods */
  0,                                          /* tp_members */
  event_object_getset,                        /* tp_getset */
  0,                                          /* tp_base */
  0,                                          /* tp_dict */
  0,                                          /* tp_descr_get */
  0,                                          /* tp_descr_set */
  0,                                          /* tp_dictoffset */
  0,                                          /* tp_init */
  0                                           /* tp_alloc */
};


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]