Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 01c3ce1

Browse filesBrowse files
committed
Simplify/improve check for pycairo in Gtk-based backends.
Gtk-based backends require pycairo (cairocffi is unsupported: https://pygobject.readthedocs.io/en/latest/guide/cairo_integration.html). Prior to this patch, trying to run gtk{3,4}{agg,cairo} fails *at draw time* `TypeError: Couldn't find foreign struct converter for 'cairo.Context'`). This patch moves the import check for cairo to the top of the common _backend_gtk, and also simplifies backend_gtk{3,4}agg to always use pycairo (in which case backend_cairo._to_context is a no-op) as they cannot inter-operate with cairocffi anyways.
1 parent 3522217 commit 01c3ce1
Copy full SHA for 01c3ce1

File tree

Expand file treeCollapse file tree

3 files changed

+11
-16
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+11
-16
lines changed

‎lib/matplotlib/backends/_backend_gtk.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/_backend_gtk.py
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@
1212
_Backend, FigureManagerBase, NavigationToolbar2, TimerBase)
1313
from matplotlib.backend_tools import Cursors
1414

15+
import gi
1516
# The GTK3/GTK4 backends will have already called `gi.require_version` to set
1617
# the desired GTK.
1718
from gi.repository import Gdk, Gio, GLib, Gtk
1819

1920

21+
try:
22+
gi.require_foreign("cairo")
23+
except ImportError as e:
24+
raise ImportError("Gtk-based backends require cairo") from e
25+
2026
_log = logging.getLogger(__name__)
2127

2228
backend_version = "%s.%s.%s" % (

‎lib/matplotlib/backends/backend_gtk3agg.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_gtk3agg.py
+3-9Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import numpy as np
22

3-
from .. import _api, cbook
4-
try:
5-
from . import backend_cairo
6-
except ImportError as e:
7-
raise ImportError('backend Gtk3Agg requires cairo') from e
3+
from .. import _api, cbook, transforms
84
from . import backend_agg, backend_gtk3
9-
from .backend_cairo import cairo
105
from .backend_gtk3 import Gtk, _BackendGTK3
11-
from matplotlib import transforms
6+
7+
import cairo # Presence of cairo is already checked by _backend_gtk.
128

139

1410
class FigureCanvasGTK3Agg(backend_gtk3.FigureCanvasGTK3,
@@ -32,8 +28,6 @@ def on_draw_event(self, widget, ctx):
3228
else:
3329
bbox_queue = self._bbox_queue
3430

35-
ctx = backend_cairo._to_context(ctx)
36-
3731
for bbox in bbox_queue:
3832
x = int(bbox.x0)
3933
y = h - int(bbox.y1)

‎lib/matplotlib/backends/backend_gtk4agg.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/backend_gtk4agg.py
+2-7Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import numpy as np
22

33
from .. import _api, cbook
4-
try:
5-
from . import backend_cairo
6-
except ImportError as e:
7-
raise ImportError('backend Gtk4Agg requires cairo') from e
84
from . import backend_agg, backend_gtk4
9-
from .backend_cairo import cairo
105
from .backend_gtk4 import Gtk, _BackendGTK4
116

7+
import cairo # Presence of cairo is already checked by _backend_gtk.
8+
129

1310
class FigureCanvasGTK4Agg(backend_gtk4.FigureCanvasGTK4,
1411
backend_agg.FigureCanvasAgg):
@@ -24,8 +21,6 @@ def on_draw_event(self, widget, ctx):
2421
allocation.x, allocation.y,
2522
allocation.width, allocation.height)
2623

27-
ctx = backend_cairo._to_context(ctx)
28-
2924
buf = cbook._unmultiplied_rgba8888_to_premultiplied_argb32(
3025
np.asarray(self.get_renderer().buffer_rgba()))
3126
height, width, _ = buf.shape

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.