Skip to content

Commit 60d27f4

Browse files
authored
[3.5] bpo-30109: Fix reindent.py (GH-1207) (GH-1209)
Skip the file if it has bad encoding. (cherry picked from commit 58f3c9d)
1 parent ad2d47d commit 60d27f4

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

Lib/test/test_tools/test_reindent.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import unittest
99
from test.support.script_helper import assert_python_ok
10+
from test.support import findfile
1011

1112
from test.test_tools import scriptsdir, skip_if_missing
1213

@@ -23,6 +24,12 @@ def test_help(self):
2324
self.assertEqual(out, b'')
2425
self.assertGreater(err, b'')
2526

27+
def test_reindent_file_with_bad_encoding(self):
28+
bad_coding_path = findfile('bad_coding.py')
29+
rc, out, err = assert_python_ok(self.script, '-r', bad_coding_path)
30+
self.assertEqual(out, b'')
31+
self.assertNotEqual(err, b'')
32+
2633

2734
if __name__ == '__main__':
2835
unittest.main()

Tools/scripts/reindent.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ def check(file):
118118
if verbose:
119119
print("checking", file, "...", end=' ')
120120
with open(file, 'rb') as f:
121-
encoding, _ = tokenize.detect_encoding(f.readline)
121+
try:
122+
encoding, _ = tokenize.detect_encoding(f.readline)
123+
except SyntaxError as se:
124+
errprint("%s: SyntaxError: %s" % (file, str(se)))
125+
return
122126
try:
123127
with open(file, encoding=encoding) as f:
124128
r = Reindenter(f)

0 commit comments

Comments
 (0)