Skip to content

Commit

Permalink
Do not lowercase module:// backends
Browse files Browse the repository at this point in the history
  • Loading branch information
ksunden committed Jun 26, 2024
1 parent 57cd5eb commit 6b60694
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/matplotlib/backends/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ def __init__(self):
}

def _backend_module_name(self, backend):
if backend.startswith("module://"):
return backend[9:]

# Return name of module containing the specified backend.
# Does not check if the backend is valid, use is_valid_backend for that.
backend = backend.lower()
Expand Down Expand Up @@ -189,7 +192,8 @@ def _validate_and_store_entry_points(self, entries):
def backend_for_gui_framework(self, framework):
"""
Return the name of the backend corresponding to the specified GUI framework.
:
return backend[9:]
Parameters
----------
framework : str
Expand Down Expand Up @@ -224,7 +228,8 @@ def is_valid_backend(self, backend):
bool
True if backend is valid, False otherwise.
"""
backend = backend.lower()
if not backend.startswith("module://"):
backend = backend.lower()

# For backward compatibility, convert ipympl and matplotlib-inline long
# module:// names to their shortened forms.
Expand Down Expand Up @@ -342,7 +347,8 @@ def resolve_backend(self, backend):
The GUI framework, which will be None for a backend that is non-interactive.
"""
if isinstance(backend, str):
backend = backend.lower()
if not backend.startswith("module://"):
backend = backend.lower()
else: # Might be _auto_backend_sentinel or None
# Use whatever is already running...
from matplotlib import get_backend
Expand Down Expand Up @@ -395,7 +401,8 @@ def resolve_gui_or_backend(self, gui_or_backend):
framework : str or None
The GUI framework, which will be None for a backend that is non-interactive.
"""
gui_or_backend = gui_or_backend.lower()
if not gui_or_backend.startswith("module://"):
gui_or_backend = gui_or_backend.lower()

# First check if it is a gui loop name.
backend = self.backend_for_gui_framework(gui_or_backend)
Expand Down
9 changes: 9 additions & 0 deletions lib/matplotlib/tests/test_backend_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ def test_is_valid_backend(backend, is_valid):
assert backend_registry.is_valid_backend(backend) == is_valid


@pytest.mark.parametrize("backend, normalized", [
("agg", "matplotlib.backends.backend_agg"),
("QtAgg", "matplotlib.backends.backend_qtagg"),
("module://Anything", "Anything"),
])
def test_backend_normalization(backend, normalized):
assert backend_registry._backend_module_name(backend) == normalized


def test_deprecated_rcsetup_attributes():
match = "was deprecated in Matplotlib 3.9"
with pytest.warns(mpl.MatplotlibDeprecationWarning, match=match):
Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_backend_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,14 @@ def test_show_old_global_api(monkeypatch):
mpl.use("module://mpl_test_backend")
plt.show()
mock_show.assert_called_with()


def test_load_case_sensitive(monkeypatch):
mpl_test_backend = SimpleNamespace(**vars(backend_template))
mock_show = MagicMock()
monkeypatch.setattr(
mpl_test_backend.FigureManagerTemplate, "pyplot_show", mock_show)
monkeypatch.setitem(sys.modules, "mpl_Test_Backend", mpl_test_backend)
mpl.use("module://mpl_Test_Backend")
plt.show()
mock_show.assert_called_with()

0 comments on commit 6b60694

Please sign in to comment.