Skip to content

Make backend_gtk3foo importable on headless environments. #19441

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
May 20, 2021
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
4 changes: 4 additions & 0 deletions doc/api/next_api_changes/deprecations/19441-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
``backend_gtk3.cursord``
~~~~~~~~~~~~~~~~~~~~~~~~
This dict is deprecated, in order to make the module importable on headless
environments.
20 changes: 15 additions & 5 deletions lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,15 @@

try:
_display = Gdk.Display.get_default()
cursord = {
cursord = { # deprecated in Matplotlib 3.5.
cursors.MOVE: Gdk.Cursor.new_from_name(_display, "move"),
cursors.HAND: Gdk.Cursor.new_from_name(_display, "pointer"),
cursors.POINTER: Gdk.Cursor.new_from_name(_display, "default"),
cursors.SELECT_REGION: Gdk.Cursor.new_from_name(_display, "crosshair"),
cursors.WAIT: Gdk.Cursor.new_from_name(_display, "wait"),
}
except TypeError as exc:
# Happens when running headless. Convert to ImportError to cooperate with
# backend switching.
raise ImportError(exc) from exc
cursord = {} # deprecated in Matplotlib 3.5.


class TimerGTK3(TimerBase):
Expand Down Expand Up @@ -486,10 +484,22 @@ def set_message(self, s):
escaped = GLib.markup_escape_text(s)
self.message.set_markup(f'<small>{escaped}</small>')

@staticmethod
@functools.lru_cache()
def _mpl_to_gtk_cursor(mpl_cursor):
name = {
cursors.MOVE: "move",
cursors.HAND: "pointer",
cursors.POINTER: "default",
cursors.SELECT_REGION: "crosshair",
cursors.WAIT: "wait",
}[mpl_cursor]
return Gdk.Cursor.new_from_name(Gdk.Display.get_default(), name)

def set_cursor(self, cursor):
window = self.canvas.get_property("window")
if window is not None:
window.set_cursor(cursord[cursor])
window.set_cursor(self._mpl_to_gtk_cursor(cursor))
Gtk.main_iteration()

def draw_rubberband(self, event, x0, y0, x1, y1):
Expand Down