diff options
author | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-01-03 10:00:00 +0200 |
---|---|---|
committer | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-01-03 10:00:00 +0200 |
commit | 4ad0921cb4e75f0fab120c845d73990075da152d (patch) | |
tree | badab4ca72cf7d8cc6af18f38776dddffa1ae4da | |
parent | 98f292601d5db900732842031db3b22366f799aa (diff) | |
download | pylint-4ad0921cb4e75f0fab120c845d73990075da152d.tar.gz |
Use the new Module.stream, since Module.file_stream is deprecated.
-rw-r--r-- | checkers/misc.py | 11 | ||||
-rw-r--r-- | checkers/similar.py | 3 | ||||
-rw-r--r-- | examples/custom_raw.py | 9 | ||||
-rw-r--r-- | interfaces.py | 2 | ||||
-rw-r--r-- | lint.py | 10 | ||||
-rw-r--r-- | utils.py | 21 |
6 files changed, 24 insertions, 32 deletions
diff --git a/checkers/misc.py b/checkers/misc.py index d4adfd7..7fbe70b 100644 --- a/checkers/misc.py +++ b/checkers/misc.py @@ -82,8 +82,6 @@ class EncodingChecker(BaseChecker): """inspect the source file to find encoding problem or fixmes like notes """ - stream = module.file_stream - stream.seek(0) # XXX may be removed with astroid > 0.23 if self.config.notes: notes = re.compile( r'.*?#\s*(%s)(:*\s*.+)' % "|".join(self.config.notes)) @@ -94,10 +92,11 @@ class EncodingChecker(BaseChecker): else: encoding = 'ascii' - for lineno, line in enumerate(stream): - line = self._check_encoding(lineno + 1, line, encoding) - if line is not None and notes: - self._check_note(notes, lineno + 1, line) + with module.stream() as stream: + for lineno, line in enumerate(stream): + line = self._check_encoding(lineno + 1, line, encoding) + if line is not None and notes: + self._check_note(notes, lineno + 1, line) def register(linter): diff --git a/checkers/similar.py b/checkers/similar.py index 013f1b0..593d541 100644 --- a/checkers/similar.py +++ b/checkers/similar.py @@ -42,7 +42,6 @@ class Similar(object): def append_stream(self, streamid, stream, encoding=None): """append a file to search for similarities""" - stream.seek(0) # XXX may be removed with astroid > 0.23 if encoding is None: readlines = stream.readlines else: @@ -300,7 +299,7 @@ class SimilarChecker(BaseChecker, Similar): stream must implement the readlines method """ - self.append_stream(self.linter.current_name, node.file_stream, node.file_encoding) + self.append_stream(self.linter.current_name, node.stream(), node.file_encoding) def close(self): """compute and display similarities on closing (i.e. end of parsing)""" diff --git a/examples/custom_raw.py b/examples/custom_raw.py index 00fbe89..da576a2 100644 --- a/examples/custom_raw.py +++ b/examples/custom_raw.py @@ -18,11 +18,12 @@ class MyRawChecker(BaseChecker): def process_module(self, node): """process a module - the module's content is accessible via node.file_stream object + the module's content is accessible via node.stream() function """ - for (lineno, line) in enumerate(node.file_stream): - if line.rstrip().endswith('\\'): - self.add_message('W9901', line=lineno) + with module.stream() as stream: + for (lineno, line) in enumerate(stream): + if line.rstrip().endswith('\\'): + self.add_message('W9901', line=lineno) def register(linter): diff --git a/interfaces.py b/interfaces.py index 067aaa6..64f5a95 100644 --- a/interfaces.py +++ b/interfaces.py @@ -46,7 +46,7 @@ class IRawChecker(IChecker): def process_module(self, astroid): """ process a module - the module's content is accessible via astroid.file_stream + the module's content is accessible via astroid.stream """ @@ -861,16 +861,6 @@ class PyLinter(configuration.OptionsManagerMixIn, rawcheckers, tokencheckers): """Check a module from its astroid representation.""" try: - return self._check_astroid_module(ast_node, walker, - rawcheckers, tokencheckers) - finally: - # Close the streams opened by the ast module. - ast_node.close() - - def _check_astroid_module(self, ast_node, walker, - rawcheckers, tokencheckers): - # call raw checkers if possible - try: tokens = utils.tokenize_module(ast_node) except tokenize.TokenError as ex: self.add_message('syntax-error', line=ex.args[1][0], args=ex.args[0]) @@ -128,16 +128,19 @@ def category_id(cid): return MSG_TYPES_LONG.get(cid) +def _decoding_readline(stream, module): + return lambda: stream.readline().decode(module.file_encoding, + 'replace') + + def tokenize_module(module): - stream = module.file_stream - stream.seek(0) - readline = stream.readline - if sys.version_info < (3, 0): - if module.file_encoding is not None: - readline = lambda: stream.readline().decode(module.file_encoding, - 'replace') - return list(tokenize.generate_tokens(readline)) - return list(tokenize.tokenize(readline)) + with module.stream() as stream: + readline = stream.readline + if sys.version_info < (3, 0): + if module.file_encoding is not None: + readline = _decoding_readline(stream, module) + return list(tokenize.generate_tokens(readline)) + return list(tokenize.tokenize(readline)) def build_message_def(checker, msgid, msg_tuple): if implements(checker, (IRawChecker, ITokenChecker)): |