diff options
| author | Anthony Sottile <asottile@umich.edu> | 2020-01-16 22:13:26 +0000 |
|---|---|---|
| committer | Anthony Sottile <asottile@umich.edu> | 2020-01-16 22:13:26 +0000 |
| commit | 8f9f2813c3cde29703d8ec95b64fac0466037801 (patch) | |
| tree | 479f337b0940a352e8a1f74dd366a67784630de7 /src | |
| parent | 14293cc973dd209e3fb12bb015d46a62657a31bc (diff) | |
| parent | 32c7ebcd7b527a70e96906e3d995ab79a5c39464 (diff) | |
| download | flake8-8f9f2813c3cde29703d8ec95b64fac0466037801.tar.gz | |
Merge branch 'form_feed_difference' into 'master'
split lines the same when read from stdin
Closes #270
See merge request pycqa/flake8!406
Diffstat (limited to 'src')
| -rw-r--r-- | src/flake8/processor.py | 2 | ||||
| -rw-r--r-- | src/flake8/utils.py | 34 |
2 files changed, 20 insertions, 16 deletions
diff --git a/src/flake8/processor.py b/src/flake8/processor.py index 498b9c7..aa7f1d8 100644 --- a/src/flake8/processor.py +++ b/src/flake8/processor.py @@ -334,7 +334,7 @@ class FileProcessor(object): def read_lines_from_stdin(self): # type: () -> List[str] """Read the lines from standard in.""" - return utils.stdin_get_value().splitlines(True) + return utils.stdin_get_lines() def should_ignore_file(self): # type: () -> bool diff --git a/src/flake8/utils.py b/src/flake8/utils.py index 5c7232c..29df9bc 100644 --- a/src/flake8/utils.py +++ b/src/flake8/utils.py @@ -13,6 +13,7 @@ from typing import Callable, Dict, Generator, List, Optional, Pattern from typing import Sequence, Set, Tuple, Union from flake8 import exceptions +from flake8._compat import lru_cache if False: # `typing.TYPE_CHECKING` was introduced in 3.5.2 from flake8.plugins.manager import Plugin @@ -189,28 +190,31 @@ def normalize_path(path, parent=os.curdir): return path.rstrip(separator + alternate_separator) -def _stdin_get_value_py3(): # type: () -> io.StringIO +def _stdin_get_value_py3(): # type: () -> str stdin_value = sys.stdin.buffer.read() fd = io.BytesIO(stdin_value) try: - (coding, lines) = tokenize.detect_encoding(fd.readline) - return io.StringIO(stdin_value.decode(coding)) + coding, _ = tokenize.detect_encoding(fd.readline) + return stdin_value.decode(coding) except (LookupError, SyntaxError, UnicodeError): - return io.StringIO(stdin_value.decode("utf-8")) + return stdin_value.decode("utf-8") -def stdin_get_value(): - # type: () -> str +@lru_cache(maxsize=1) +def stdin_get_value(): # type: () -> str """Get and cache it so plugins can use it.""" - cached_value = getattr(stdin_get_value, "cached_stdin", None) - if cached_value is None: - if sys.version_info < (3, 0): - stdin_value = io.BytesIO(sys.stdin.read()) - else: - stdin_value = _stdin_get_value_py3() - stdin_get_value.cached_stdin = stdin_value # type: ignore - cached_value = stdin_get_value.cached_stdin # type: ignore - return cached_value.getvalue() + if sys.version_info < (3,): + return sys.stdin.read() + else: + return _stdin_get_value_py3() + + +def stdin_get_lines(): # type: () -> List[str] + """Return lines of stdin split according to file splitting.""" + if sys.version_info < (3,): + return list(io.BytesIO(stdin_get_value())) + else: + return list(io.StringIO(stdin_get_value())) def parse_unified_diff(diff=None): |
