We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 3d7dea5 commit a7eecc2Copy full SHA for a7eecc2
2 files changed
lib/matplotlib/rcsetup.py
@@ -700,6 +700,13 @@ def cycler(*args, **kwargs):
700
return reduce(operator.add, (ccycler(k, v) for k, v in validated))
701
702
703
+class _DunderChecker(ast.NodeVisitor):
704
+ def visit_Attribute(self, node):
705
+ if node.attr.startswith("__") and node.attr.endswith("__"):
706
+ raise ValueError("cycler strings with dunders are forbidden")
707
+ self.generic_visit(node)
708
+
709
710
def validate_cycler(s):
711
"""Return a Cycler object from a string repr or the object itself."""
712
if isinstance(s, str):
@@ -715,9 +722,7 @@ def validate_cycler(s):
715
722
# We should replace this eval with a combo of PyParsing and
716
723
# ast.literal_eval()
717
724
try:
718
- if '.__' in s.replace(' ', ''):
719
- raise ValueError("'%s' seems to have dunder methods. Raising"
720
- " an exception for your safety")
725
+ _DunderChecker().visit(ast.parse(s))
721
726
s = eval(s, {'cycler': cycler, '__builtins__': {}})
727
except BaseException as e:
728
raise ValueError("'%s' is not a valid cycler construction: %s" %
lib/matplotlib/tests/test_rcparams.py
@@ -278,6 +278,17 @@ def generate_validator_testcases(valid):
278
('cycler("bleh, [])', ValueError), # syntax error
279
('Cycler("linewidth", [1, 2, 3])',
280
ValueError), # only 'cycler()' function is allowed
281
+ # do not allow dunder in string literals
282
+ ("cycler('c', [j.__class__(j) for j in ['r', 'b']])",
283
+ ValueError),
284
+ ("cycler('c', [j. __class__(j) for j in ['r', 'b']])",
285
286
+ ("cycler('c', [j.\t__class__(j) for j in ['r', 'b']])",
287
288
+ ("cycler('c', [j.\u000c__class__(j) for j in ['r', 'b']])",
289
290
+ ("cycler('c', [j.__class__(j).lower() for j in ['r', 'b']])",
291
292
('1 + 2', ValueError), # doesn't produce a Cycler object
293
('os.system("echo Gotcha")', ValueError), # os not available
294
('import os', ValueError), # should not be able to import
0 commit comments