From dde53f69f11571161c1db7c43ebfc3cfdfcc6f96 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 7 Jan 2021 08:16:57 -0800 Subject: [PATCH] use tokenize.open to avoid incorrect line endings at beginning of file --- pycodestyle.py | 6 ++---- testsuite/crlf.py | 3 +++ 2 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 testsuite/crlf.py diff --git a/pycodestyle.py b/pycodestyle.py index 17fac7a8..2f4313ff 100755 --- a/pycodestyle.py +++ b/pycodestyle.py @@ -1814,10 +1814,8 @@ def readlines(filename): def readlines(filename): """Read the source code.""" try: - with open(filename, 'rb') as f: - (coding, lines) = tokenize.detect_encoding(f.readline) - f = TextIOWrapper(f, coding, line_buffering=True) - return [line.decode(coding) for line in lines] + f.readlines() + with tokenize.open(filename) as f: + return f.readlines() except (LookupError, SyntaxError, UnicodeError): # Fall back if file encoding is improperly declared with open(filename, encoding='latin-1') as f: diff --git a/testsuite/crlf.py b/testsuite/crlf.py new file mode 100644 index 00000000..41f0d0fc --- /dev/null +++ b/testsuite/crlf.py @@ -0,0 +1,3 @@ +'''\ +test +'''