Skip to content

Commit 88b4a0d

Browse files
committed
Support quoted strings in matplotlibrc
This enables using the comment character # within strings. Closes #19288. Superseeds #22565.
1 parent 2bb9b03 commit 88b4a0d

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

lib/matplotlib/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
780780
try:
781781
for line_no, line in enumerate(fd, 1):
782782
line = transform(line)
783-
strippedline = line.split('#', 1)[0].strip()
783+
strippedline = cbook._strip_comment(line)
784784
if not strippedline:
785785
continue
786786
tup = strippedline.split(':', 1)
@@ -791,6 +791,8 @@ def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
791791
key, val = tup
792792
key = key.strip()
793793
val = val.strip()
794+
if val.startswith('"') and val.endswith('"'):
795+
val = val[1:-1] # strip double quotes
794796
if key in rc_temp:
795797
_log.warning('Duplicate key in file %r, line %d (%r)',
796798
fname, line_no, line.rstrip('\n'))

lib/matplotlib/cbook/__init__.py

+15
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,21 @@ def strip_math(s):
400400
return s
401401

402402

403+
def _strip_comment(s):
404+
"""Strip everything from the first unquoted #."""
405+
pos = 0
406+
while True:
407+
quote_pos = s.find('"', pos)
408+
hash_pos = s.find('#', pos)
409+
if quote_pos < 0:
410+
without_comment = s if hash_pos < 0 else s[:hash_pos]
411+
return without_comment.strip()
412+
elif 0 < hash_pos < quote_pos:
413+
return s[:hash_pos].strip()
414+
else:
415+
pos = s.find('"', quote_pos + 1) + 1 # behind closing quote
416+
417+
403418
def is_writable_file_like(obj):
404419
"""Return whether *obj* looks like a file object with a *write* method."""
405420
return callable(getattr(obj, 'write', None))

lib/matplotlib/mpl-data/matplotlibrc

+9-5
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@
3333
## Colors: for the color values below, you can either use
3434
## - a Matplotlib color string, such as r, k, or b
3535
## - an RGB tuple, such as (1.0, 0.5, 0.0)
36-
## - a hex string, such as ff00ff
36+
## - a hex string, such as "#ff00ff" (for backward compatibility, but
37+
## discouraged, also ff00ff is supported).
3738
## - a scalar grayscale intensity such as 0.75
3839
## - a legal html color name, e.g., red, blue, darkslategray
3940
##
41+
## String values may optionally be enclosed in double quotes, which allows
42+
## using the comment character # in the string.
43+
##
4044
## Matplotlib configuration are currently divided into following parts:
4145
## - BACKENDS
4246
## - LINES
@@ -506,10 +510,10 @@
506510
## ***************************************************************************
507511
## * GRIDS *
508512
## ***************************************************************************
509-
#grid.color: b0b0b0 # grid color
510-
#grid.linestyle: - # solid
511-
#grid.linewidth: 0.8 # in points
512-
#grid.alpha: 1.0 # transparency, between 0.0 and 1.0
513+
#grid.color: "#b0b0b0" # grid color
514+
#grid.linestyle: - # solid
515+
#grid.linewidth: 0.8 # in points
516+
#grid.alpha: 1.0 # transparency, between 0.0 and 1.0
513517

514518

515519
## ***************************************************************************

lib/matplotlib/tests/test_cbook.py

+15
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,21 @@ def test_func2():
408408
cb.process("test2")
409409

410410

411+
@pytest.mark.parametrize('line, result', [
412+
('a : no_comment', 'a : no_comment'),
413+
('a : "quoted str"', 'a : "quoted str"'),
414+
('a : "quoted str" # comment', 'a : "quoted str"'),
415+
('a : "#000000"', 'a : "#000000"'),
416+
('a : "#000000" # comment', 'a : "#000000"'),
417+
('a : ["#000000", "#FFFFFF"]', 'a : ["#000000", "#FFFFFF"]'),
418+
('a : ["#000000", "#FFFFFF"] # comment', 'a : ["#000000", "#FFFFFF"]'),
419+
('a : val # a comment "with quotes"', 'a : val')
420+
])
421+
def test_strip_comment(line, result):
422+
"""Strip everything from the first unqouted #."""
423+
assert cbook._strip_comment(line) == result
424+
425+
411426
def test_sanitize_sequence():
412427
d = {'a': 1, 'b': 2, 'c': 3}
413428
k = ['a', 'b', 'c']

0 commit comments

Comments
 (0)