Skip to content

Commit b909939

Browse files
committed
Make backend_gtk3foo importable on headless environments.
This should ultimately make it easier to generate apidocs on CI. The change in value of `cursord` only happens on headless envs, where the module was not importable at all before, so that's not an API break.
1 parent 303873f commit b909939

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``backend_gtk3.cursord``
2+
~~~~~~~~~~~~~~~~~~~~~~~~
3+
This dict is deprecated, in order to make the module importable on headless
4+
environments.

lib/matplotlib/backends/backend_gtk3.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,15 @@
3737

3838
try:
3939
_display = Gdk.Display.get_default()
40-
cursord = {
40+
cursord = { # deprecated in Matplotlib 3.5.
4141
cursors.MOVE: Gdk.Cursor.new_from_name(_display, "move"),
4242
cursors.HAND: Gdk.Cursor.new_from_name(_display, "pointer"),
4343
cursors.POINTER: Gdk.Cursor.new_from_name(_display, "default"),
4444
cursors.SELECT_REGION: Gdk.Cursor.new_from_name(_display, "crosshair"),
4545
cursors.WAIT: Gdk.Cursor.new_from_name(_display, "wait"),
4646
}
4747
except TypeError as exc:
48-
# Happens when running headless. Convert to ImportError to cooperate with
49-
# backend switching.
50-
raise ImportError(exc) from exc
48+
cursord = {} # deprecated in Matplotlib 3.5.
5149

5250

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

487+
@staticmethod
488+
@functools.lru_cache()
489+
def _mpl_to_gtk_cursor(mpl_cursor):
490+
name = {
491+
cursors.MOVE: "move",
492+
cursors.HAND: "pointer",
493+
cursors.POINTER: "default",
494+
cursors.SELECT_REGION: "crosshair",
495+
cursors.WAIT: "wait",
496+
}[mpl_cursor]
497+
return Gdk.Cursor.new_from_name(Gdk.Display.get_default(), name)
498+
489499
def set_cursor(self, cursor):
490500
window = self.canvas.get_property("window")
491501
if window is not None:
492-
window.set_cursor(cursord[cursor])
502+
window.set_cursor(self._mpl_to_gtk_cursor(cursor))
493503
Gtk.main_iteration()
494504

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

0 commit comments

Comments
 (0)