Skip to content

Make guiEvent available only within the event handlers. #25559

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

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/api/next_api_changes/deprecations/25559-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Accessing ``event.guiEvent`` after event handlers return
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
... is deprecated: for some GUI toolkits, it is unsafe to do so. In the
future, ``event.guiEvent`` will be set to None once the event handlers return;
you may separately stash the object at your own risk.
19 changes: 17 additions & 2 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1257,11 +1257,26 @@ class Event:
def __init__(self, name, canvas, guiEvent=None):
self.name = name
self.canvas = canvas
self.guiEvent = guiEvent
self._guiEvent = guiEvent
self._guiEvent_deleted = False

def _process(self):
"""Generate an event with name ``self.name`` on ``self.canvas``."""
"""Process this event on ``self.canvas``, then unset ``guiEvent``."""
self.canvas.callbacks.process(self.name, self)
self._guiEvent_deleted = True

@property
def guiEvent(self):
# After deprecation elapses: remove _guiEvent_deleted; make guiEvent a plain
# attribute set to None by _process.
if self._guiEvent_deleted:
_api.warn_deprecated(
"3.8", message="Accessing guiEvent outside of the original GUI event "
"handler is unsafe and deprecated since %(since)s; in the future, the "
"attribute will be set to None after quitting the event handler. You "
"may separately record the value of the guiEvent attribute at your own "
"risk.")
return self._guiEvent


class DrawEvent(Event):
Expand Down