summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormihai_stan <devnull@localhost>2015-02-05 18:47:10 +0200
committermihai_stan <devnull@localhost>2015-02-05 18:47:10 +0200
commitb4e4382b009d84cf0f7665aa872d6f01eeb5f6a1 (patch)
tree37907ca626e09c35e6c236d44746e925264319ff
parentf2cde1f92acf660c178a17f7c774b4061e7771b8 (diff)
downloadpylint-b4e4382b009d84cf0f7665aa872d6f01eeb5f6a1.tar.gz
Added C0403 invalid-characters-in-docstring for docstrings with invalid characters (e.g. \x00)
-rw-r--r--checkers/spelling.py13
-rw-r--r--test/unittest_checker_spelling.py12
2 files changed, 22 insertions, 3 deletions
diff --git a/checkers/spelling.py b/checkers/spelling.py
index c716ff2..f6edd5d 100644
--- a/checkers/spelling.py
+++ b/checkers/spelling.py
@@ -61,6 +61,9 @@ class SpellingChecker(BaseTokenChecker):
'%s\nDid you mean: \'%s\'?',
'wrong-spelling-in-docstring',
'Used when a word in docstring is not spelled correctly.'),
+ 'C0403': ('Invalid characters %r in a docstring',
+ 'invalid-characters-in-docstring',
+ 'Used when a word in docstring cannot be checked by enchant.'),
}
options = (('spelling-dict',
{'default' : '', 'type' : 'choice', 'metavar' : '<dict name>',
@@ -131,8 +134,6 @@ class SpellingChecker(BaseTokenChecker):
line2 = re.sub("([^a-zA-Z]|^)'", " ", line2)
# Replace punctuation signs with space e.g. and/or -> and or
line2 = self.punctuation_regex.sub(' ', line2)
- # Replace null bytes with space
- line2 = line2.replace('\x00', ' ')
words = []
for word in line2.split():
@@ -170,7 +171,13 @@ class SpellingChecker(BaseTokenChecker):
word = word[2:]
# If it is a known word, then continue.
- if self.spelling_dict.check(word):
+ try:
+ if self.spelling_dict.check(word):
+ continue
+ except enchant.errors.Error:
+ # this can only happen in docstrings, not comments
+ self.add_message('invalid-characters-in-docstring',
+ line=line_num, args=(word,))
continue
# Store word to private dict or raise a message.
diff --git a/test/unittest_checker_spelling.py b/test/unittest_checker_spelling.py
index 8b55042..c630eb2 100644
--- a/test/unittest_checker_spelling.py
+++ b/test/unittest_checker_spelling.py
@@ -76,6 +76,18 @@ class SpellingCheckerTest(CheckerTestCase):
"comet' or 'comment' or 'moment' or 'foment"))):
self.checker.visit_class(stmt)
+ @unittest.skipIf(spell_dict is None,
+ "missing python-enchant package or missing "
+ "spelling dictionaries")
+ @set_config(spelling_dict=spell_dict)
+ def test_invalid_docstring_characters(self):
+ stmt = test_utils.extract_node(
+ 'def fff():\n """test\\x00"""\n pass')
+ with self.assertAddsMessages(
+ Message('invalid-characters-in-docstring', line=2,
+ args=('test\x00',))):
+ self.checker.visit_function(stmt)
+
if __name__ == '__main__':
unittest.main()