Skip to content

Commit 3b3be1f

Browse files
bpo-11594: Ensure line-endings are respected when using 2to3 (GH-6483)
(cherry picked from commit c127a86) Co-authored-by: Aaron Ang <aaronang@users.noreply.github.com>
1 parent 902bb62 commit 3b3be1f

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

Lib/lib2to3/refactor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def _read_python_source(self, filename):
314314
encoding = tokenize.detect_encoding(f.readline)[0]
315315
finally:
316316
f.close()
317-
with io.open(filename, "r", encoding=encoding) as f:
317+
with io.open(filename, "r", encoding=encoding, newline='') as f:
318318
return f.read(), encoding
319319

320320
def refactor_file(self, filename, write=False, doctests_only=False):

Lib/lib2to3/tests/data/crlf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
print "hi"
2-
3-
print "Like bad Windows newlines?"
1+
print "hi"
2+
3+
print "Like bad Windows newlines?"

Lib/lib2to3/tests/test_refactor.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -180,32 +180,42 @@ def print_output(self, old_text, new_text, filename, equal):
180180
def check_file_refactoring(self, test_file, fixers=_2TO3_FIXERS,
181181
options=None, mock_log_debug=None,
182182
actually_write=True):
183-
tmpdir = tempfile.mkdtemp(prefix="2to3-test_refactor")
184-
self.addCleanup(shutil.rmtree, tmpdir)
185-
# make a copy of the tested file that we can write to
186-
shutil.copy(test_file, tmpdir)
187-
test_file = os.path.join(tmpdir, os.path.basename(test_file))
188-
os.chmod(test_file, 0o644)
189-
190-
def read_file():
191-
with open(test_file, "rb") as fp:
192-
return fp.read()
193-
194-
old_contents = read_file()
183+
test_file = self.init_test_file(test_file)
184+
old_contents = self.read_file(test_file)
195185
rt = self.rt(fixers=fixers, options=options)
196186
if mock_log_debug:
197187
rt.log_debug = mock_log_debug
198188

199189
rt.refactor_file(test_file)
200-
self.assertEqual(old_contents, read_file())
190+
self.assertEqual(old_contents, self.read_file(test_file))
201191

202192
if not actually_write:
203193
return
204194
rt.refactor_file(test_file, True)
205-
new_contents = read_file()
195+
new_contents = self.read_file(test_file)
206196
self.assertNotEqual(old_contents, new_contents)
207197
return new_contents
208198

199+
def init_test_file(self, test_file):
200+
tmpdir = tempfile.mkdtemp(prefix="2to3-test_refactor")
201+
self.addCleanup(shutil.rmtree, tmpdir)
202+
shutil.copy(test_file, tmpdir)
203+
test_file = os.path.join(tmpdir, os.path.basename(test_file))
204+
os.chmod(test_file, 0o644)
205+
return test_file
206+
207+
def read_file(self, test_file):
208+
with open(test_file, "rb") as fp:
209+
return fp.read()
210+
211+
def refactor_file(self, test_file, fixers=_2TO3_FIXERS):
212+
test_file = self.init_test_file(test_file)
213+
old_contents = self.read_file(test_file)
214+
rt = self.rt(fixers=fixers)
215+
rt.refactor_file(test_file, True)
216+
new_contents = self.read_file(test_file)
217+
return old_contents, new_contents
218+
209219
def test_refactor_file(self):
210220
test_file = os.path.join(FIXER_DIR, "parrot_example.py")
211221
self.check_file_refactoring(test_file, _DEFAULT_FIXERS)
@@ -285,6 +295,12 @@ def test_crlf_newlines(self):
285295
finally:
286296
os.linesep = old_sep
287297

298+
def test_crlf_unchanged(self):
299+
fn = os.path.join(TEST_DATA_DIR, "crlf.py")
300+
old, new = self.refactor_file(fn)
301+
self.assertIn(b"\r\n", old)
302+
self.assertIn(b"\r\n", new)
303+
288304
def test_refactor_docstring(self):
289305
rt = self.rt()
290306

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure line-endings are respected when using lib2to3.

0 commit comments

Comments
 (0)