After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 693994 - GTK overrides: Make connect_signals handle tuple
GTK overrides: Make connect_signals handle tuple
Status: RESOLVED FIXED
Product: pygobject
Classification: Bindings
Component: general
unspecified
Other All
: Normal enhancement
: GNOME 3.10
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
Depends on:
Blocks:
 
 
Reported: 2013-02-16 22:32 UTC by Cole Robinson
Modified: 2013-07-03 11:49 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
GTK overrides: Make connect_signals handle tuple (2.24 KB, patch)
2013-02-16 22:32 UTC, Cole Robinson
reviewed Details | Review
GTK overrides: Make connect_signals handle tuple (v2) (2.47 KB, patch)
2013-03-25 19:27 UTC, Cole Robinson
needs-work Details | Review
GTK overrides: Make connect_signals handle tuple (11.04 KB, patch)
2013-07-03 11:49 UTC, Simon Feltman
committed Details | Review

Description Cole Robinson 2013-02-16 22:32:37 UTC
pygtk2 allowed passing a tuple to connect_signals, like

(handler, arg1, arg2, ...)

Though pygobject doesn't allow it.
Comment 1 Cole Robinson 2013-02-16 22:32:38 UTC
Created attachment 236418 [details] [review]
GTK overrides: Make connect_signals handle tuple

This is used for passing passing extra arguments to a callback.
pygtk2 allowed this so we should at least do it for back compat.
Comment 2 Cole Robinson 2013-03-19 15:47:46 UTC
ping
Comment 3 Simon Feltman 2013-03-21 03:33:12 UTC
Review of attachment 236418 [details] [review]:

Since gtk_builder_connect_signals is so specific to C programming (and not useful for Python) it makes sense to override this to work well for Python programmers. A couple things:

* docs: It would be nice to have a pydoc describing the addition (or even the whole function).
* unittests: It would be great to have tests for different variations of the added functionality. As a starting point, there are existing builder tests: pygobject/tests/test_overrides_gtk.py : TestGtk.test_builder. I recommend breaking down "_full_callback" to make it more testable. i.e. A new utility function which extracts the handler and args given the obj_or_map and handler_name args. This will make testing easier because you won't have to mess with actually connecting signals and can test the behaviour of how the obj_or_map arg is supposed to work in isolation. For example:

handler, args = _extract_handler_and_args(obj_or_map, handler_name)

Basically the entirety of what _full_callback does with obj_or_map would go in here (including the error conditions which should also be tested). Note: to make this accessible to the unittest you might need to do something like add it as a static method to the Builder class.

A workaround (if you don't already do something like this):

def _builder_callback(builder, gobj, signal_name, handler_name, connect_obj, flags, signal_map):
    handler, *args = signal_map[handler_name]  # Python3 only
    gobj.connect(signal_name, handler, *args)

builder.connect_signals_full(_builder_callback, signal_map)

::: gi/overrides/Gtk.py
@@ +360,3 @@
 
+            args = ()
+            if type(handler) is tuple:

Use isinstance(handler, collections.Sequence) here
Comment 4 Simon Feltman 2013-03-21 03:35:13 UTC
Note: We are currently in a hard code freeze for 3.8 so I'm adding this to the 3.10 milestone.
Comment 5 Cole Robinson 2013-03-25 19:26:06 UTC
Hi Simon, thanks for the review.

(In reply to comment #3)
> Review of attachment 236418 [details] [review]:
> 
> Since gtk_builder_connect_signals is so specific to C programming (and not
> useful for Python) it makes sense to override this to work well for Python
> programmers. A couple things:
> 
> * docs: It would be nice to have a pydoc describing the addition (or even the
> whole function).
> * unittests: It would be great to have tests for different variations of the
> added functionality. As a starting point, there are existing builder tests:
> pygobject/tests/test_overrides_gtk.py : TestGtk.test_builder. I recommend
> breaking down "_full_callback" to make it more testable. i.e. A new utility
> function which extracts the handler and args given the obj_or_map and
> handler_name args. This will make testing easier because you won't have to mess
> with actually connecting signals and can test the behaviour of how the
> obj_or_map arg is supposed to work in isolation. For example:
> 
> handler, args = _extract_handler_and_args(obj_or_map, handler_name)
> 
> Basically the entirety of what _full_callback does with obj_or_map would go in
> here (including the error conditions which should also be tested). Note: to
> make this accessible to the unittest you might need to do something like add it
> as a static method to the Builder class.
> 

Thanks for the detailed info. Unfortunately doing the above will likely exceed the time I can allocate to this right now.

I'll post a v2 with the collections.Sequence bit you mention. If anyone wants to run with the docs + tests bit, please do, otherwise I'll try and get to it when I have more time.

> A workaround (if you don't already do something like this):
> 
> def _builder_callback(builder, gobj, signal_name, handler_name, connect_obj,
> flags, signal_map):
>     handler, *args = signal_map[handler_name]  # Python3 only
>     gobj.connect(signal_name, handler, *args)
> 
> builder.connect_signals_full(_builder_callback, signal_map)
> 

Thanks for the hint. I already reworked my code to not need this, but that's nice in the general case.

> ::: gi/overrides/Gtk.py
> @@ +360,3 @@
> 
> +            args = ()
> +            if type(handler) is tuple:
> 
> Use isinstance(handler, collections.Sequence) here

Thanks, will do.

(In reply to comment #4)
> Note: We are currently in a hard code freeze for 3.8 so I'm adding this to the
> 3.10 milestone.

No prob.
Comment 6 Cole Robinson 2013-03-25 19:27:50 UTC
Created attachment 239817 [details] [review]
GTK overrides: Make connect_signals handle tuple (v2)

This is used for passing passing extra arguments to a callback.
pygtk2 allowed this so we should at least do it for back compat.

v2: Check against collections.Sequence, not raw tuple
Comment 7 Simon Feltman 2013-07-03 11:47:22 UTC
Pushed with additional refactoring, tests and docs described in comment #3

The following fixes have been pushed:
Comment 8 Simon Feltman 2013-07-03 11:49:16 UTC
Created attachment 248303 [details] [review]
GTK overrides: Make connect_signals handle tuple

This is used for passing extra arguments to callbacks during
signal emission in the form of:
builder.connect_signals({'on_clicked': (on_clicked, arg1, arg2)})

Co-Authored-By: Simon Feltman <sfeltman@src.gnome.org>