From e2d9f0bab5220a6a5b5e27c74d3852bf8a4fb96b Mon Sep 17 00:00:00 2001 From: Aaron Ang Date: Sun, 15 Apr 2018 19:03:24 -0700 Subject: [PATCH 1/7] bpo-11594: Add test to ensure line-endings are respected --- Lib/lib2to3/tests/data/crlf.py | 6 +++--- Lib/lib2to3/tests/test_refactor.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Lib/lib2to3/tests/data/crlf.py b/Lib/lib2to3/tests/data/crlf.py index dbe2d7bb1012ba..a83ca8f0a2068f 100644 --- a/Lib/lib2to3/tests/data/crlf.py +++ b/Lib/lib2to3/tests/data/crlf.py @@ -1,3 +1,3 @@ -print "hi" - -print "Like bad Windows newlines?" +print "hi" + +print "Like bad Windows newlines?" diff --git a/Lib/lib2to3/tests/test_refactor.py b/Lib/lib2to3/tests/test_refactor.py index e9bae5e45d0136..487cb53537e763 100644 --- a/Lib/lib2to3/tests/test_refactor.py +++ b/Lib/lib2to3/tests/test_refactor.py @@ -206,6 +206,23 @@ def read_file(): self.assertNotEqual(old_contents, new_contents) return new_contents + def refactor_file(self, test_file, fixers=_2TO3_FIXERS): + tmpdir = tempfile.mkdtemp(prefix="2to3-test_refactor") + self.addCleanup(shutil.rmtree, tmpdir) + shutil.copy(test_file, tmpdir) + test_file = os.path.join(tmpdir, os.path.basename(test_file)) + + def read_file(): + with open(test_file, "rb") as fp: + return fp.read() + + old_contents = read_file() + rt = self.rt(fixers=fixers) + + rt.refactor_file(test_file, True) + new_contents = read_file() + return old_contents, new_contents + def test_refactor_file(self): test_file = os.path.join(FIXER_DIR, "parrot_example.py") self.check_file_refactoring(test_file, _DEFAULT_FIXERS) @@ -285,6 +302,17 @@ def test_crlf_newlines(self): finally: os.linesep = old_sep + def test_crlf_unchanged(self): + old_sep = os.linesep + os.linesep = "\n" + try: + fn = os.path.join(TEST_DATA_DIR, "crlf.py") + old, new = self.refactor_file(fn) + self.assertIn(b"\r\n", old) + self.assertIn(b"\r\n", new) + finally: + os.linesep = old_sep + def test_refactor_docstring(self): rt = self.rt() From d15e0acdf5cfae90100e4f039edf47bb14155d28 Mon Sep 17 00:00:00 2001 From: Aaron Ang Date: Sun, 15 Apr 2018 19:35:31 -0700 Subject: [PATCH 2/7] bpo-11594: Ensure line-endings are respected when using lib2to3 --- Lib/lib2to3/refactor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/lib2to3/refactor.py b/Lib/lib2to3/refactor.py index db2e38d22f16f0..7c4e0649975cec 100644 --- a/Lib/lib2to3/refactor.py +++ b/Lib/lib2to3/refactor.py @@ -314,7 +314,7 @@ def _read_python_source(self, filename): encoding = tokenize.detect_encoding(f.readline)[0] finally: f.close() - with io.open(filename, "r", encoding=encoding) as f: + with io.open(filename, "r", encoding=encoding, newline='') as f: return f.read(), encoding def refactor_file(self, filename, write=False, doctests_only=False): From 940dad172e52f5e418f4525a92e018dac1b8ca28 Mon Sep 17 00:00:00 2001 From: Aaron Ang Date: Sun, 15 Apr 2018 19:39:09 -0700 Subject: [PATCH 3/7] bpo-11594: Simplify test that ensures line-endings are respected --- Lib/lib2to3/tests/test_refactor.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/Lib/lib2to3/tests/test_refactor.py b/Lib/lib2to3/tests/test_refactor.py index 487cb53537e763..6b4a5d9f1cd32f 100644 --- a/Lib/lib2to3/tests/test_refactor.py +++ b/Lib/lib2to3/tests/test_refactor.py @@ -303,15 +303,10 @@ def test_crlf_newlines(self): os.linesep = old_sep def test_crlf_unchanged(self): - old_sep = os.linesep - os.linesep = "\n" - try: - fn = os.path.join(TEST_DATA_DIR, "crlf.py") - old, new = self.refactor_file(fn) - self.assertIn(b"\r\n", old) - self.assertIn(b"\r\n", new) - finally: - os.linesep = old_sep + fn = os.path.join(TEST_DATA_DIR, "crlf.py") + old, new = self.refactor_file(fn) + self.assertIn(b"\r\n", old) + self.assertIn(b"\r\n", new) def test_refactor_docstring(self): rt = self.rt() From 853bd6e6b398234832fa1ca0d11ebc95549d8a82 Mon Sep 17 00:00:00 2001 From: Aaron Ang Date: Sun, 15 Apr 2018 19:40:00 -0700 Subject: [PATCH 4/7] bpo-11594: Ensure test file is writable --- Lib/lib2to3/tests/test_refactor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/lib2to3/tests/test_refactor.py b/Lib/lib2to3/tests/test_refactor.py index 6b4a5d9f1cd32f..705836c709f28b 100644 --- a/Lib/lib2to3/tests/test_refactor.py +++ b/Lib/lib2to3/tests/test_refactor.py @@ -211,6 +211,7 @@ def refactor_file(self, test_file, fixers=_2TO3_FIXERS): self.addCleanup(shutil.rmtree, tmpdir) shutil.copy(test_file, tmpdir) test_file = os.path.join(tmpdir, os.path.basename(test_file)) + os.chmod(test_file, 0o644) def read_file(): with open(test_file, "rb") as fp: From f7be6c6c9beb502b597f983857979328cb3522f1 Mon Sep 17 00:00:00 2001 From: Aaron Ang Date: Sun, 15 Apr 2018 19:42:40 -0700 Subject: [PATCH 5/7] bpo-11594: Remove unnecessary blank line --- Lib/lib2to3/tests/test_refactor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/lib2to3/tests/test_refactor.py b/Lib/lib2to3/tests/test_refactor.py index 705836c709f28b..34f2094e4e12de 100644 --- a/Lib/lib2to3/tests/test_refactor.py +++ b/Lib/lib2to3/tests/test_refactor.py @@ -219,7 +219,6 @@ def read_file(): old_contents = read_file() rt = self.rt(fixers=fixers) - rt.refactor_file(test_file, True) new_contents = read_file() return old_contents, new_contents From 505ee8d84dfd728b94cbbc0467d8eb68fbb91003 Mon Sep 17 00:00:00 2001 From: Aaron Ang Date: Mon, 16 Apr 2018 08:43:12 -0700 Subject: [PATCH 6/7] bpo-11594: Add news file --- .../NEWS.d/next/Library/2018-04-16-08-42-03.bpo-11594.QLo4vv.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2018-04-16-08-42-03.bpo-11594.QLo4vv.rst diff --git a/Misc/NEWS.d/next/Library/2018-04-16-08-42-03.bpo-11594.QLo4vv.rst b/Misc/NEWS.d/next/Library/2018-04-16-08-42-03.bpo-11594.QLo4vv.rst new file mode 100644 index 00000000000000..be2abf6c34dd69 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-04-16-08-42-03.bpo-11594.QLo4vv.rst @@ -0,0 +1 @@ +Ensure line-endings are respected when using lib2to3. From 56b3d358702f3b95231f02d33ee5ba1d9631e27f Mon Sep 17 00:00:00 2001 From: Aaron Ang Date: Mon, 16 Apr 2018 20:58:22 -0700 Subject: [PATCH 7/7] bpo-11594: Apply DRY principle --- Lib/lib2to3/tests/test_refactor.py | 33 ++++++++++++------------------ 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/Lib/lib2to3/tests/test_refactor.py b/Lib/lib2to3/tests/test_refactor.py index 34f2094e4e12de..f3059a93113b1c 100644 --- a/Lib/lib2to3/tests/test_refactor.py +++ b/Lib/lib2to3/tests/test_refactor.py @@ -180,47 +180,40 @@ def print_output(self, old_text, new_text, filename, equal): def check_file_refactoring(self, test_file, fixers=_2TO3_FIXERS, options=None, mock_log_debug=None, actually_write=True): - tmpdir = tempfile.mkdtemp(prefix="2to3-test_refactor") - self.addCleanup(shutil.rmtree, tmpdir) - # make a copy of the tested file that we can write to - shutil.copy(test_file, tmpdir) - test_file = os.path.join(tmpdir, os.path.basename(test_file)) - os.chmod(test_file, 0o644) - - def read_file(): - with open(test_file, "rb") as fp: - return fp.read() - - old_contents = read_file() + test_file = self.init_test_file(test_file) + old_contents = self.read_file(test_file) rt = self.rt(fixers=fixers, options=options) if mock_log_debug: rt.log_debug = mock_log_debug rt.refactor_file(test_file) - self.assertEqual(old_contents, read_file()) + self.assertEqual(old_contents, self.read_file(test_file)) if not actually_write: return rt.refactor_file(test_file, True) - new_contents = read_file() + new_contents = self.read_file(test_file) self.assertNotEqual(old_contents, new_contents) return new_contents - def refactor_file(self, test_file, fixers=_2TO3_FIXERS): + def init_test_file(self, test_file): tmpdir = tempfile.mkdtemp(prefix="2to3-test_refactor") self.addCleanup(shutil.rmtree, tmpdir) shutil.copy(test_file, tmpdir) test_file = os.path.join(tmpdir, os.path.basename(test_file)) os.chmod(test_file, 0o644) + return test_file - def read_file(): - with open(test_file, "rb") as fp: - return fp.read() + def read_file(self, test_file): + with open(test_file, "rb") as fp: + return fp.read() - old_contents = read_file() + def refactor_file(self, test_file, fixers=_2TO3_FIXERS): + test_file = self.init_test_file(test_file) + old_contents = self.read_file(test_file) rt = self.rt(fixers=fixers) rt.refactor_file(test_file, True) - new_contents = read_file() + new_contents = self.read_file(test_file) return old_contents, new_contents def test_refactor_file(self):