Skip to content

Commit 054328f

Browse files
authored
gh-96189: Fix test_invalid_utf8 on a number of build bots (GH-96190)
The clearing of the temporary directory is not working on some platforms and leaving behind files. This has been updated to use the pattern in test_cmd_line.py [1] using the special TESTFN rather than a test directory. [1] https://github.com/python/cpython/blob/main/Lib/test/test_cmd_line.py#L559
1 parent e046cf8 commit 054328f

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

Lib/test/test_source_encoding.py

+43-43
Original file line numberDiff line numberDiff line change
@@ -239,50 +239,50 @@ def test_invalid_utf8(self):
239239
# it's an otherwise valid Python source file.
240240
template = b'"%s"\n'
241241

242-
with tempfile.TemporaryDirectory() as tmpd:
243-
fn = os.path.join(tmpd, 'test.py')
242+
fn = TESTFN
243+
self.addCleanup(unlink, fn)
244+
245+
def check(content):
246+
with open(fn, 'wb') as fp:
247+
fp.write(template % content)
248+
script_helper.assert_python_failure(fn)
249+
250+
# continuation bytes in a sequence of 2, 3, or 4 bytes
251+
continuation_bytes = [bytes([x]) for x in range(0x80, 0xC0)]
252+
# start bytes of a 2-byte sequence equivalent to code points < 0x7F
253+
invalid_2B_seq_start_bytes = [bytes([x]) for x in range(0xC0, 0xC2)]
254+
# start bytes of a 4-byte sequence equivalent to code points > 0x10FFFF
255+
invalid_4B_seq_start_bytes = [bytes([x]) for x in range(0xF5, 0xF8)]
256+
invalid_start_bytes = (
257+
continuation_bytes + invalid_2B_seq_start_bytes +
258+
invalid_4B_seq_start_bytes + [bytes([x]) for x in range(0xF7, 0x100)]
259+
)
244260

245-
def check(content):
246-
with open(fn, 'wb') as fp:
247-
fp.write(template % content)
248-
script_helper.assert_python_failure(fn)
249-
250-
# continuation bytes in a sequence of 2, 3, or 4 bytes
251-
continuation_bytes = [bytes([x]) for x in range(0x80, 0xC0)]
252-
# start bytes of a 2-byte sequence equivalent to code points < 0x7F
253-
invalid_2B_seq_start_bytes = [bytes([x]) for x in range(0xC0, 0xC2)]
254-
# start bytes of a 4-byte sequence equivalent to code points > 0x10FFFF
255-
invalid_4B_seq_start_bytes = [bytes([x]) for x in range(0xF5, 0xF8)]
256-
invalid_start_bytes = (
257-
continuation_bytes + invalid_2B_seq_start_bytes +
258-
invalid_4B_seq_start_bytes + [bytes([x]) for x in range(0xF7, 0x100)]
259-
)
260-
261-
for byte in invalid_start_bytes:
262-
check(byte)
263-
264-
for sb in invalid_2B_seq_start_bytes:
265-
for cb in continuation_bytes:
266-
check(sb + cb)
267-
268-
for sb in invalid_4B_seq_start_bytes:
269-
for cb1 in continuation_bytes[:3]:
270-
for cb3 in continuation_bytes[:3]:
271-
check(sb+cb1+b'\x80'+cb3)
272-
273-
for cb in [bytes([x]) for x in range(0x80, 0xA0)]:
274-
check(b'\xE0'+cb+b'\x80')
275-
check(b'\xE0'+cb+b'\xBF')
276-
# surrogates
277-
for cb in [bytes([x]) for x in range(0xA0, 0xC0)]:
278-
check(b'\xED'+cb+b'\x80')
279-
check(b'\xED'+cb+b'\xBF')
280-
for cb in [bytes([x]) for x in range(0x80, 0x90)]:
281-
check(b'\xF0'+cb+b'\x80\x80')
282-
check(b'\xF0'+cb+b'\xBF\xBF')
283-
for cb in [bytes([x]) for x in range(0x90, 0xC0)]:
284-
check(b'\xF4'+cb+b'\x80\x80')
285-
check(b'\xF4'+cb+b'\xBF\xBF')
261+
for byte in invalid_start_bytes:
262+
check(byte)
263+
264+
for sb in invalid_2B_seq_start_bytes:
265+
for cb in continuation_bytes:
266+
check(sb + cb)
267+
268+
for sb in invalid_4B_seq_start_bytes:
269+
for cb1 in continuation_bytes[:3]:
270+
for cb3 in continuation_bytes[:3]:
271+
check(sb+cb1+b'\x80'+cb3)
272+
273+
for cb in [bytes([x]) for x in range(0x80, 0xA0)]:
274+
check(b'\xE0'+cb+b'\x80')
275+
check(b'\xE0'+cb+b'\xBF')
276+
# surrogates
277+
for cb in [bytes([x]) for x in range(0xA0, 0xC0)]:
278+
check(b'\xED'+cb+b'\x80')
279+
check(b'\xED'+cb+b'\xBF')
280+
for cb in [bytes([x]) for x in range(0x80, 0x90)]:
281+
check(b'\xF0'+cb+b'\x80\x80')
282+
check(b'\xF0'+cb+b'\xBF\xBF')
283+
for cb in [bytes([x]) for x in range(0x90, 0xC0)]:
284+
check(b'\xF4'+cb+b'\x80\x80')
285+
check(b'\xF4'+cb+b'\xBF\xBF')
286286

287287

288288
class BytesSourceEncodingTest(AbstractSourceEncodingTest, unittest.TestCase):

0 commit comments

Comments
 (0)