Skip to content

Commit

Permalink
Correct handle default backend.
Browse files Browse the repository at this point in the history
See comments in `matplotlib/__init__.py` and `setup.py`.  Test
by updating your local repo to this branch and `pip install
git+file:///path/to/repo` (doing so with the current master throws
`ValueError: Key backend: '' is not a valid value for backend`.
  • Loading branch information
anntzer committed Apr 2, 2021
1 parent cc8669f commit 0b078ad
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
6 changes: 6 additions & 0 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,12 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
transform=lambda line: line[1:] if line.startswith("#") else line,
fail_on_error=True)
dict.update(rcParamsDefault, rcsetup._hardcoded_defaults)
# Normally, the default matplotlibrc file contains *no* entry for backend (the
# corresponding line starts with ##, not #; we fill on _auto_backend_sentinel
# in that case. However, packagers can set a different default backend
# (resulting in a normal `#backend: foo` line) in which case we should *not*
# fill in _auto_backend_sentinel.
dict.setdefault(rcParamsDefault, "backend", rcsetup._auto_backend_sentinel)
rcParams = RcParams() # The global instance.
dict.update(rcParams, dict.items(rcParamsDefault))
dict.update(rcParams, _rc_params_in_file(matplotlib_fname()))
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/mpl-data/matplotlibrc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
## PS PDF SVG Template
## You can also deploy your own backend outside of Matplotlib by referring to
## the module name (which must be in the PYTHONPATH) as 'module://my_backend'.
#backend: Agg
##backend: Agg

## The port to use for the web server in the WebAgg backend.
#webagg.port: 8988
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -1411,14 +1411,13 @@ def _convert_validator_spec(key, conv):
"_internal.classic_mode": validate_bool
}
_hardcoded_defaults = { # Defaults not inferred from matplotlibrc.template...
# ... because it can"t be:
"backend": _auto_backend_sentinel,
# ... because they are private:
"_internal.classic_mode": False,
# ... because they are deprecated:
"animation.avconv_path": "avconv",
"animation.avconv_args": [],
"animation.html_args": [],
# backend is handled separately when constructing rcParamsDefault.
}
_validators = {k: _convert_validator_spec(k, conv)
for k, conv in _validators.items()}
10 changes: 6 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,18 @@ def build_extensions(self):


def update_matplotlibrc(path):
# Update the matplotlibrc file if packagers want to change the default
# backend.
# If packagers want to change the default backend, insert a `#backend: ...`
# line. Otherwise, use the default `##backend: Agg` which has no effect
# even after decommenting, which allows _auto_backend_sentinel to be filled
# in at import time.
template_lines = path.read_text().splitlines(True)
backend_line_idx, = [ # Also asserts that there is a single such line.
idx for idx, line in enumerate(template_lines)
if line.startswith("#backend:")]
if "#backend:" in line]
template_lines[backend_line_idx] = (
"#backend: {}".format(setupext.options["backend"])
if setupext.options["backend"]
else "#backend:")
else "##backend: Agg")
path.write_text("".join(template_lines))


Expand Down

0 comments on commit 0b078ad

Please sign in to comment.