[pygobject] Consolidate signal connection code
- From: Martin Pitt <martinpitt src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject] Consolidate signal connection code
- Date: Thu, 18 Apr 2013 06:17:13 +0000 (UTC)
commit e91b35d72f93678a79623347dce271148d57046f
Author: Daniel Drake <dsd laptop org>
Date: Mon Mar 18 15:24:52 2013 -0600
Consolidate signal connection code
This code was repeated 4 times with very little variance;
consolidate it and add simple tests to ensure basic coverage.
https://bugzilla.gnome.org/show_bug.cgi?id=696143
gi/_gobject/pygobject.c | 129 +++++++++++++++--------------------------------
tests/test_everything.py | 33 ++++++++++++
2 files changed, 74 insertions(+), 88 deletions(-)
---
diff --git a/gi/_gobject/pygobject.c b/gi/_gobject/pygobject.c
index c92a3f7..a39ef60 100644
--- a/gi/_gobject/pygobject.c
+++ b/gi/_gobject/pygobject.c
@@ -1635,14 +1635,39 @@ pygobject_bind_property(PyGObject *self, PyObject *args)
}
static PyObject *
-pygobject_connect(PyGObject *self, PyObject *args)
+connect_helper(PyGObject *self, gchar *name, PyObject *callback, PyObject *extra_args, PyObject *object,
gboolean after)
{
- PyObject *first, *callback, *extra_args, *repr = NULL;
- gchar *name;
- guint sigid, len;
- gulong handlerid;
+ guint sigid;
GQuark detail = 0;
GClosure *closure;
+ gulong handlerid;
+
+ if (!g_signal_parse_name(name, G_OBJECT_TYPE(self->obj),
+ &sigid, &detail, TRUE)) {
+ PyObject *repr = PyObject_Repr((PyObject*)self);
+ PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s",
+ PYGLIB_PyUnicode_AsString(repr),
+ name);
+ Py_DECREF(repr);
+ return NULL;
+ }
+
+ closure = pygi_signal_closure_new(self, name, callback, extra_args, object);
+ if (closure == NULL)
+ closure = pyg_closure_new(callback, extra_args, object);
+
+ pygobject_watch_closure((PyObject *)self, closure);
+ handlerid = g_signal_connect_closure_by_id(self->obj, sigid, detail,
+ closure, after);
+ return PyLong_FromUnsignedLong(handlerid);
+}
+
+static PyObject *
+pygobject_connect(PyGObject *self, PyObject *args)
+{
+ PyObject *first, *callback, *extra_args, *ret;
+ gchar *name;
+ guint len;
len = PyTuple_Size(args);
if (len < 2) {
@@ -1663,40 +1688,21 @@ pygobject_connect(PyGObject *self, PyObject *args)
CHECK_GOBJECT(self);
- if (!g_signal_parse_name(name, G_OBJECT_TYPE(self->obj),
- &sigid, &detail, TRUE)) {
- repr = PyObject_Repr((PyObject*)self);
- PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s",
- PYGLIB_PyUnicode_AsString(repr),
- name);
- Py_DECREF(repr);
- return NULL;
- }
extra_args = PySequence_GetSlice(args, 2, len);
if (extra_args == NULL)
return NULL;
- closure = pygi_signal_closure_new(self, name, callback, extra_args, NULL);
- if (closure == NULL)
- closure = pyg_closure_new(callback, extra_args, NULL);
-
- pygobject_watch_closure((PyObject *)self, closure);
- handlerid = g_signal_connect_closure_by_id(self->obj, sigid, detail,
- closure, FALSE);
+ ret = connect_helper(self, name, callback, extra_args, NULL, FALSE);
Py_DECREF(extra_args);
- return PyLong_FromUnsignedLong(handlerid);
+ return ret;
}
static PyObject *
pygobject_connect_after(PyGObject *self, PyObject *args)
{
- PyObject *first, *callback, *extra_args, *repr = NULL;
+ PyObject *first, *callback, *extra_args, *ret;
gchar *name;
- guint sigid;
- gulong handlerid;
Py_ssize_t len;
- GQuark detail;
- GClosure *closure;
len = PyTuple_Size(args);
if (len < 2) {
@@ -1718,40 +1724,21 @@ pygobject_connect_after(PyGObject *self, PyObject *args)
CHECK_GOBJECT(self);
- if (!g_signal_parse_name(name, G_OBJECT_TYPE(self->obj),
- &sigid, &detail, TRUE)) {
- repr = PyObject_Repr((PyObject*)self);
- PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s",
- PYGLIB_PyUnicode_AsString(repr),
- name);
- Py_DECREF(repr);
- return NULL;
- }
extra_args = PySequence_GetSlice(args, 2, len);
if (extra_args == NULL)
return NULL;
- closure = pygi_signal_closure_new(self, name, callback, extra_args, NULL);
- if (closure == NULL)
- closure = pyg_closure_new(callback, extra_args, NULL);
-
- pygobject_watch_closure((PyObject *)self, closure);
- handlerid = g_signal_connect_closure_by_id(self->obj, sigid, detail,
- closure, TRUE);
+ ret = connect_helper(self, name, callback, extra_args, NULL, TRUE);
Py_DECREF(extra_args);
- return PyLong_FromUnsignedLong(handlerid);
+ return ret;
}
static PyObject *
pygobject_connect_object(PyGObject *self, PyObject *args)
{
- PyObject *first, *callback, *extra_args, *object, *repr = NULL;
+ PyObject *first, *callback, *extra_args, *object, *ret;
gchar *name;
- guint sigid;
- gulong handlerid;
Py_ssize_t len;
- GQuark detail;
- GClosure *closure;
len = PyTuple_Size(args);
if (len < 3) {
@@ -1773,40 +1760,21 @@ pygobject_connect_object(PyGObject *self, PyObject *args)
CHECK_GOBJECT(self);
- if (!g_signal_parse_name(name, G_OBJECT_TYPE(self->obj),
- &sigid, &detail, TRUE)) {
- repr = PyObject_Repr((PyObject*)self);
- PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s",
- PYGLIB_PyUnicode_AsString(repr),
- name);
- Py_DECREF(repr);
- return NULL;
- }
extra_args = PySequence_GetSlice(args, 3, len);
if (extra_args == NULL)
return NULL;
- closure = pygi_signal_closure_new(self, name, callback, extra_args, object);
- if (closure == NULL)
- closure = pyg_closure_new(callback, extra_args, object);
-
- pygobject_watch_closure((PyObject *)self, closure);
- handlerid = g_signal_connect_closure_by_id(self->obj, sigid, detail,
- closure, FALSE);
+ ret = connect_helper(self, name, callback, extra_args, object, FALSE);
Py_DECREF(extra_args);
- return PyLong_FromUnsignedLong(handlerid);
+ return ret;
}
static PyObject *
pygobject_connect_object_after(PyGObject *self, PyObject *args)
{
- PyObject *first, *callback, *extra_args, *object, *repr = NULL;
+ PyObject *first, *callback, *extra_args, *object, *ret;
gchar *name;
- guint sigid;
- gulong handlerid;
Py_ssize_t len;
- GQuark detail;
- GClosure *closure;
len = PyTuple_Size(args);
if (len < 3) {
@@ -1828,28 +1796,13 @@ pygobject_connect_object_after(PyGObject *self, PyObject *args)
CHECK_GOBJECT(self);
- if (!g_signal_parse_name(name, G_OBJECT_TYPE(self->obj),
- &sigid, &detail, TRUE)) {
- repr = PyObject_Repr((PyObject*)self);
- PyErr_Format(PyExc_TypeError, "%s: unknown signal name: %s",
- PYGLIB_PyUnicode_AsString(repr),
- name);
- Py_DECREF(repr);
- return NULL;
- }
extra_args = PySequence_GetSlice(args, 3, len);
if (extra_args == NULL)
return NULL;
- closure = pygi_signal_closure_new(self, name, callback, extra_args, object);
- if (closure == NULL)
- closure = pyg_closure_new(callback, extra_args, object);
-
- pygobject_watch_closure((PyObject *)self, closure);
- handlerid = g_signal_connect_closure_by_id(self->obj, sigid, detail,
- closure, TRUE);
+ ret = connect_helper(self, name, callback, extra_args, object, TRUE);
Py_DECREF(extra_args);
- return PyLong_FromUnsignedLong(handlerid);
+ return ret;
}
static PyObject *
diff --git a/tests/test_everything.py b/tests/test_everything.py
index 3c820d7..93fb3ab 100644
--- a/tests/test_everything.py
+++ b/tests/test_everything.py
@@ -1139,6 +1139,39 @@ class TestSignals(unittest.TestCase):
obj.emit_sig_with_obj()
self.assertTrue(obj.called)
+ def test_connect_after(self):
+ obj = Everything.TestObj()
+
+ def callback(obj, obj_param):
+ obj.called = True
+
+ obj.called = False
+ obj.connect_after('sig-with-obj', callback)
+ obj.emit_sig_with_obj()
+ self.assertTrue(obj.called)
+
+ def test_connect_object(self):
+ obj = Everything.TestObj()
+
+ def callback(obj, obj_param):
+ obj.called = True
+
+ obj.called = False
+ obj.connect_object('sig-with-obj', callback, obj)
+ obj.emit_sig_with_obj()
+ self.assertTrue(obj.called)
+
+ def test_connect_object_after(self):
+ obj = Everything.TestObj()
+
+ def callback(obj, obj_param):
+ obj.called = True
+
+ obj.called = False
+ obj.connect_object_after('sig-with-obj', callback, obj)
+ obj.emit_sig_with_obj()
+ self.assertTrue(obj.called)
+
def test_int64_param_from_py(self):
obj = Everything.TestObj()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]