GeezerVison is showing github.com — bpo-43651: PEP 597: Fix EncodingWarning in some tests (GH-25142) · python/cpython@8bbfeb3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Lib/test/test__xxsubinterpreters.py +6-6Lines changed: 6 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -25,11 +25,11 @@ def _captured_script(script): | 25 | 25 | indented = script.replace('\n', '\n ') | 26 | 26 | wrapped = dedent(f""" | 27 | 27 | import contextlib | 28 | | - with open({w}, 'w') as spipe: | | 28 | + with open({w}, 'w', encoding="utf-8") as spipe: | 29 | 29 | with contextlib.redirect_stdout(spipe): | 30 | 30 | {indented} | 31 | 31 | """) | 32 | | - return wrapped, open(r) | | 32 | + return wrapped, open(r, encoding="utf-8") | 33 | 33 |
| 34 | 34 |
| 35 | 35 | def _run_output(interp, request, shared=None): | @@ -45,7 +45,7 @@ def _running(interp): | 45 | 45 | def run(): | 46 | 46 | interpreters.run_string(interp, dedent(f""" | 47 | 47 | # wait for "signal" | 48 | | - with open({r}) as rpipe: | | 48 | + with open({r}, encoding="utf-8") as rpipe: | 49 | 49 | rpipe.read() | 50 | 50 | """)) | 51 | 51 |
| @@ -54,7 +54,7 @@ def run(): | 54 | 54 |
| 55 | 55 | yield | 56 | 56 |
| 57 | | - with open(w, 'w') as spipe: | | 57 | + with open(w, 'w', encoding="utf-8") as spipe: | 58 | 58 | spipe.write('done') | 59 | 59 | t.join() | 60 | 60 |
| @@ -806,7 +806,7 @@ def f(): | 806 | 806 | @unittest.skipUnless(hasattr(os, 'fork'), "test needs os.fork()") | 807 | 807 | def test_fork(self): | 808 | 808 | import tempfile | 809 | | - with tempfile.NamedTemporaryFile('w+') as file: | | 809 | + with tempfile.NamedTemporaryFile('w+', encoding="utf-8") as file: | 810 | 810 | file.write('') | 811 | 811 | file.flush() | 812 | 812 |
| @@ -816,7 +816,7 @@ def test_fork(self): | 816 | 816 | try: | 817 | 817 | os.fork() | 818 | 818 | except RuntimeError: | 819 | | - with open('{file.name}', 'w') as out: | | 819 | + with open('{file.name}', 'w', encoding='utf-8') as out: | 820 | 820 | out.write('{expected}') | 821 | 821 | """) | 822 | 822 | interpreters.run_string(self.id, script) | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_argparse.py +8-6Lines changed: 8 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,7 +45,7 @@ def tearDown(self): | 45 | 45 |
| 46 | 46 | def create_readonly_file(self, filename): | 47 | 47 | file_path = os.path.join(self.temp_dir, filename) | 48 | | - with open(file_path, 'w') as file: | | 48 | + with open(file_path, 'w', encoding="utf-8") as file: | 49 | 49 | file.write(filename) | 50 | 50 | os.chmod(file_path, stat.S_IREAD) | 51 | 51 |
| @@ -1468,7 +1468,7 @@ def setUp(self): | 1468 | 1468 | ('invalid', '@no-such-path\n'), | 1469 | 1469 | ] | 1470 | 1470 | for path, text in file_texts: | 1471 | | - with open(path, 'w') as file: | | 1471 | + with open(path, 'w', encoding="utf-8") as file: | 1472 | 1472 | file.write(text) | 1473 | 1473 |
| 1474 | 1474 | parser_signature = Sig(fromfile_prefix_chars='@') | @@ -1498,7 +1498,7 @@ def setUp(self): | 1498 | 1498 | ('hello', 'hello world!\n'), | 1499 | 1499 | ] | 1500 | 1500 | for path, text in file_texts: | 1501 | | - with open(path, 'w') as file: | | 1501 | + with open(path, 'w', encoding="utf-8") as file: | 1502 | 1502 | file.write(text) | 1503 | 1503 |
| 1504 | 1504 | class FromFileConverterArgumentParser(ErrorRaisingArgumentParser): | @@ -1580,7 +1580,8 @@ class TestFileTypeR(TempDirMixin, ParserTestCase): | 1580 | 1580 | def setUp(self): | 1581 | 1581 | super(TestFileTypeR, self).setUp() | 1582 | 1582 | for file_name in ['foo', 'bar']: | 1583 | | - with open(os.path.join(self.temp_dir, file_name), 'w') as file: | | 1583 | + with open(os.path.join(self.temp_dir, file_name), | | 1584 | + 'w', encoding="utf-8") as file: | 1584 | 1585 | file.write(file_name) | 1585 | 1586 | self.create_readonly_file('readonly') | 1586 | 1587 |
| @@ -1601,7 +1602,7 @@ class TestFileTypeDefaults(TempDirMixin, ParserTestCase): | 1601 | 1602 | """Test that a file is not created unless the default is needed""" | 1602 | 1603 | def setUp(self): | 1603 | 1604 | super(TestFileTypeDefaults, self).setUp() | 1604 | | - file = open(os.path.join(self.temp_dir, 'good'), 'w') | | 1605 | + file = open(os.path.join(self.temp_dir, 'good'), 'w', encoding="utf-8") | 1605 | 1606 | file.write('good') | 1606 | 1607 | file.close() | 1607 | 1608 |
| @@ -1620,7 +1621,8 @@ class TestFileTypeRB(TempDirMixin, ParserTestCase): | 1620 | 1621 | def setUp(self): | 1621 | 1622 | super(TestFileTypeRB, self).setUp() | 1622 | 1623 | for file_name in ['foo', 'bar']: | 1623 | | - with open(os.path.join(self.temp_dir, file_name), 'w') as file: | | 1624 | + with open(os.path.join(self.temp_dir, file_name), | | 1625 | + 'w', encoding="utf-8") as file: | 1624 | 1626 | file.write(file_name) | 1625 | 1627 |
| 1626 | 1628 | argument_signatures = [ | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_baseexception.py +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,8 +28,9 @@ def test_inheritance(self): | 28 | 28 | except TypeError: | 29 | 29 | pass | 30 | 30 |
| 31 | | - inheritance_tree = open(os.path.join(os.path.split(__file__)[0], | 32 | | - 'exception_hierarchy.txt')) | | 31 | + inheritance_tree = open( | | 32 | + os.path.join(os.path.split(__file__)[0], 'exception_hierarchy.txt'), | | 33 | + encoding="utf-8") | 33 | 34 | try: | 34 | 35 | superclass_name = inheritance_tree.readline().rstrip() | 35 | 36 | try: | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_bdb.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -539,7 +539,7 @@ def create_modules(modules): | 539 | 539 | try: | 540 | 540 | for m in modules: | 541 | 541 | fname = m + '.py' | 542 | | - with open(fname, 'w') as f: | | 542 | + with open(fname, 'w', encoding="utf-8") as f: | 543 | 543 | f.write(textwrap.dedent(modules[m])) | 544 | 544 | linecache.checkcache(fname) | 545 | 545 | importlib.invalidate_caches() | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_bool.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -235,7 +235,7 @@ def test_boolean(self): | 235 | 235 |
| 236 | 236 | def test_fileclosed(self): | 237 | 237 | try: | 238 | | - with open(os_helper.TESTFN, "w") as f: | | 238 | + with open(os_helper.TESTFN, "w", encoding="utf-8") as f: | 239 | 239 | self.assertIs(f.closed, False) | 240 | 240 | self.assertIs(f.closed, True) | 241 | 241 | finally: | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_builtin.py +8-6Lines changed: 8 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1159,7 +1159,7 @@ def test_oct(self): | 1159 | 1159 |
| 1160 | 1160 | def write_testfile(self): | 1161 | 1161 | # NB the first 4 lines are also used to test input, below | 1162 | | - fp = open(TESTFN, 'w') | | 1162 | + fp = open(TESTFN, 'w', encoding="utf-8") | 1163 | 1163 | self.addCleanup(unlink, TESTFN) | 1164 | 1164 | with fp: | 1165 | 1165 | fp.write('1+1\n') | @@ -1171,7 +1171,7 @@ def write_testfile(self): | 1171 | 1171 |
| 1172 | 1172 | def test_open(self): | 1173 | 1173 | self.write_testfile() | 1174 | | - fp = open(TESTFN, 'r') | | 1174 | + fp = open(TESTFN, encoding="utf-8") | 1175 | 1175 | with fp: | 1176 | 1176 | self.assertEqual(fp.readline(4), '1+1\n') | 1177 | 1177 | self.assertEqual(fp.readline(), 'The quick brown fox jumps over the lazy dog.\n') | @@ -1197,15 +1197,17 @@ def test_open_default_encoding(self): | 1197 | 1197 |
| 1198 | 1198 | self.write_testfile() | 1199 | 1199 | current_locale_encoding = locale.getpreferredencoding(False) | 1200 | | - fp = open(TESTFN, 'w') | | 1200 | + with warnings.catch_warnings(): | | 1201 | + warnings.simplefilter("ignore", EncodingWarning) | | 1202 | + fp = open(TESTFN, 'w') | 1201 | 1203 | with fp: | 1202 | 1204 | self.assertEqual(fp.encoding, current_locale_encoding) | 1203 | 1205 | finally: | 1204 | 1206 | os.environ.clear() | 1205 | 1207 | os.environ.update(old_environ) | 1206 | 1208 |
| 1207 | 1209 | def test_open_non_inheritable(self): | 1208 | | - fileobj = open(__file__) | | 1210 | + fileobj = open(__file__, encoding="utf-8") | 1209 | 1211 | with fileobj: | 1210 | 1212 | self.assertFalse(os.get_inheritable(fileobj.fileno())) | 1211 | 1213 |
| @@ -1300,7 +1302,7 @@ def test_pow(self): | 1300 | 1302 |
| 1301 | 1303 | def test_input(self): | 1302 | 1304 | self.write_testfile() | 1303 | | - fp = open(TESTFN, 'r') | | 1305 | + fp = open(TESTFN, encoding="utf-8") | 1304 | 1306 | savestdin = sys.stdin | 1305 | 1307 | savestdout = sys.stdout # Eats the echo | 1306 | 1308 | try: | @@ -2022,7 +2024,7 @@ def _run_child(self, child, terminal_input): | 2022 | 2024 | os.write(fd, terminal_input) | 2023 | 2025 |
| 2024 | 2026 | # Get results from the pipe | 2025 | | - with open(r, "r") as rpipe: | | 2027 | + with open(r, encoding="utf-8") as rpipe: | 2026 | 2028 | lines = [] | 2027 | 2029 | while True: | 2028 | 2030 | line = rpipe.readline().strip() | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_doctest.py +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2846,7 +2846,7 @@ def test_CLI(): r""" | 2846 | 2846 | >>> from test.support.os_helper import temp_dir | 2847 | 2847 | >>> with temp_dir() as tmpdir: | 2848 | 2848 | ... fn = os.path.join(tmpdir, 'myfile.doc') | 2849 | | - ... with open(fn, 'w') as f: | | 2849 | + ... with open(fn, 'w', encoding='utf-8') as f: | 2850 | 2850 | ... _ = f.write('This is a very simple test file.\n') | 2851 | 2851 | ... _ = f.write(' >>> 1 + 1\n') | 2852 | 2852 | ... _ = f.write(' 2\n') | @@ -2898,7 +2898,7 @@ def test_CLI(): r""" | 2898 | 2898 | >>> from test.support.os_helper import temp_dir | 2899 | 2899 | >>> with temp_dir() as tmpdir: | 2900 | 2900 | ... fn = os.path.join(tmpdir, 'myfile.doc') | 2901 | | - ... with open(fn, 'w') as f: | | 2901 | + ... with open(fn, 'w', encoding="utf-8") as f: | 2902 | 2902 | ... _ = f.write('This is another simple test file.\n') | 2903 | 2903 | ... _ = f.write(' >>> 1 + 1\n') | 2904 | 2904 | ... _ = f.write(' 2\n') | @@ -2909,7 +2909,7 @@ def test_CLI(): r""" | 2909 | 2909 | ... _ = f.write('\n') | 2910 | 2910 | ... _ = f.write('And that is it.\n') | 2911 | 2911 | ... fn2 = os.path.join(tmpdir, 'myfile2.py') | 2912 | | - ... with open(fn2, 'w') as f: | | 2912 | + ... with open(fn2, 'w', encoding='utf-8') as f: | 2913 | 2913 | ... _ = f.write('def test_func():\n') | 2914 | 2914 | ... _ = f.write(' \"\"\"\n') | 2915 | 2915 | ... _ = f.write(' This is simple python test function.\n') | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_exceptions.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,9 +54,9 @@ def testRaising(self): | 54 | 54 | self.assertRaises(AttributeError, getattr, sys, "undefined_attribute") | 55 | 55 |
| 56 | 56 | self.raise_catch(EOFError, "EOFError") | 57 | | - fp = open(TESTFN, 'w') | | 57 | + fp = open(TESTFN, 'w', encoding="utf-8") | 58 | 58 | fp.close() | 59 | | - fp = open(TESTFN, 'r') | | 59 | + fp = open(TESTFN, 'r', encoding="utf-8") | 60 | 60 | savestdin = sys.stdin | 61 | 61 | try: | 62 | 62 | try: | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_opcodes.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,7 +24,7 @@ def test_try_inside_for_loop(self): | 24 | 24 | def test_setup_annotations_line(self): | 25 | 25 | # check that SETUP_ANNOTATIONS does not create spurious line numbers | 26 | 26 | try: | 27 | | - with open(ann_module.__file__) as f: | | 27 | + with open(ann_module.__file__, encoding="utf-8") as f: | 28 | 28 | txt = f.read() | 29 | 29 | co = compile(txt, ann_module.__file__, 'exec') | 30 | 30 | self.assertEqual(co.co_firstlineno, 1) | |
Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_support.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -47,7 +47,7 @@ def test_unload(self): | 47 | 47 | self.assertNotIn("sched", sys.modules) | 48 | 48 |
| 49 | 49 | def test_unlink(self): | 50 | | - with open(TESTFN, "w") as f: | | 50 | + with open(TESTFN, "w", encoding="utf-8") as f: | 51 | 51 | pass | 52 | 52 | os_helper.unlink(TESTFN) | 53 | 53 | self.assertFalse(os.path.exists(TESTFN)) | @@ -79,7 +79,7 @@ def test_rmtree(self): | 79 | 79 |
| 80 | 80 | def test_forget(self): | 81 | 81 | mod_filename = TESTFN + '.py' | 82 | | - with open(mod_filename, 'w') as f: | | 82 | + with open(mod_filename, 'w', encoding="utf-8") as f: | 83 | 83 | print('foo = 1', file=f) | 84 | 84 | sys.path.insert(0, os.curdir) | 85 | 85 | importlib.invalidate_caches() | |
|