]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-96189: Fix test_invalid_utf8 on a number of build bots (GH-96190)
authorMichael Droettboom <mdboom@gmail.com>
Tue, 23 Aug 2022 20:52:55 +0000 (16:52 -0400)
committerGitHub <noreply@github.com>
Tue, 23 Aug 2022 20:52:55 +0000 (13:52 -0700)
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

Lib/test/test_source_encoding.py

index e1b0de2adef621567d2b25cabf49efc0f0f07f15..8d7b573c7e915b9dbf039d5f1faad73b8622fc2a 100644 (file)
@@ -239,50 +239,50 @@ class UTF8ValidatorTest(unittest.TestCase):
         # it's an otherwise valid Python source file.
         template = b'"%s"\n'
 
-        with tempfile.TemporaryDirectory() as tmpd:
-            fn = os.path.join(tmpd, 'test.py')
+        fn = TESTFN
+        self.addCleanup(unlink, fn)
+
+        def check(content):
+            with open(fn, 'wb') as fp:
+                fp.write(template % content)
+            script_helper.assert_python_failure(fn)
+
+        # continuation bytes in a sequence of 2, 3, or 4 bytes
+        continuation_bytes = [bytes([x]) for x in range(0x80, 0xC0)]
+        # start bytes of a 2-byte sequence equivalent to code points < 0x7F
+        invalid_2B_seq_start_bytes = [bytes([x]) for x in range(0xC0, 0xC2)]
+        # start bytes of a 4-byte sequence equivalent to code points > 0x10FFFF
+        invalid_4B_seq_start_bytes = [bytes([x]) for x in range(0xF5, 0xF8)]
+        invalid_start_bytes = (
+            continuation_bytes + invalid_2B_seq_start_bytes +
+            invalid_4B_seq_start_bytes + [bytes([x]) for x in range(0xF7, 0x100)]
+        )
 
-            def check(content):
-                with open(fn, 'wb') as fp:
-                    fp.write(template % content)
-                script_helper.assert_python_failure(fn)
-
-            # continuation bytes in a sequence of 2, 3, or 4 bytes
-            continuation_bytes = [bytes([x]) for x in range(0x80, 0xC0)]
-            # start bytes of a 2-byte sequence equivalent to code points < 0x7F
-            invalid_2B_seq_start_bytes = [bytes([x]) for x in range(0xC0, 0xC2)]
-            # start bytes of a 4-byte sequence equivalent to code points > 0x10FFFF
-            invalid_4B_seq_start_bytes = [bytes([x]) for x in range(0xF5, 0xF8)]
-            invalid_start_bytes = (
-                continuation_bytes + invalid_2B_seq_start_bytes +
-                invalid_4B_seq_start_bytes + [bytes([x]) for x in range(0xF7, 0x100)]
-            )
-
-            for byte in invalid_start_bytes:
-                check(byte)
-
-            for sb in invalid_2B_seq_start_bytes:
-                for cb in continuation_bytes:
-                    check(sb + cb)
-
-            for sb in invalid_4B_seq_start_bytes:
-                for cb1 in continuation_bytes[:3]:
-                    for cb3 in continuation_bytes[:3]:
-                        check(sb+cb1+b'\x80'+cb3)
-
-            for cb in [bytes([x]) for x in range(0x80, 0xA0)]:
-                check(b'\xE0'+cb+b'\x80')
-                check(b'\xE0'+cb+b'\xBF')
-                # surrogates
-            for cb in [bytes([x]) for x in range(0xA0, 0xC0)]:
-                check(b'\xED'+cb+b'\x80')
-                check(b'\xED'+cb+b'\xBF')
-            for cb in [bytes([x]) for x in range(0x80, 0x90)]:
-                check(b'\xF0'+cb+b'\x80\x80')
-                check(b'\xF0'+cb+b'\xBF\xBF')
-            for cb in [bytes([x]) for x in range(0x90, 0xC0)]:
-                check(b'\xF4'+cb+b'\x80\x80')
-                check(b'\xF4'+cb+b'\xBF\xBF')
+        for byte in invalid_start_bytes:
+            check(byte)
+
+        for sb in invalid_2B_seq_start_bytes:
+            for cb in continuation_bytes:
+                check(sb + cb)
+
+        for sb in invalid_4B_seq_start_bytes:
+            for cb1 in continuation_bytes[:3]:
+                for cb3 in continuation_bytes[:3]:
+                    check(sb+cb1+b'\x80'+cb3)
+
+        for cb in [bytes([x]) for x in range(0x80, 0xA0)]:
+            check(b'\xE0'+cb+b'\x80')
+            check(b'\xE0'+cb+b'\xBF')
+            # surrogates
+        for cb in [bytes([x]) for x in range(0xA0, 0xC0)]:
+            check(b'\xED'+cb+b'\x80')
+            check(b'\xED'+cb+b'\xBF')
+        for cb in [bytes([x]) for x in range(0x80, 0x90)]:
+            check(b'\xF0'+cb+b'\x80\x80')
+            check(b'\xF0'+cb+b'\xBF\xBF')
+        for cb in [bytes([x]) for x in range(0x90, 0xC0)]:
+            check(b'\xF4'+cb+b'\x80\x80')
+            check(b'\xF4'+cb+b'\xBF\xBF')
 
 
 class BytesSourceEncodingTest(AbstractSourceEncodingTest, unittest.TestCase):