Skip to content

Correct handle default backend. #19855

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

Merged
merged 1 commit into from
Apr 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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