summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2018-03-16 17:59:52 +0100
committerClaudiu Popa <pcmanticore@gmail.com>2018-03-16 18:02:02 +0100
commit0a0c3775b8c7dc0ae6ea977273c7d41aadf16158 (patch)
treeef4af303479f7b64729512a401a7c2c396374210
parent2746fe3a0faaf7bc5cd002af9408460f383d867b (diff)
downloadpylint-git-0a0c3775b8c7dc0ae6ea977273c7d41aadf16158.tar.gz
Don't crash on invalid strings when checking for `logging-format-interpolation`
Close #1944
-rw-r--r--ChangeLog5
-rw-r--r--pylint/checkers/logging.py7
-rw-r--r--pylint/test/unittest_checker_logging.py7
3 files changed, 18 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 64f296049..7a85b01b3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,7 @@
Pylint's ChangeLog
------------------
+<<<<<<< HEAD
What's New in Pylint 1.8.3?
==========================
@@ -17,6 +18,10 @@ Release date: -
`__doc__` can be used to specify a docstring for a module without
passing it as a first-statement string.
+ * Don't crash on invalid strings when checking for `logging-format-interpolation`
+
+ Close #1944
+
* Fix false positive ``inconsistent-return-statements`` message by
avoiding useless exception inference if the exception is not handled.
diff --git a/pylint/checkers/logging.py b/pylint/checkers/logging.py
index 051fa7589..f87242fb2 100644
--- a/pylint/checkers/logging.py
+++ b/pylint/checkers/logging.py
@@ -263,7 +263,12 @@ def is_complex_format_str(node):
inferred = utils.safe_infer(node)
if inferred is None or not isinstance(inferred.value, six.string_types):
return True
- for _, _, format_spec, _ in string.Formatter().parse(inferred.value):
+ try:
+ parsed = list(string.Formatter().parse(inferred.value))
+ except ValueError:
+ # This format string is invalid
+ return False
+ for _, _, format_spec, _ in parsed:
if format_spec:
return True
return False
diff --git a/pylint/test/unittest_checker_logging.py b/pylint/test/unittest_checker_logging.py
index 6075a2a2e..a87c3f542 100644
--- a/pylint/test/unittest_checker_logging.py
+++ b/pylint/test/unittest_checker_logging.py
@@ -27,6 +27,13 @@ class TestLoggingModuleDetection(CheckerTestCase):
with self.assertAddsMessages(Message('logging-not-lazy', node=stmts[1])):
self.checker.visit_call(stmts[1])
+ def test_dont_crash_on_invalid_format_string(self):
+ node = astroid.parse('''
+ import logging
+ logging.error('0} - {1}'.format(1, 2))
+ ''')
+ self.walk(node)
+
def test_detects_renamed_standard_logging_module(self):
stmts = astroid.extract_node("""
import logging as blogging #@