-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fallback to pgi #4810
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
Fallback to pgi #4810
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
PGI - Pure Python GObject Introspection Bindings | ||
------------------------------------------------ | ||
|
||
For the GTK3 backend, matplotlib now supports PGI bindings as an alternative | ||
to PyGObject. By default matplotlib will still use PyGObject, otherwise it | ||
will look for pgi. You can change this behaviour through the rcParam | ||
backend.gi_preference which takes either a string, or a list of strings in | ||
order of preference. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
try: | ||
import cairocffi as cairo | ||
except ImportError: | ||
try: | ||
import cairo | ||
except ImportError: | ||
raise ImportError( | ||
"Cairo backend requires that cairocffi or pycairo is installed.") | ||
else: | ||
HAS_CAIRO_CFFI = False | ||
else: | ||
HAS_CAIRO_CFFI = True | ||
|
||
_version_required = (1, 2, 0) | ||
if cairo.version_info < _version_required: | ||
raise ImportError("Pycairo %d.%d.%d is installed\n" | ||
"Pycairo %d.%d.%d or later is required" | ||
% (cairo.version_info + _version_required)) | ||
backend_version = cairo.version | ||
del _version_required |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import matplotlib | ||
|
||
error_msg_gtk3 = "Gtk3 backend requires the installation of pygobject or pgi." | ||
|
||
# Import the first library that works from the rcParam list | ||
# throw ImportError if none works | ||
for lib in matplotlib.rcParams['backend.gi_preference']: | ||
try: | ||
gi = __import__(lib, globals(), locals(), [], 0) | ||
break | ||
except ImportError: | ||
pass | ||
else: | ||
raise ImportError(error_msg_gtk3) | ||
|
||
# Check version | ||
try: | ||
gi.require_version("Gtk", "3.0") | ||
except AttributeError: | ||
raise ImportError( | ||
"pygobject version too old -- it must have require_version") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add the minimum required version here? It will be easier for a user if the errors specify which min version is supported. |
||
except ValueError: | ||
raise ImportError( | ||
"Gtk3 backend requires the installation of GObject introspection " | ||
"bindings for Gtk 3") | ||
|
||
# cleanly import pkgs to global scope | ||
try: | ||
pkgs = ['Gtk', 'Gdk', 'GObject', 'GLib'] | ||
name = gi.__name__ + '.repository' | ||
_temp = __import__(name, globals(), locals(), pkgs, 0) | ||
globals().update(dict((k, getattr(_temp, k)) for k in pkgs)) | ||
except (ImportError, AttributeError): | ||
raise ImportError(error_msg_gtk3) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -410,6 +410,9 @@ def deprecate_axes_colorcycle(value): | |
validate_stringlist = _listify_validator(six.text_type) | ||
validate_stringlist.__doc__ = 'return a list' | ||
|
||
validate_gi_preference = _listify_validator(ValidateInStrings( | ||
'backend.gi_preference', ['gi', 'pgi'])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. backend.gi, similarly to backend.qt4. |
||
|
||
validate_orientation = ValidateInStrings( | ||
'orientation', ['landscape', 'portrait']) | ||
|
||
|
@@ -888,6 +891,7 @@ def validate_animation_writer_path(p): | |
'backend_fallback': [True, validate_bool], # agg is certainly present | ||
'backend.qt4': ['PyQt4', validate_qt4], | ||
'backend.qt5': ['PyQt5', validate_qt5], | ||
'backend.gi_preference': [['gi', 'pgi'], validate_gi_preference], | ||
'webagg.port': [8988, validate_int], | ||
'webagg.open_in_browser': [True, validate_bool], | ||
'webagg.port_retries': [50, validate_int], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from __future__ import (absolute_import, division, print_function, | ||
unicode_literals) | ||
|
||
from matplotlib.externals import six | ||
from matplotlib.externals.six import unichr | ||
from matplotlib import pyplot as plt | ||
from matplotlib.testing.decorators import cleanup, switch_backend | ||
from matplotlib.testing.decorators import knownfailureif | ||
from matplotlib._pylab_helpers import Gcf | ||
import copy | ||
|
||
try: | ||
# mock in python 3.3+ | ||
from unittest import mock | ||
except ImportError: | ||
import mock | ||
|
||
try: | ||
from matplotlib.backends.gtk3_compat import Gtk, Gdk, GObject, GLib | ||
HAS_GTK3 = True | ||
except ImportError: | ||
HAS_GTK3 = False | ||
|
||
|
||
def simulate_key_press(canvas, key, modifiers=[]): | ||
event = mock.Mock() | ||
|
||
keyval = [k for k, v in six.iteritems(canvas.keyvald) if v == key] | ||
if keyval: | ||
keyval = keyval[0] | ||
else: | ||
keyval = ord(key) | ||
event.keyval = keyval | ||
|
||
event.state = 0 | ||
for key_mask, prefix in canvas.modifier_keys: | ||
if prefix in modifiers: | ||
event.state |= key_mask | ||
|
||
canvas.key_press_event(None, event) | ||
|
||
|
||
@cleanup | ||
#@knownfailureif(not HAS_GTK3) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unintentionally commented out? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nevermind -- I see your comment about this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, just for Travis testing, it makes it easier to spot import mistakes. With this line commented out, if everything goes well all but python 2.6 should pass. With this line in, it can mask import errors on builds that should pass. I guess I could replace this with a knownfailiureif(sys.version_info < (2, 7)), which would ensure Travis always flags errors, but for people testing on their own machines without GTK3 installed it wouldn't work so well... Do you have an opinion on this? Of course the middle way, perhaps we should add (or perhaps it already exists, but I haven't seen), an |
||
@switch_backend('GTK3Agg') | ||
def test_fig_close(): | ||
#save the state of Gcf.figs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code in that function is not strictly pep8. There are spaces missing after comments. |
||
init_figs = copy.copy(Gcf.figs) | ||
|
||
# make a figure using pyplot interface | ||
fig = plt.figure() | ||
|
||
# simulate user pressing the close shortcut | ||
#simulate_key_press(fig.canvas, 'w', ['ctrl']) | ||
|
||
plt.show() | ||
|
||
# assert that we have removed the reference to the FigureManager | ||
# that got added by plt.figure() | ||
assert(init_figs == Gcf.figs) |
Uh oh!
There was an error while loading. Please reload this page.
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 am not very familiar with the import mechanisms of python. Can you explain why
import lib as gi
wouldn't work here and why not resorting toimportlib
isn't an option? My very little knowledge says thatimport
>importlib
>>__import__
.Right now, it looks like we are trying to be too smart about this.