Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-31485: Fix Misc.unbind behaviour when funcid is given #17954

Open
wants to merge 3 commits into
base: master
from

Conversation

@GiovaLomba
Copy link

GiovaLomba commented Jan 11, 2020

GiovaLomba added 2 commits Jan 6, 2020
Allows Misc.unbind to delete only the bound callback
having the given funcid. See the issue above for more.
bpo-31485: Fix Misc.unbind behaviour when funcid is given
@the-knights-who-say-ni

This comment has been minimized.

Copy link

the-knights-who-say-ni commented Jan 11, 2020

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

Recognized GitHub username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@GiovaLomba

This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@vstinner

This comment has been minimized.

Copy link
Member

vstinner commented Jan 14, 2020

@GiovaLomba

This comment has been minimized.

Copy link
Author

GiovaLomba commented Jan 14, 2020

About the ubuntu test failure: it happens at cpython/Lib/idlelib/multicall.py, line 241 in __del__. Having a look at the code it appears that, for reasons unknown to me, on Ubuntu when the destructor is called functions in tk have already been gone and hence the deletecommand(funcid) raises Can't delete Tcl command. As a workaround to Ubuntu specific behaviour, and maybe future changes in the tk library itself, exception handling could be added around the deletecommand(funcid) call, avoiding exception being raised when funcid does not exists anymore. What's your opinion?

Copy link
Member

terryjreedy left a comment

I think a new test is needed to verify that the revision does what we want and expect it to.

self.tk.call('bind', self._w, sequence, '')
"""Unbind for this widget the event SEQUENCE. if
FUNCID is given, delete the command also."""
bound = ''
if funcid:
self.deletecommand(funcid)

This comment has been minimized.

Copy link
@terryjreedy

terryjreedy Jan 14, 2020

Member

The test failure is here, _tkinter.TclError: can't delete Tcl command. In the absence of more specific information, I interpret this as tk, on Linux, refusing to delete a command that is in use. Try moving the deletion back to after the rebinding, as it originally was. It is OK to have two if funcid clauses.

This comment has been minimized.

Copy link
@GiovaLomba

GiovaLomba Jan 14, 2020

Author

Should this work? I just give it a try with ubuntu tests and it worked.

    def unbind(self, sequence, funcid=None):
        """Unbind for this widget the event SEQUENCE. if
        FUNCID is given, delete the command also."""
        funcs = self.tk.call('bind', self._w, sequence, '')
        if funcid:
            self.deletecommand(funcid)
            keep = '\n'.join(f for f in funcs.split('\n') if not f.startswith(f'if {{"[{funcid}'))
            self.tk.call('bind', self._w, sequence, keep)

Where should the test be written? Would the test just check that after unbind with given funcid, other bound function are there and the given one is gone?

self.tk.call('bind', self._w, sequence, '')
"""Unbind for this widget the event SEQUENCE. if
FUNCID is given, delete the command also."""
bound = ''

This comment has been minimized.

Copy link
@terryjreedy

terryjreedy Jan 14, 2020

Member

keep or rebind strike me as better names, but follow Serhiy if he disagrees.

This comment has been minimized.

Copy link
@GiovaLomba

GiovaLomba Jan 14, 2020

Author

I'm using keep.

if funcid:
self.deletecommand(funcid)
funcs = self.tk.call('bind', self._w, sequence, None).split('\n')
bound = '\n'.join([f for f in funcs if not f.startswith(f'if {{"[{funcid}')])

This comment has been minimized.

Copy link
@terryjreedy

terryjreedy Jan 14, 2020

Member

join does not need the intermediate list and the second iteration. Remove '[' and ']' to leave a generator expression (comprehension).

I don't understand f.startswith(f'if {{"[{funcid}'), ie, why the func corresponding to funcid should necessarily be a string starting with something like if {"[<funcid>. Does this have something to do with how tcl wraps python functions? If so, should we assume that the bound function is such?

This comment has been minimized.

Copy link
@GiovaLomba

GiovaLomba Jan 14, 2020

Author

You'right for the generator.
Neither do I. After having done several tests to check how callbacks are bound, it came out that they always start with that pattern, so I assumed this is "how tcl wraps python functions".

@@ -0,0 +1 @@
On the tkinter library, the widget.unbind(<Sequence>, funcid) call unbinds all bindings for <Sequence>, while documentation states that when funcid is given all other bindings remains untuched. Apparently this behaviour is not offered by tkinter itself and, in case of non-empty funcid argument value be given, should be obtained by, at first, unbinding all bindings and then bind again all the ones not having the given funcid. This is basically what the proposed patch does. It takes account of the modifications done into the unbind method docstring, as described by other PSF members on the issue tracker.

This comment has been minimized.

Copy link
@terryjreedy

terryjreedy Jan 14, 2020

Member

This is one line of about 500 chars. I think we usually comment with line breaks, but I don't know if it makes much difference. In any case, this seems like more explanation than needed by users, who are the audience for news items.

The technical details can be appropriate for the commit message. You can edit the initial message on the PR to include a suggested commit message (about the bpo link).

@bedevere-bot

This comment has been minimized.

Copy link

bedevere-bot commented Jan 14, 2020

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.