public inbox for archer@sourceware.org
 help / color / mirror / Atom feed
* [RFC][2/5] Event and event registry
@ 2009-08-23 15:35 Oguz Kayral
  2009-09-21 21:55 ` Tom Tromey
  0 siblings, 1 reply; 7+ messages in thread
From: Oguz Kayral @ 2009-08-23 15:35 UTC (permalink / raw)
  To: archer

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 */
};

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [RFC][2/5] Event and event registry
  2009-08-23 15:35 [RFC][2/5] Event and event registry Oguz Kayral
@ 2009-09-21 21:55 ` Tom Tromey
  2009-09-23 23:28   ` Richard Ward
  2010-02-05 11:12   ` Oguz Kayral
  0 siblings, 2 replies; 7+ messages in thread
From: Tom Tromey @ 2009-09-21 21:55 UTC (permalink / raw)
  To: Oguz Kayral; +Cc: archer

>>>>> "Oguz" == Oguz Kayral <oguzkayral@gmail.com> writes:

Oguz> These two new files adds event registries and events to Python
Oguz> scripting.  An event registry contains a list of callbacks to be
Oguz> fired when an event occurs.

Oguz>   Py_INCREF (func);
Oguz>   PyList_Append (callback_list, func);

I always forget whether the list APIs incref themselves, or not.

Oguz> /* Implementation of EventRegistry.disconnect () -> NULL */
Oguz> static PyObject *
Oguz> evregpy_disconnect (PyObject *self, PyObject *function)
[...]
Oguz>   Py_INCREF (func);
Oguz>   PySequence_DelItem (callback_list, PySequence_Index (callback_list, func));

I don't understand the need to incref here.

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

It seems to me that we could just define that an event is a dictionary,
rather than introducing a new type.  Is there a drawback to doing this?

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [RFC][2/5] Event and event registry
  2009-09-21 21:55 ` Tom Tromey
@ 2009-09-23 23:28   ` Richard Ward
  2009-09-25 18:37     ` Tom Tromey
  2010-02-05 11:12   ` Oguz Kayral
  1 sibling, 1 reply; 7+ messages in thread
From: Richard Ward @ 2009-09-23 23:28 UTC (permalink / raw)
  To: archer

Tom Tromey wrote:
>>>>>> "Oguz" == Oguz Kayral <oguzkayral@gmail.com> writes:
> Oguz>   Py_INCREF (func);
> Oguz>   PyList_Append (callback_list, func);
> 
> I always forget whether the list APIs incref themselves, or not.

As a general rule, The list/dict/tupe functions do their own 
incrementing (so the Py_INCREF above is not needed).

The main Exceptions are Py(List|Tuple)_SetItem, which steal a reference. 
The idea being if you create a sequence with Py(List|Tuple)_New(length) 
with length>0, you are probably going to create a bunch of objects to 
put in the list. The stealing of references mean you don't have to call 
Py_DECREF after each Py(List|Tuple)_SetItem (but you must own a `spare' 
reference when you do it).

FWIW there seem to be a fair few places in gdb where Py_INCREF is used 
over-zealously. Sometimes this is just extra increments to python's 
None, True and False values, which I suppose won't cause any leaks, but 
there are a few places that will.

Richard.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [RFC][2/5] Event and event registry
  2009-09-23 23:28   ` Richard Ward
@ 2009-09-25 18:37     ` Tom Tromey
  0 siblings, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2009-09-25 18:37 UTC (permalink / raw)
  To: Richard Ward; +Cc: archer

>>>>> "Richard" == Richard Ward <richard.j.ward1@googlemail.com> writes:

Tom> I always forget whether the list APIs incref themselves, or not.

Richard> As a general rule, The list/dict/tupe functions do their own
Richard> incrementing (so the Py_INCREF above is not needed).

Thanks.

Richard> FWIW there seem to be a fair few places in gdb where Py_INCREF is used
Richard> over-zealously. Sometimes this is just extra increments to python's
Richard> None, True and False values, which I suppose won't cause any leaks,
Richard> but there are a few places that will.

If you know of ones offhand, let me know and I will fix them.

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [RFC][2/5] Event and event registry
  2009-09-21 21:55 ` Tom Tromey
  2009-09-23 23:28   ` Richard Ward
@ 2010-02-05 11:12   ` Oguz Kayral
  2010-02-05 11:31     ` [Archer] " Joel Brobecker
  2010-02-10 22:45     ` Tom Tromey
  1 sibling, 2 replies; 7+ messages in thread
From: Oguz Kayral @ 2010-02-05 11:12 UTC (permalink / raw)
  To: Tom Tromey; +Cc: archer

Hi,

On 21 September 2009 23:55, Tom Tromey <tromey@redhat.com> wrote:
>>>>>> "Oguz" == Oguz Kayral <oguzkayral@gmail.com> writes:
>
> Oguz> An event object contains the details of an occured event (exit code,
> Oguz> breakpoint number, stop reason etc.)
>
> It seems to me that we could just define that an event is a dictionary,
> rather than introducing a new type.  Is there a drawback to doing this?
>
> Tom
>

One thing I tried to achieve was to minimize the use of brackets,
quotation marks etc. Assuming we defined an event as a dictionary, if
a user wants to reach the stop_reason he will have to use
stop_event["stop_reason"]. But in our case he uses
stop_event.stop_reason, which I think is more pythonic.

What do you guys think on this? Maybe we can use a dictionary and find
some way to generate proper getters.,

Oguz

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [Archer] Re: [RFC][2/5] Event and event registry
  2010-02-05 11:12   ` Oguz Kayral
@ 2010-02-05 11:31     ` Joel Brobecker
  2010-02-10 22:45     ` Tom Tromey
  1 sibling, 0 replies; 7+ messages in thread
From: Joel Brobecker @ 2010-02-05 11:31 UTC (permalink / raw)
  To: Oguz Kayral; +Cc: Tom Tromey, archer

> One thing I tried to achieve was to minimize the use of brackets,
> quotation marks etc. Assuming we defined an event as a dictionary, if
> a user wants to reach the stop_reason he will have to use
> stop_event["stop_reason"]. But in our case he uses
> stop_event.stop_reason, which I think is more pythonic.

For some reason, I missing the start of this conversation, so apologies
if I'm a off track in my answer.

FWIW, I agree that using a new type with attributes is more pythonic.
In the end, it's fairly equivalent anyway, but writing stop_event.stop_reason
seems easier than stop_event["stop_reason"].  I assume that the list
of attributes is statically known, so there is no risk of collision like
there was with Values (we discussed the idea of providing access to
struct/union fields via attributes, but there was a risk of collision
with the other attributes of class GDB.Value).

-- 
Joel

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [RFC][2/5] Event and event registry
  2010-02-05 11:12   ` Oguz Kayral
  2010-02-05 11:31     ` [Archer] " Joel Brobecker
@ 2010-02-10 22:45     ` Tom Tromey
  1 sibling, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2010-02-10 22:45 UTC (permalink / raw)
  To: Oguz Kayral; +Cc: archer

>>>>> "Oguz" == Oguz Kayral <oguzkayral@gmail.com> writes:

Tom> It seems to me that we could just define that an event is a dictionary,
Tom> rather than introducing a new type.  Is there a drawback to doing this?

Oguz> One thing I tried to achieve was to minimize the use of brackets,
Oguz> quotation marks etc. Assuming we defined an event as a dictionary, if
Oguz> a user wants to reach the stop_reason he will have to use
Oguz> stop_event["stop_reason"]. But in our case he uses
Oguz> stop_event.stop_reason, which I think is more pythonic.

Ok, I see.  That does make sense.

Oguz> What do you guys think on this? Maybe we can use a dictionary and find
Oguz> some way to generate proper getters.,

It has been a while since I looked at the patches, but basically what I
would like to avoid is having to make a whole new class every time we
want to add an event.

Tom

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2010-02-10 22:45 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-23 15:35 [RFC][2/5] Event and event registry Oguz Kayral
2009-09-21 21:55 ` Tom Tromey
2009-09-23 23:28   ` Richard Ward
2009-09-25 18:37     ` Tom Tromey
2010-02-05 11:12   ` Oguz Kayral
2010-02-05 11:31     ` [Archer] " Joel Brobecker
2010-02-10 22:45     ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).