Close file stream in qute_pylint.modeline

This commit is contained in:
Florian Bruhin 2016-07-23 17:00:05 +02:00
parent 3d4a01ef4c
commit 04fdce9058

View File

@ -19,6 +19,7 @@
"""Checker for vim modelines in files.""" """Checker for vim modelines in files."""
import os.path import os.path
import contextlib
from pylint import interfaces, checkers from pylint import interfaces, checkers
@ -41,19 +42,20 @@ class ModelineChecker(checkers.BaseChecker):
if os.path.basename(os.path.splitext(node.file)[0]) == '__init__': if os.path.basename(os.path.splitext(node.file)[0]) == '__init__':
return return
max_lineno = 1 max_lineno = 1
for (lineno, line) in enumerate(node.file_stream): with contextlib.closing(node.stream()) as stream:
if lineno == 1 and line.startswith(b'#!'): for (lineno, line) in enumerate(stream):
max_lineno += 1 if lineno == 1 and line.startswith(b'#!'):
continue max_lineno += 1
elif line.startswith(b'# vim:'): continue
if lineno > max_lineno: elif line.startswith(b'# vim:'):
self.add_message('modeline-position', line=lineno) if lineno > max_lineno:
if (line.rstrip() != self.add_message('modeline-position', line=lineno)
b'# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:'): if (line.rstrip() != b'# vim: ft=python '
self.add_message('invalid-modeline', line=lineno) b'fileencoding=utf-8 sts=4 sw=4 et:'):
break self.add_message('invalid-modeline', line=lineno)
else: break
self.add_message('modeline-missing', line=1) else:
self.add_message('modeline-missing', line=1)
def register(linter): def register(linter):