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-32857: tkinter after_cancel to raise error if called with None #5701
Conversation
|
||
# Cancel timer event. | ||
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1)) | ||
self.assertIn(script, root._tclCommands) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_tclCommands
is an implementation detail. It would be better to not use it in tests. Otherwise changing the implementation (it already was changed in the past) will break tests.
But you can test a side effect of the script by calling root.tk.call(script)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this suggestion. I wasn't quite sure how to test that the script
still existed after the cancel.
with self.assertRaises(ValueError): | ||
root.after_cancel(None) | ||
|
||
# A non-existent id raises a TclError, which is caught in after_cancel. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment looks misleading. This is the test for after_cancel()
which doesn't raise a TclError.
Actually I think that checking that root.tk.call('after', 'info', 'spam')
raises a TclError is not needed. Using this command in after_cancel()
is an implementation detail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
@@ -48,6 +48,39 @@ def test_tk_setPalette(self): | |||
'^must specify a background color$', | |||
root.tk_setPalette, highlightColor='blue') | |||
|
|||
def test_after_cancel(self): | |||
root = self.root | |||
timer1 = root.after(5000, lambda: 'break') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make commands having a side effect (for example adding an item to the list) and check that after canceling events and calling update()
they are not executed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made a counter within a callback function. Is that OK or is a list a better solution?
(script, _) = root.tk.splitlist(root.tk.call('after', 'info', timer1)) | ||
self.assertIn(script, root._tclCommands) | ||
root.after_cancel(timer1) | ||
self.assertNotIn(script, root._tclCommands) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here root.tk.call(script)
should raise a TclError and shouldn't have a side effect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! This is very helpful in my understanding.
@@ -0,0 +1 @@ | |||
:mod:`tkinter` change handling of ``None`` parameter call to after_cancel. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please provide more useful information for readers? Like "after_cancel(None) now is error instead of canceling the first scheduled function."
And please add "Patch by your name."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Thanks @csabella for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 2.7, 3.6, 3.7. |
…e. (pythonGH-5701) (cherry picked from commit 74382a3) Co-authored-by: Cheryl Sabella <cheryl.sabella@gmail.com>
GH-5972 is a backport of this pull request to the 3.7 branch. |
Sorry, @csabella and @serhiy-storchaka, I could not cleanly backport this to |
…e. (pythonGH-5701) (cherry picked from commit 74382a3) Co-authored-by: Cheryl Sabella <cheryl.sabella@gmail.com>
GH-5973 is a backport of this pull request to the 3.6 branch. |
@serhiy-storchaka Would you like me to cherry pick this to 2.7? |
Yes, it would be nice. |
…e. (pythonGH-5701) (cherry picked from commit 74382a3)
GH-6620 is a backport of this pull request to the 2.7 branch. |
https://bugs.python.org/issue32857