Skip to content
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

avoid pylint crash on ill-formatted template strings #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pdawyndt
Copy link
Owner

@pdawyndt pdawyndt commented Oct 12, 2016

We use pylint to evaluate student submission on an electronic learning environments. Pylint crashed while evaluating this submitted code snippet:

A = int(input())
B = int(input())
C = int(input())

M = ((A - C) * (B - C)) / C

print("There are {.:2f} undiscovered errors.".format(M))

Clearly the template string is ill-structured (dot and colon should be swapped) and pylint should annotate this as such (not crash). Here's the stack trace:

Traceback (most recent call last):
  File "/mnt/d20bjaD5tEDaCnle2gIkwg/judge/lint_judge.py", line 136, in run_pylint
    exit=False
  File "/opt/conda/lib/python3.5/site-packages/pylint/lint.py", line 1310, in __init__
    linter.check(args)
  File "/opt/conda/lib/python3.5/site-packages/pylint/lint.py", line 732, in check
    self._do_check(files_or_modules)
  File "/opt/conda/lib/python3.5/site-packages/pylint/lint.py", line 863, in _do_check
    self.check_astroid_module(ast_node, walker, rawcheckers, tokencheckers)
  File "/opt/conda/lib/python3.5/site-packages/pylint/lint.py", line 944, in check_astroid_module
    walker.walk(ast_node)
  File "/opt/conda/lib/python3.5/site-packages/pylint/utils.py", line 944, in walk
    self.walk(child)
  File "/opt/conda/lib/python3.5/site-packages/pylint/utils.py", line 944, in walk
    self.walk(child)
  File "/opt/conda/lib/python3.5/site-packages/pylint/utils.py", line 944, in walk
    self.walk(child)
  File "/opt/conda/lib/python3.5/site-packages/pylint/utils.py", line 941, in walk
    cb(astroid)
  File "/opt/conda/lib/python3.5/site-packages/pylint/checkers/strings.py", line 330, in visit_call
    self._check_new_format(node, func)
  File "/opt/conda/lib/python3.5/site-packages/pylint/checkers/strings.py", line 364, in _check_new_format
    fields, num_args, manual_pos = parse_format_method_string(strnode.value)
  File "/opt/conda/lib/python3.5/site-packages/pylint/checkers/strings.py", line 187, in parse_format_method_string
    keys.append((keyname, list(fielditerator)))
ValueError: Empty attribute in format string

I have caught the exception in the parse_format_method_string and raised an utils.IncompleteFormatString() exception instead in this pull request. This should avoid the pylint crash. Someone should check if the error handling results in the correct pylint annotation, or to see if the problem could be handled earlier (because format string is not valid according to PEP 3101).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant