diff --git a/pyfakefs/fake_pathlib.py b/pyfakefs/fake_pathlib.py index c595b7fd..3e815dad 100644 --- a/pyfakefs/fake_pathlib.py +++ b/pyfakefs/fake_pathlib.py @@ -749,7 +749,7 @@ def touch(self, mode=0o666, exist_ok=True): else: self.filesystem.raise_os_error(errno.EEXIST, self._path()) else: - fake_file = self.open("w") + fake_file = self.open("w", encoding="utf8") fake_file.close() self.chmod(mode) diff --git a/pyfakefs/tests/dynamic_patch_test.py b/pyfakefs/tests/dynamic_patch_test.py index 8acf7464..3b3d10b7 100644 --- a/pyfakefs/tests/dynamic_patch_test.py +++ b/pyfakefs/tests/dynamic_patch_test.py @@ -60,7 +60,7 @@ def test_shutil_patch(self): def test_pathlib_path_patch(self): file_path = "test.txt" path = pathlib.Path(file_path) - with path.open("w") as f: + with path.open("w", encoding="utf8") as f: f.write("test") self.assertTrue(self.fs.exists(file_path)) diff --git a/pyfakefs/tests/example.py b/pyfakefs/tests/example.py index 4ab9921e..e3e03e14 100644 --- a/pyfakefs/tests/example.py +++ b/pyfakefs/tests/example.py @@ -61,12 +61,12 @@ def create_file(path): >>> create_file('/test/file.txt') >>> os.path.exists('/test/file.txt') True - >>> with open('/test/file.txt') as f: + >>> with open('/test/file.txt', encoding='utf8') as f: ... f.readlines() ["This is test file '/test/file.txt'.\\n", \ 'It was created using open().\\n'] """ - with open(path, "w") as f: + with open(path, "w", encoding="utf8") as f: f.write("This is test file '{0}'.\n".format(path)) f.write("It was created using open().\n") diff --git a/pyfakefs/tests/fake_filesystem_shutil_test.py b/pyfakefs/tests/fake_filesystem_shutil_test.py index 5516415d..6dd106a5 100644 --- a/pyfakefs/tests/fake_filesystem_shutil_test.py +++ b/pyfakefs/tests/fake_filesystem_shutil_test.py @@ -138,7 +138,7 @@ def test_rmtree_with_open_file_posix(self): self.create_file(os.path.join(dir_path, "bar")) file_path = os.path.join(dir_path, "baz") self.create_file(file_path) - with open(file_path): + with open(file_path, encoding="utf8"): shutil.rmtree(dir_path) self.assertFalse(os.path.exists(file_path)) @@ -149,7 +149,7 @@ def test_rmtree_with_open_file_fails_under_windows(self): self.create_file(os.path.join(dir_path, "bar")) file_path = os.path.join(dir_path, "baz") self.create_file(file_path) - with open(file_path): + with open(file_path, encoding="utf8"): with self.assertRaises(OSError): shutil.rmtree(dir_path) self.assertTrue(os.path.exists(dir_path)) diff --git a/pyfakefs/tests/fake_filesystem_test.py b/pyfakefs/tests/fake_filesystem_test.py index 917725fc..ad59812e 100644 --- a/pyfakefs/tests/fake_filesystem_test.py +++ b/pyfakefs/tests/fake_filesystem_test.py @@ -605,7 +605,7 @@ def test_empty_file_created_for_none_contents(self): fake_open = fake_filesystem.FakeFileOpen(self.filesystem) path = "foo/bar/baz" self.filesystem.create_file(path, contents=None) - with fake_open(path) as f: + with fake_open(path, encoding="utf8") as f: self.assertEqual("", f.read()) def test_create_file_with_incorrect_mode_type(self): @@ -1649,7 +1649,7 @@ def test_disk_usage_on_file_creation(self): self.fs.add_mount_point("!mount", total_size) def create_too_large_file(): - with self.open("!mount!file", "w") as dest: + with self.open("!mount!file", "w", encoding="utf8") as dest: dest.write("a" * (total_size + 1)) with self.assertRaises(OSError): @@ -1657,7 +1657,7 @@ def create_too_large_file(): self.assertEqual(0, self.fs.get_disk_usage("!mount").used) - with self.open("!mount!file", "w") as dest: + with self.open("!mount!file", "w", encoding="utf8") as dest: dest.write("a" * total_size) self.assertEqual(total_size, self.fs.get_disk_usage("!mount").used) @@ -1882,50 +1882,50 @@ def test_copying_preserves_byte_contents(self): self.assertEqual(dest_file.contents, source_file.contents) def test_diskusage_after_open_write(self): - with self.open("bar.txt", "w") as f: + with self.open("bar.txt", "w", encoding="utf8") as f: f.write("a" * 60) f.flush() self.assertEqual(60, self.fs.get_disk_usage()[1]) def test_disk_full_after_reopened(self): - with self.open("bar.txt", "w") as f: + with self.open("bar.txt", "w", encoding="utf8") as f: f.write("a" * 60) - with self.open("bar.txt") as f: + with self.open("bar.txt", encoding="utf8") as f: self.assertEqual("a" * 60, f.read()) with self.raises_os_error(errno.ENOSPC): - with self.open("bar.txt", "w") as f: + with self.open("bar.txt", "w", encoding="utf8") as f: f.write("b" * 110) with self.raises_os_error(errno.ENOSPC): f.flush() - with self.open("bar.txt") as f: + with self.open("bar.txt", encoding="utf8") as f: self.assertEqual("", f.read()) def test_disk_full_append(self): file_path = "bar.txt" - with self.open(file_path, "w") as f: + with self.open(file_path, "w", encoding="utf8") as f: f.write("a" * 60) - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: self.assertEqual("a" * 60, f.read()) with self.raises_os_error(errno.ENOSPC): - with self.open(file_path, "a") as f: + with self.open(file_path, "a", encoding="utf8") as f: f.write("b" * 41) with self.raises_os_error(errno.ENOSPC): f.flush() - with self.open("bar.txt") as f: + with self.open("bar.txt", encoding="utf8") as f: self.assertEqual(f.read(), "a" * 60) def test_disk_full_after_reopened_rplus_seek(self): - with self.open("bar.txt", "w") as f: + with self.open("bar.txt", "w", encoding="utf8") as f: f.write("a" * 60) - with self.open("bar.txt") as f: + with self.open("bar.txt", encoding="utf8") as f: self.assertEqual(f.read(), "a" * 60) with self.raises_os_error(errno.ENOSPC): - with self.open("bar.txt", "r+") as f: + with self.open("bar.txt", "r+", encoding="utf8") as f: f.seek(50) f.write("b" * 60) with self.raises_os_error(errno.ENOSPC): f.flush() - with self.open("bar.txt") as f: + with self.open("bar.txt", encoding="utf8") as f: self.assertEqual(f.read(), "a" * 60) @@ -2055,11 +2055,13 @@ def create_real_paths(self): for dir_name in ("foo", "bar"): real_dir = os.path.join(real_dir_root, dir_name) os.makedirs(real_dir, exist_ok=True) - with open(os.path.join(real_dir, "test.txt"), "w") as f: + with open( + os.path.join(real_dir, "test.txt"), "w", encoding="utf8" + ) as f: f.write("test") sub_dir = os.path.join(real_dir, "sub") os.makedirs(sub_dir, exist_ok=True) - with open(os.path.join(sub_dir, "sub.txt"), "w") as f: + with open(os.path.join(sub_dir, "sub.txt"), "w", encoding="utf8") as f: f.write("sub") yield real_dir_root finally: @@ -2203,7 +2205,7 @@ def test_write_to_real_file(self): # regression test for #470 real_file_path = os.path.abspath(__file__) self.filesystem.add_real_file(real_file_path, read_only=False) - with self.fake_open(real_file_path, "w") as f: + with self.fake_open(real_file_path, "w", encoding="utf8") as f: f.write("foo") with self.fake_open(real_file_path, "rb") as f: @@ -2289,7 +2291,7 @@ def test_add_existing_real_directory_symlink(self): self.filesystem.create_file("/etc/something") - with fake_open("/etc/something", "w") as f: + with fake_open("/etc/something", "w", encoding="utf8") as f: f.write("good morning") try: @@ -2385,7 +2387,8 @@ def test_add_existing_real_directory_symlink(self): "pyfakefs", "tests", "fixtures/symlink_file_absolute_outside", - ) + ), + encoding="utf8", ).read(), "good morning", ) @@ -2585,14 +2588,14 @@ def setUp(self): def test_side_effect_called(self): fake_open = fake_filesystem.FakeFileOpen(self.filesystem) self.side_effect_called = False - with fake_open("/a/b/file_one", "w") as handle: + with fake_open("/a/b/file_one", "w", encoding="utf8") as handle: handle.write("foo") self.assertTrue(self.side_effect_called) def test_side_effect_file_object(self): fake_open = fake_filesystem.FakeFileOpen(self.filesystem) self.side_effect_called = False - with fake_open("/a/b/file_one", "w") as handle: + with fake_open("/a/b/file_one", "w", encoding="utf8") as handle: handle.write("foo") self.assertEqual(self.side_effect_file_object_content, "foo") diff --git a/pyfakefs/tests/fake_filesystem_unittest_test.py b/pyfakefs/tests/fake_filesystem_unittest_test.py index 4a3566e4..72d9c7bd 100644 --- a/pyfakefs/tests/fake_filesystem_unittest_test.py +++ b/pyfakefs/tests/fake_filesystem_unittest_test.py @@ -56,14 +56,14 @@ class TestPatcher(TestCase): def test_context_manager(self): with Patcher() as patcher: patcher.fs.create_file("/foo/bar", contents="test") - with open("/foo/bar") as f: + with open("/foo/bar", encoding="utf8") as f: contents = f.read() self.assertEqual("test", contents) @patchfs def test_context_decorator(self, fake_fs): fake_fs.create_file("/foo/bar", contents="test") - with open("/foo/bar") as f: + with open("/foo/bar", encoding="utf8") as f: contents = f.read() self.assertEqual("test", contents) @@ -73,7 +73,7 @@ class TestPatchfsArgumentOrder(TestCase): @mock.patch("os.system") def test_argument_order1(self, fake_fs, patched_system): fake_fs.create_file("/foo/bar", contents="test") - with open("/foo/bar") as f: + with open("/foo/bar", encoding="utf8") as f: contents = f.read() self.assertEqual("test", contents) os.system("foo") @@ -83,7 +83,7 @@ def test_argument_order1(self, fake_fs, patched_system): @patchfs def test_argument_order2(self, patched_system, fake_fs): fake_fs.create_file("/foo/bar", contents="test") - with open("/foo/bar") as f: + with open("/foo/bar", encoding="utf8") as f: contents = f.read() self.assertEqual("test", contents) os.system("foo") @@ -102,10 +102,10 @@ class TestPyfakefsUnittest(TestPyfakefsUnittestBase): # pylint: disable=R0904 def test_open(self): """Fake `open()` function is bound""" self.assertFalse(os.path.exists("/fake_file.txt")) - with open("/fake_file.txt", "w") as f: + with open("/fake_file.txt", "w", encoding="utf8") as f: f.write("This test file was created using the open() function.\n") self.assertTrue(self.fs.exists("/fake_file.txt")) - with open("/fake_file.txt") as f: + with open("/fake_file.txt", encoding="utf8") as f: content = f.read() self.assertEqual( "This test file was created using the " "open() function.\n", @@ -115,10 +115,10 @@ def test_open(self): def test_io_open(self): """Fake io module is bound""" self.assertFalse(os.path.exists("/fake_file.txt")) - with io.open("/fake_file.txt", "w") as f: + with io.open("/fake_file.txt", "w", encoding="utf8") as f: f.write("This test file was created using the" " io.open() function.\n") self.assertTrue(self.fs.exists("/fake_file.txt")) - with open("/fake_file.txt") as f: + with open("/fake_file.txt", encoding="utf8") as f: content = f.read() self.assertEqual( "This test file was created using the " "io.open() function.\n", @@ -160,7 +160,7 @@ def test_shutil(self): def test_fakepathlib(self): p = pathlib.Path("/fake_file.txt") - with p.open("w") as f: + with p.open("w", encoding="utf8") as f: f.write("text") is_windows = sys.platform.startswith("win") if is_windows: @@ -532,7 +532,7 @@ def test_non_root_behavior(self): self.fs.create_file(file_path) os.chmod(file_path, 0o400) with self.assertRaises(OSError): - open(file_path, "w") + open(file_path, "w", encoding="utf8") class PauseResumeTest(fake_filesystem_unittest.TestCase): @@ -824,11 +824,11 @@ def test_real_file_with_home(self): if self.fs.is_windows_fs: self.fs.is_macos = False self.fs.add_real_file(__file__) - with open(__file__) as f: + with open(__file__, encoding="utf8") as f: self.assertTrue(f.read()) home = Path.home() os.chdir(home) - with open(__file__) as f: + with open(__file__, encoding="utf8") as f: self.assertTrue(f.read()) def test_windows(self): @@ -916,7 +916,7 @@ def setUpClass(cls): def test_using_fs_functions(self): self.assertTrue(os.path.exists("foo/bar")) - with open("foo/bar") as f: + with open("foo/bar", encoding="utf8") as f: contents = f.read() self.assertEqual("test", contents) diff --git a/pyfakefs/tests/fake_filesystem_vs_real_test.py b/pyfakefs/tests/fake_filesystem_vs_real_test.py index 3cc8f6c9..c20530d7 100644 --- a/pyfakefs/tests/fake_filesystem_vs_real_test.py +++ b/pyfakefs/tests/fake_filesystem_vs_real_test.py @@ -65,10 +65,10 @@ def _create_test_file(self, file_type, path, contents=None): os.mkdir(real_path) self.fake_os.mkdir(fake_path) if file_type == "f": - fh = open(real_path, "w") + fh = open(real_path, "w", encoding="utf8") fh.write(contents or "") fh.close() - fh = self.fake_open(fake_path, "w") + fh = self.fake_open(fake_path, "w", encoding="utf8") fh.write(contents or "") fh.close() # b for binary file @@ -318,8 +318,11 @@ def diff_open_method_behavior( Returns: A description of the difference in behavior, or None. """ - with open(path, mode) as real_fh: - with self.fake_open(path, mode) as fake_fh: + kwargs = {} + if "b" not in mode: + kwargs["encoding"] = "utf8" + with open(path, mode, **kwargs) as real_fh: + with self.fake_open(path, mode, **kwargs) as fake_fh: return self._compare_behaviors( method_name, data, real_fh, fake_fh, method_returns_data ) diff --git a/pyfakefs/tests/fake_open_test.py b/pyfakefs/tests/fake_open_test.py index 56979de0..de4bf9a0 100644 --- a/pyfakefs/tests/fake_open_test.py +++ b/pyfakefs/tests/fake_open_test.py @@ -63,13 +63,13 @@ def test_delete_on_close(self): file_path = "boo!far" self.os.mkdir(file_dir) self.open = fake_filesystem.FakeFileOpen(self.filesystem, delete_on_close=True) - with self.open(file_path, "w"): + with self.open(file_path, "w", encoding="utf8"): self.assertTrue(self.filesystem.exists(file_path)) self.assertFalse(self.filesystem.exists(file_path)) def test_no_delete_on_close_by_default(self): file_path = self.make_path("czar") - with self.open(file_path, "w"): + with self.open(file_path, "w", encoding="utf8"): self.assertTrue(self.os.path.exists(file_path)) self.assertTrue(self.os.path.exists(file_path)) @@ -78,7 +78,7 @@ def test_compatibility_of_with_statement(self): self.open = fake_filesystem.FakeFileOpen(self.filesystem, delete_on_close=True) file_path = "foo" self.assertFalse(self.os.path.exists(file_path)) - with self.open(file_path, "w"): + with self.open(file_path, "w", encoding="utf8"): self.assertTrue(self.os.path.exists(file_path)) # After the 'with' statement, the close() method should have been # called. @@ -91,13 +91,13 @@ def test_unicode_contents(self): # usually not UTF-8, but something like Latin1, depending on the locale text_fractions = "Ümläüts" try: - with self.open(file_path, "w") as f: + with self.open(file_path, "w", encoding=get_locale_encoding()) as f: f.write(text_fractions) except UnicodeEncodeError: # see https://github.com/pytest-dev/pyfakefs/issues/623 self.skipTest("This test does not work with an ASCII locale") - with self.open(file_path) as f: + with self.open(file_path, encoding=get_locale_encoding()) as f: contents = f.read() self.assertEqual(contents, text_fractions) @@ -116,7 +116,7 @@ def test_write_str_read_bytes(self): file_path = self.make_path("foo") str_contents = "Äsgül" try: - with self.open(file_path, "w") as f: + with self.open(file_path, "w", encoding=get_locale_encoding()) as f: f.write(str_contents) with self.open(file_path, "rb") as f: contents = f.read() @@ -134,7 +134,7 @@ def test_open_valid_file(self): ] file_path = self.make_path("bar.txt") self.create_file(file_path, contents="".join(contents)) - with self.open(file_path) as fake_file: + with self.open(file_path, encoding="utf8") as fake_file: self.assertEqual(contents, fake_file.readlines()) def test_open_valid_args(self): @@ -145,10 +145,15 @@ def test_open_valid_args(self): file_path = self.make_path("abbey_road", "maxwell") self.create_file(file_path, contents="".join(contents)) - with self.open(file_path, buffering=1) as f: + with self.open(file_path, encoding="utf8", buffering=1) as f: self.assertEqual(contents, f.readlines()) with self.open( - file_path, buffering=1, errors="strict", newline="\n", opener=None + file_path, + encoding="utf8", + buffering=1, + errors="strict", + newline="\n", + opener=None, ) as f: expected_contents = [ contents[0][:-1] + self.os.linesep, @@ -166,7 +171,7 @@ def test_open_valid_file_with_cwd(self): file_path = self.make_path("bar.txt") self.create_file(file_path, contents="".join(contents)) self.os.chdir(self.base_path) - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: self.assertEqual(contents, f.readlines()) def test_iterate_over_file(self): @@ -176,7 +181,7 @@ def test_iterate_over_file(self): ] file_path = self.make_path("abbey_road", "maxwell") self.create_file(file_path, contents="\n".join(contents)) - with self.open(file_path) as fake_file: + with self.open(file_path, encoding="utf8") as fake_file: result = [line.rstrip() for line in fake_file] self.assertEqual(contents, result) @@ -185,7 +190,7 @@ def test_next_over_file(self): result = [] file_path = self.make_path("foo.txt") self.create_file(file_path, contents="".join(contents)) - with self.open(file_path) as fake_file: + with self.open(file_path, encoding="utf8") as fake_file: result.append(next(fake_file)) result.append(next(fake_file)) self.assertEqual(contents, result) @@ -211,10 +216,10 @@ def test_create_file_with_write(self): file_dir = self.make_path("abbey_road") file_path = self.os.path.join(file_dir, "here_comes_the_sun") self.os.mkdir(file_dir) - with self.open(file_path, "w") as fake_file: + with self.open(file_path, "w", encoding="utf8") as fake_file: for line in contents: fake_file.write(line + "\n") - with self.open(file_path) as fake_file: + with self.open(file_path, encoding="utf8") as fake_file: result = [line.rstrip() for line in fake_file] self.assertEqual(contents, result) @@ -227,10 +232,10 @@ def test_create_file_with_append(self): file_dir = self.make_path("abbey_road") file_path = self.os.path.join(file_dir, "here_comes_the_sun") self.os.mkdir(file_dir) - with self.open(file_path, "a") as fake_file: + with self.open(file_path, "a", encoding="utf8") as fake_file: for line in contents: fake_file.write(line + "\n") - with self.open(file_path) as fake_file: + with self.open(file_path, encoding="utf8") as fake_file: result = [line.rstrip() for line in fake_file] self.assertEqual(contents, result) @@ -246,9 +251,9 @@ def test_exclusive_create_file(self): file_path = self.os.path.join(file_dir, "bar") self.os.mkdir(file_dir) contents = "String contents" - with self.open(file_path, "x") as fake_file: + with self.open(file_path, "x", encoding="utf8") as fake_file: fake_file.write(contents) - with self.open(file_path) as fake_file: + with self.open(file_path, encoding="utf8") as fake_file: self.assertEqual(contents, fake_file.read()) def test_exclusive_create_binary_file(self): @@ -268,10 +273,10 @@ def test_overwrite_existing_file(self): "Only these lines", "should be in the file.", ] - with self.open(file_path, "w") as fake_file: + with self.open(file_path, "w", encoding="utf8") as fake_file: for line in new_contents: fake_file.write(line + "\n") - with self.open(file_path) as fake_file: + with self.open(file_path, encoding="utf8") as fake_file: result = [line.rstrip() for line in fake_file] self.assertEqual(new_contents, result) @@ -282,10 +287,10 @@ def test_append_existing_file(self): ] self.create_file(file_path, contents=contents[0]) - with self.open(file_path, "a") as fake_file: + with self.open(file_path, "a", encoding="utf8") as fake_file: for line in contents[1:]: fake_file.write(line + "\n") - with self.open(file_path) as fake_file: + with self.open(file_path, encoding="utf8") as fake_file: result = [line.rstrip() for line in fake_file] self.assertEqual(contents, result) @@ -294,10 +299,10 @@ def test_open_with_wplus(self): file_path = self.make_path("wplus_file") self.create_file(file_path, contents="old contents") self.assertTrue(self.os.path.exists(file_path)) - with self.open(file_path, "r") as fake_file: + with self.open(file_path, "r", encoding="utf8") as fake_file: self.assertEqual("old contents", fake_file.read()) # actual tests - with self.open(file_path, "w+") as fake_file: + with self.open(file_path, "w+", encoding="utf8") as fake_file: fake_file.write("new contents") fake_file.seek(0) self.assertTrue("new contents", fake_file.read()) @@ -307,10 +312,10 @@ def test_open_with_wplus_truncation(self): file_path = self.make_path("wplus_file") self.create_file(file_path, contents="old contents") self.assertTrue(self.os.path.exists(file_path)) - with self.open(file_path, "r") as fake_file: + with self.open(file_path, "r", encoding="utf8") as fake_file: self.assertEqual("old contents", fake_file.read()) # actual tests - with self.open(file_path, "w+") as fake_file: + with self.open(file_path, "w+", encoding="utf8") as fake_file: fake_file.seek(0) self.assertEqual("", fake_file.read()) @@ -324,7 +329,7 @@ def test_open_with_append_flag(self): additional_contents = ["These new lines\n", "like you a lot.\n"] file_path = self.make_path("appendfile") self.create_file(file_path, contents="".join(contents)) - with self.open(file_path, "a") as fake_file: + with self.open(file_path, "a", encoding="utf8") as fake_file: with self.assertRaises(io.UnsupportedOperation): fake_file.read(0) with self.assertRaises(io.UnsupportedOperation): @@ -335,14 +340,14 @@ def test_open_with_append_flag(self): fake_file.seek(0) self.assertEqual(0, fake_file.tell()) fake_file.writelines(additional_contents) - with self.open(file_path) as fake_file: + with self.open(file_path, encoding="utf8") as fake_file: self.assertEqual(contents + additional_contents, fake_file.readlines()) def check_append_with_aplus(self): file_path = self.make_path("aplus_file") self.create_file(file_path, contents="old contents") self.assertTrue(self.os.path.exists(file_path)) - with self.open(file_path, "r") as fake_file: + with self.open(file_path, "r", encoding="utf8") as fake_file: self.assertEqual("old contents", fake_file.read()) if self.filesystem: @@ -350,7 +355,7 @@ def check_append_with_aplus(self): self.open = fake_filesystem.FakeFileOpen( self.filesystem, delete_on_close=True ) - with self.open(file_path, "a+") as fake_file: + with self.open(file_path, "a+", encoding="utf8") as fake_file: self.assertEqual(12, fake_file.tell()) fake_file.write("new contents") self.assertEqual(24, fake_file.tell()) @@ -370,10 +375,10 @@ def test_append_with_aplus_read_with_loop(self): file_path = self.make_path("aplus_file") self.create_file(file_path, contents="old contents") self.assertTrue(self.os.path.exists(file_path)) - with self.open(file_path, "r") as fake_file: + with self.open(file_path, "r", encoding="utf8") as fake_file: self.assertEqual("old contents", fake_file.read()) # actual tests - with self.open(file_path, "a+") as fake_file: + with self.open(file_path, "a+", encoding="utf8") as fake_file: fake_file.seek(0) fake_file.write("new contents") fake_file.seek(0) @@ -382,7 +387,7 @@ def test_append_with_aplus_read_with_loop(self): def test_read_empty_file_with_aplus(self): file_path = self.make_path("aplus_file") - with self.open(file_path, "a+") as fake_file: + with self.open(file_path, "a+", encoding="utf8") as fake_file: self.assertEqual("", fake_file.read()) def test_read_with_rplus(self): @@ -390,10 +395,10 @@ def test_read_with_rplus(self): file_path = self.make_path("rplus_file") self.create_file(file_path, contents="old contents here") self.assertTrue(self.os.path.exists(file_path)) - with self.open(file_path, "r") as fake_file: + with self.open(file_path, "r", encoding="utf8") as fake_file: self.assertEqual("old contents here", fake_file.read()) # actual tests - with self.open(file_path, "r+") as fake_file: + with self.open(file_path, "r+", encoding="utf8") as fake_file: self.assertEqual("old contents here", fake_file.read()) fake_file.seek(0) fake_file.write("new contents") @@ -415,11 +420,11 @@ def test_open_flags700(self): file_path = self.make_path("target_file") self.create_with_permission(file_path, 0o700) # actual tests - self.open(file_path, "r").close() - self.open(file_path, "w").close() - self.open(file_path, "w+").close() + self.open(file_path, "r", encoding="utf8").close() + self.open(file_path, "w", encoding="utf8").close() + self.open(file_path, "w+", encoding="utf8").close() with self.assertRaises(ValueError): - self.open(file_path, "INV") + self.open(file_path, "INV", encoding="utf8") def test_open_flags400(self): # set up @@ -427,13 +432,13 @@ def test_open_flags400(self): file_path = self.make_path("target_file") self.create_with_permission(file_path, 0o400) # actual tests - self.open(file_path, "r").close() + self.open(file_path, "r", encoding="utf8").close() if not is_root(): self.assert_raises_os_error(errno.EACCES, self.open, file_path, "w") self.assert_raises_os_error(errno.EACCES, self.open, file_path, "w+") else: - self.open(file_path, "w").close() - self.open(file_path, "w+").close() + self.open(file_path, "w", encoding="utf8").close() + self.open(file_path, "w+", encoding="utf8").close() def test_open_flags200(self): # set up @@ -441,15 +446,15 @@ def test_open_flags200(self): file_path = self.make_path("target_file") self.create_with_permission(file_path, 0o200) # actual tests - self.open(file_path, "w").close() + self.open(file_path, "w", encoding="utf8").close() if not is_root(): with self.assertRaises(OSError): - self.open(file_path, "r") + self.open(file_path, "r", encoding="utf8") with self.assertRaises(OSError): - self.open(file_path, "w+") + self.open(file_path, "w+", encoding="utf8") else: - self.open(file_path, "r").close() - self.open(file_path, "w+").close() + self.open(file_path, "r", encoding="utf8").close() + self.open(file_path, "w+", encoding="utf8").close() def test_open_flags100(self): # set up @@ -459,15 +464,15 @@ def test_open_flags100(self): # actual tests if not is_root(): with self.assertRaises(OSError): - self.open(file_path, "r") + self.open(file_path, "r", encoding="utf8") with self.assertRaises(OSError): - self.open(file_path, "w") + self.open(file_path, "w", encoding="utf8") with self.assertRaises(OSError): - self.open(file_path, "w+") + self.open(file_path, "w+", encoding="utf8") else: - self.open(file_path, "r").close() - self.open(file_path, "w").close() - self.open(file_path, "w+").close() + self.open(file_path, "r", encoding="utf8").close() + self.open(file_path, "w", encoding="utf8").close() + self.open(file_path, "w+", encoding="utf8").close() def test_follow_link_read(self): self.skip_if_symlink_not_supported() @@ -477,7 +482,7 @@ def test_follow_link_read(self): self.create_file(target, contents=target_contents) self.create_symlink(link_path, target) self.assert_equal_paths(target, self.os.readlink(link_path)) - fh = self.open(link_path, "r") + fh = self.open(link_path, "r", encoding="utf8") got_contents = fh.read() fh.close() self.assertEqual(target_contents, got_contents) @@ -490,9 +495,9 @@ def test_follow_link_write(self): self.create_symlink(link_path, target) self.assertFalse(self.os.path.exists(target)) - with self.open(link_path, "w") as fh: + with self.open(link_path, "w", encoding="utf8") as fh: fh.write(target_contents) - with self.open(target, "r") as fh: + with self.open(target, "r", encoding="utf8") as fh: got_contents = fh.read() self.assertEqual(target_contents, got_contents) @@ -513,9 +518,9 @@ def test_follow_intra_path_link_write(self): self.assertFalse(self.os.path.exists(target)) target_contents = "real baz contents" - with self.open(link_path, "w") as fh: + with self.open(link_path, "w", encoding="utf8") as fh: fh.write(target_contents) - with self.open(target, "r") as fh: + with self.open(target, "r", encoding="utf8") as fh: got_contents = fh.read() self.assertEqual(target_contents, got_contents) @@ -536,9 +541,9 @@ def test_file_descriptors_for_different_files(self): third_path = self.make_path("some_file3") self.create_file(third_path, contents="contents here3") - with self.open(first_path) as fake_file1: - with self.open(second_path) as fake_file2: - with self.open(third_path) as fake_file3: + with self.open(first_path, encoding="utf8") as fake_file1: + with self.open(second_path, encoding="utf8") as fake_file2: + with self.open(third_path, encoding="utf8") as fake_file3: fileno2 = fake_file2.fileno() self.assertGreater(fileno2, fake_file1.fileno()) self.assertGreater(fake_file3.fileno(), fileno2) @@ -548,9 +553,9 @@ def test_file_descriptors_for_the_same_file_are_different(self): self.create_file(first_path, contents="contents here1") second_path = self.make_path("some_file2") self.create_file(second_path, contents="contents here2") - with self.open(first_path) as fake_file1: - with self.open(second_path) as fake_file2: - with self.open(first_path) as fake_file1a: + with self.open(first_path, encoding="utf8") as fake_file1: + with self.open(second_path, encoding="utf8") as fake_file2: + with self.open(first_path, encoding="utf8") as fake_file1a: fileno2 = fake_file2.fileno() self.assertGreater(fileno2, fake_file1.fileno()) self.assertGreater(fake_file1a.fileno(), fileno2) @@ -563,17 +568,17 @@ def test_reused_file_descriptors_do_not_affect_others(self): third_path = self.make_path("some_file3") self.create_file(third_path, contents="contents here3") - with self.open(first_path, "r") as fake_file1: - with self.open(second_path, "r") as fake_file2: - fake_file3 = self.open(third_path, "r") - fake_file1a = self.open(first_path, "r") + with self.open(first_path, "r", encoding="utf8") as fake_file1: + with self.open(second_path, "r", encoding="utf8") as fake_file2: + fake_file3 = self.open(third_path, "r", encoding="utf8") + fake_file1a = self.open(first_path, "r", encoding="utf8") fileno1 = fake_file1.fileno() fileno2 = fake_file2.fileno() fileno3 = fake_file3.fileno() fileno4 = fake_file1a.fileno() - with self.open(second_path, "r") as fake_file2: - with self.open(first_path, "r") as fake_file1b: + with self.open(second_path, "r", encoding="utf8") as fake_file2: + with self.open(first_path, "r", encoding="utf8") as fake_file1b: self.assertEqual(fileno1, fake_file2.fileno()) self.assertEqual(fileno2, fake_file1b.fileno()) self.assertEqual(fileno3, fake_file3.fileno()) @@ -585,8 +590,8 @@ def test_intertwined_read_write(self): file_path = self.make_path("some_file") self.create_file(file_path) - with self.open(file_path, "a") as writer: - with self.open(file_path, "r") as reader: + with self.open(file_path, "a", encoding="utf8") as writer: + with self.open(file_path, "r", encoding="utf8") as reader: writes = [ "hello", "world\n", @@ -639,17 +644,17 @@ def test_open_io_errors(self): file_path = self.make_path("some_file") self.create_file(file_path) - with self.open(file_path, "a") as fh: + with self.open(file_path, "a", encoding="utf8") as fh: with self.assertRaises(OSError): fh.read() with self.assertRaises(OSError): fh.readlines() - with self.open(file_path, "w") as fh: + with self.open(file_path, "w", encoding="utf8") as fh: with self.assertRaises(OSError): fh.read() with self.assertRaises(OSError): fh.readlines() - with self.open(file_path, "r") as fh: + with self.open(file_path, "r", encoding="utf8") as fh: with self.assertRaises(OSError): fh.truncate() with self.assertRaises(OSError): @@ -658,7 +663,7 @@ def test_open_io_errors(self): fh.writelines(["con", "tents"]) def _iterator_open(mode): - with self.open(file_path, mode) as f: + with self.open(file_path, mode, encoding="utf8") as f: for _ in f: pass @@ -702,14 +707,14 @@ def test_can_read_from_block_device(self): self.skip_real_fs() device_path = "device" self.filesystem.create_file(device_path, stat.S_IFBLK | helpers.PERM_ALL) - with self.open(device_path, "r") as fh: + with self.open(device_path, "r", encoding="utf8") as fh: self.assertEqual("", fh.read()) def test_truncate_flushes_contents(self): # Regression test for #285 file_path = self.make_path("baz") self.create_file(file_path) - with self.open(file_path, "w") as f0: + with self.open(file_path, "w", encoding="utf8") as f0: f0.write("test") f0.truncate() self.assertEqual(4, self.os.path.getsize(file_path)) @@ -717,8 +722,8 @@ def test_truncate_flushes_contents(self): def test_update_other_instances_of_same_file_on_flush(self): # Regression test for #302 file_path = self.make_path("baz") - with self.open(file_path, "w") as f0: - with self.open(file_path, "w") as f1: + with self.open(file_path, "w", encoding="utf8") as f0: + with self.open(file_path, "w", encoding="utf8") as f1: f0.write("test") f0.truncate() f1.flush() @@ -727,7 +732,7 @@ def test_update_other_instances_of_same_file_on_flush(self): def test_getsize_after_truncate(self): # Regression test for #412 file_path = self.make_path("foo") - with self.open(file_path, "a") as f: + with self.open(file_path, "a", encoding="utf8") as f: f.write("a") f.seek(0) f.truncate() @@ -739,7 +744,7 @@ def test_getsize_after_truncate(self): def test_st_size_after_truncate(self): # Regression test for #412 file_path = self.make_path("foo") - with self.open(file_path, "a") as f: + with self.open(file_path, "a", encoding="utf8") as f: f.write("a") f.truncate() f.write("b") @@ -750,7 +755,7 @@ def test_that_read_over_end_does_not_reset_position(self): # Regression test for #286 file_path = self.make_path("baz") self.create_file(file_path) - with self.open(file_path) as f0: + with self.open(file_path, encoding="utf8") as f0: f0.seek(2) f0.read() self.assertEqual(2, f0.tell()) @@ -761,7 +766,7 @@ def test_accessing_closed_file_raises(self): raise unittest.SkipTest("Different exceptions with PyPy") file_path = self.make_path("foo") self.create_file(file_path, contents=b"test") - fake_file = self.open(file_path, "r") + fake_file = self.open(file_path, "r", encoding="utf8") fake_file.close() with self.assertRaises(ValueError): fake_file.read(1) @@ -784,7 +789,7 @@ def test_accessing_open_file_with_another_handle_raises(self): raise unittest.SkipTest("Different exceptions with PyPy") file_path = self.make_path("foo") f0 = self.os.open(file_path, os.O_CREAT | os.O_WRONLY | os.O_TRUNC) - fake_file = self.open(file_path, "r") + fake_file = self.open(file_path, "r", encoding="utf8") fake_file.close() with self.assertRaises(ValueError): fake_file.read(1) @@ -796,7 +801,7 @@ def test_tell_flushes_under_mac_os(self): # Regression test for #288 self.check_macos_only() file_path = self.make_path("foo") - with self.open(file_path, "w") as f0: + with self.open(file_path, "w", encoding="utf8") as f0: f0.write("test") self.assertEqual(4, f0.tell()) self.assertEqual(4, self.os.path.getsize(file_path)) @@ -805,7 +810,7 @@ def test_tell_flushes_in_python3(self): # Regression test for #288 self.check_linux_and_windows() file_path = self.make_path("foo") - with self.open(file_path, "w") as f0: + with self.open(file_path, "w", encoding="utf8") as f0: f0.write("test") self.assertEqual(4, f0.tell()) self.assertEqual(4, self.os.path.getsize(file_path)) @@ -814,7 +819,7 @@ def test_read_flushes_under_posix(self): # Regression test for #278 self.check_posix_only() file_path = self.make_path("foo") - with self.open(file_path, "a+") as f0: + with self.open(file_path, "a+", encoding="utf8") as f0: f0.write("test") self.assertEqual("", f0.read()) self.assertEqual(4, self.os.path.getsize(file_path)) @@ -823,7 +828,7 @@ def test_read_flushes_under_windows_in_python3(self): # Regression test for #278 self.check_windows_only() file_path = self.make_path("foo") - with self.open(file_path, "w+") as f0: + with self.open(file_path, "w+", encoding="utf8") as f0: f0.write("test") f0.read() self.assertEqual(4, self.os.path.getsize(file_path)) @@ -831,7 +836,7 @@ def test_read_flushes_under_windows_in_python3(self): def test_seek_flushes(self): # Regression test for #290 file_path = self.make_path("foo") - with self.open(file_path, "w") as f0: + with self.open(file_path, "w", encoding="utf8") as f0: f0.write("test") self.assertEqual(0, self.os.path.getsize(file_path)) f0.seek(3) @@ -840,7 +845,7 @@ def test_seek_flushes(self): def test_truncate_flushes(self): # Regression test for #291 file_path = self.make_path("foo") - with self.open(file_path, "a") as f0: + with self.open(file_path, "a", encoding="utf8") as f0: f0.write("test") self.assertEqual(0, self.os.path.getsize(file_path)) f0.truncate() @@ -849,7 +854,7 @@ def test_truncate_flushes(self): def check_seek_outside_and_truncate_sets_size(self, mode): # Regression test for #294 and #296 file_path = self.make_path("baz") - with self.open(file_path, mode) as f0: + with self.open(file_path, mode, encoding="utf8") as f0: f0.seek(1) f0.truncate() self.assertEqual(1, f0.tell()) @@ -868,11 +873,11 @@ def test_seek_outside_and_truncate_sets_size_in_append_mode(self): def test_closed(self): file_path = self.make_path("foo") - f = self.open(file_path, "w") + f = self.open(file_path, "w", encoding="utf8") self.assertFalse(f.closed) f.close() self.assertTrue(f.closed) - f = self.open(file_path) + f = self.open(file_path, encoding="utf8") self.assertFalse(f.closed) f.close() self.assertTrue(f.closed) @@ -880,9 +885,9 @@ def test_closed(self): def test_closing_closed_file_does_nothing(self): # Regression test for #299 file_path = self.make_path("baz") - f0 = self.open(file_path, "w") + f0 = self.open(file_path, "w", encoding="utf8") f0.close() - with self.open(file_path) as f1: + with self.open(file_path, encoding="utf8") as f1: # would close f1 if not handled f0.close() self.assertEqual("", f1.read()) @@ -901,8 +906,8 @@ def test_closing_file_with_different_close_mode(self): def test_truncate_flushes_zeros(self): # Regression test for #301 file_path = self.make_path("baz") - with self.open(file_path, "w") as f0: - with self.open(file_path) as f1: + with self.open(file_path, "w", encoding="utf8") as f0: + with self.open(file_path, encoding="utf8") as f1: f0.seek(1) f0.truncate() self.assertEqual("\0", f1.read()) @@ -923,9 +928,9 @@ def test_unicode_filename(self): def test_write_devnull(self): for mode in ("r+", "w", "w+", "a", "a+"): - with self.open(self.os.devnull, mode) as f: + with self.open(self.os.devnull, mode, encoding="utf8") as f: f.write("test") - with self.open(self.os.devnull) as f: + with self.open(self.os.devnull, encoding="utf8") as f: self.assertEqual("", f.read()) def test_utf16_text(self): @@ -954,7 +959,7 @@ def opener(self, path, flags): def test_use_opener_with_read(self): file_path = self.make_path("foo") self.create_file(file_path, contents="test") - with self.open(file_path, opener=self.opener) as f: + with self.open(file_path, encoding="utf8", opener=self.opener) as f: assert f.read() == "test" with self.assertRaises(OSError): f.write("foo") @@ -962,7 +967,7 @@ def test_use_opener_with_read(self): def test_no_opener_with_read(self): file_path = self.make_path("foo") self.create_file(file_path, contents="test") - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: assert f.read() == "test" with self.assertRaises(OSError): f.write("foo") @@ -970,75 +975,75 @@ def test_no_opener_with_read(self): def test_use_opener_with_read_plus(self): file_path = self.make_path("foo") self.create_file(file_path, contents="test") - with self.open(file_path, "r+", opener=self.opener) as f: + with self.open(file_path, "r+", encoding="utf8", opener=self.opener) as f: assert f.read() == "test" assert f.write("bar") == 3 - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: assert f.read() == "testbar" def test_use_opener_with_write(self): file_path = self.make_path("foo") self.create_file(file_path, contents="foo") - with self.open(file_path, "w", opener=self.opener) as f: + with self.open(file_path, "w", encoding="utf8", opener=self.opener) as f: with self.assertRaises(OSError): f.read() assert f.write("bar") == 3 - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: assert f.read() == "bar" def test_use_opener_with_write_plus(self): file_path = self.make_path("foo") self.create_file(file_path, contents="test") - with self.open(file_path, "w+", opener=self.opener) as f: + with self.open(file_path, "w+", encoding="utf8", opener=self.opener) as f: assert f.read() == "" assert f.write("bar") == 3 - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: assert f.read() == "bar" def test_use_opener_with_append(self): file_path = self.make_path("foo") self.create_file(file_path, contents="foo") - with self.open(file_path, "a", opener=self.opener) as f: + with self.open(file_path, "a", encoding="utf8", opener=self.opener) as f: assert f.write("bar") == 3 with self.assertRaises(OSError): f.read() - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: assert f.read() == "foobar" def test_use_opener_with_append_plus(self): file_path = self.make_path("foo") self.create_file(file_path, contents="foo") - with self.open(file_path, "a+", opener=self.opener) as f: + with self.open(file_path, "a+", encoding="utf8", opener=self.opener) as f: assert f.read() == "" assert f.write("bar") == 3 - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: assert f.read() == "foobar" def test_use_opener_with_exclusive_write(self): file_path = self.make_path("foo") self.create_file(file_path, contents="test") with self.assertRaises(OSError): - self.open(file_path, "x", opener=self.opener) + self.open(file_path, "x", encoding="utf8", opener=self.opener) file_path = self.make_path("bar") - with self.open(file_path, "x", opener=self.opener) as f: + with self.open(file_path, "x", encoding="utf8", opener=self.opener) as f: assert f.write("bar") == 3 with self.assertRaises(OSError): f.read() - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: assert f.read() == "bar" def test_use_opener_with_exclusive_plus(self): file_path = self.make_path("foo") self.create_file(file_path, contents="test") with self.assertRaises(OSError): - self.open(file_path, "x+", opener=self.opener) + self.open(file_path, "x+", encoding="utf8", opener=self.opener) file_path = self.make_path("bar") - with self.open(file_path, "x+", opener=self.opener) as f: + with self.open(file_path, "x+", encoding="utf8", opener=self.opener) as f: assert f.write("bar") == 3 assert f.read() == "" - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: assert f.read() == "bar" @@ -1142,7 +1147,7 @@ def test_no_buffering(self): def test_no_buffering_not_allowed_in_textmode(self): file_path = self.make_path("buffertest.txt") with self.assertRaises(ValueError): - self.open(file_path, "w", buffering=0) + self.open(file_path, "w", encoding="utf8", buffering=0) def test_default_buffering_no_flush(self): file_path = self.make_path("buffertest.bin") @@ -1196,95 +1201,95 @@ def test_writing_with_specific_buffer(self): def test_writing_text_with_line_buffer(self): file_path = self.make_path("buffertest.bin") - with self.open(file_path, "w", buffering=1) as f: + with self.open(file_path, "w", encoding="utf8", buffering=1) as f: f.write("test" * 100) - with self.open(file_path, "r") as r: + with self.open(file_path, "r", encoding="utf8") as r: x = r.read() # no new line - not written self.assertEqual(0, len(x)) f.write("\ntest") - with self.open(file_path, "r") as r: + with self.open(file_path, "r", encoding="utf8") as r: x = r.read() # new line - buffer written self.assertEqual(405, len(x)) f.write("test" * 10) - with self.open(file_path, "r") as r: + with self.open(file_path, "r", encoding="utf8") as r: x = r.read() # buffer not filled - not written self.assertEqual(405, len(x)) f.write("\ntest") - with self.open(file_path, "r") as r: + with self.open(file_path, "r", encoding="utf8") as r: x = r.read() # new line - buffer written self.assertEqual(450, len(x)) def test_writing_large_text_with_line_buffer(self): file_path = self.make_path("buffertest.bin") - with self.open(file_path, "w", buffering=1) as f: + with self.open(file_path, "w", encoding="utf8", buffering=1) as f: f.write("test" * 4000) - with self.open(file_path, "r") as r: + with self.open(file_path, "r", encoding="utf8") as r: x = r.read() # buffer larger than default - written self.assertEqual(16000, len(x)) f.write("test") - with self.open(file_path, "r") as r: + with self.open(file_path, "r", encoding="utf8") as r: x = r.read() # buffer not filled - not written self.assertEqual(16000, len(x)) f.write("\ntest") - with self.open(file_path, "r") as r: + with self.open(file_path, "r", encoding="utf8") as r: x = r.read() # new line - buffer written self.assertEqual(16009, len(x)) f.write("\ntest") - with self.open(file_path, "r") as r: + with self.open(file_path, "r", encoding="utf8") as r: x = r.read() # another new line - buffer written self.assertEqual(16014, len(x)) def test_writing_text_with_default_buffer(self): file_path = self.make_path("buffertest.txt") - with self.open(file_path, "w") as f: + with self.open(file_path, "w", encoding="utf8") as f: f.write("test" * 5) - with self.open(file_path, "r") as r: + with self.open(file_path, "r", encoding="utf8") as r: x = r.read() # buffer not filled - not written self.assertEqual(0, len(x)) f.write("\ntest") - with self.open(file_path, "r") as r: + with self.open(file_path, "r", encoding="utf8") as r: x = r.read() # buffer exceeded, but new buffer (400) not - previous written self.assertEqual(0, len(x)) f.write("test" * 10) - with self.open(file_path, "r") as r: + with self.open(file_path, "r", encoding="utf8") as r: x = r.read() # buffer not filled - not written self.assertEqual(0, len(x)) f.write("\ntest") - with self.open(file_path, "r") as r: + with self.open(file_path, "r", encoding="utf8") as r: x = r.read() self.assertEqual(0, len(x)) def test_writing_text_with_specific_buffer(self): file_path = self.make_path("buffertest.txt") - with self.open(file_path, "w", buffering=2) as f: + with self.open(file_path, "w", encoding="utf8", buffering=2) as f: f.write("a" * 8000) - with self.open(file_path, "r") as r: + with self.open(file_path, "r", encoding="utf8") as r: x = r.read() # buffer not filled - not written self.assertEqual(0, len(x)) f.write("test") - with self.open(file_path, "r") as r: + with self.open(file_path, "r", encoding="utf8") as r: x = r.read() # buffer exceeded, but new buffer (400) not - previous written self.assertEqual(0, len(x)) f.write("test") - with self.open(file_path, "r") as r: + with self.open(file_path, "r", encoding="utf8") as r: x = r.read() # buffer not filled - not written self.assertEqual(0, len(x)) f.write("test") - with self.open(file_path, "r") as r: + with self.open(file_path, "r", encoding="utf8") as r: x = r.read() self.assertEqual(0, len(x)) # with self.open(file_path, "r") as r: @@ -1536,21 +1541,21 @@ def test_read_default_newline_mode(self): file_path = self.make_path("some_file") for contents in (b"1\n2", b"1\r\n2", b"1\r2"): self.create_file(file_path, contents=contents) - with self.open(file_path, mode="r") as f: + with self.open(file_path, mode="r", encoding="utf8") as f: self.assertEqual(["1\n", "2"], f.readlines()) - with self.open(file_path, mode="r") as f: + with self.open(file_path, mode="r", encoding="utf8") as f: self.assertEqual("1\n2", f.read()) with self.open(file_path, mode="rb") as f: self.assertEqual(contents, f.read()) def test_write_universal_newline_mode(self): file_path = self.make_path("some_file") - with self.open(file_path, "w") as f: + with self.open(file_path, "w", encoding="utf8") as f: f.write("1\n2") with self.open(file_path, mode="rb") as f: self.assertEqual(b"1" + self.os.linesep.encode() + b"2", f.read()) - with self.open(file_path, "w") as f: + with self.open(file_path, "w", encoding="utf8") as f: f.write("1\r\n2") with self.open(file_path, mode="rb") as f: self.assertEqual(b"1\r" + self.os.linesep.encode() + b"2", f.read()) @@ -1559,26 +1564,26 @@ def test_read_with_newline_arg(self): file_path = self.make_path("some_file") file_contents = b"1\r\n2\n3\r4" self.create_file(file_path, contents=file_contents) - with self.open(file_path, mode="r", newline="") as f: + with self.open(file_path, mode="r", encoding="utf8", newline="") as f: self.assertEqual("1\r\n2\n3\r4", f.read()) - with self.open(file_path, mode="r", newline="\r") as f: + with self.open(file_path, mode="r", encoding="utf8", newline="\r") as f: self.assertEqual("1\r\n2\n3\r4", f.read()) - with self.open(file_path, mode="r", newline="\n") as f: + with self.open(file_path, mode="r", encoding="utf8", newline="\n") as f: self.assertEqual("1\r\n2\n3\r4", f.read()) - with self.open(file_path, mode="r", newline="\r\n") as f: + with self.open(file_path, mode="r", encoding="utf8", newline="\r\n") as f: self.assertEqual("1\r\n2\n3\r4", f.read()) def test_readlines_with_newline_arg(self): file_path = self.make_path("some_file") file_contents = b"1\r\n2\n3\r4" self.create_file(file_path, contents=file_contents) - with self.open(file_path, mode="r", newline="") as f: + with self.open(file_path, mode="r", encoding="utf8", newline="") as f: self.assertEqual(["1\r\n", "2\n", "3\r", "4"], f.readlines()) - with self.open(file_path, mode="r", newline="\r") as f: + with self.open(file_path, mode="r", encoding="utf8", newline="\r") as f: self.assertEqual(["1\r", "\n2\n3\r", "4"], f.readlines()) - with self.open(file_path, mode="r", newline="\n") as f: + with self.open(file_path, mode="r", encoding="utf8", newline="\n") as f: self.assertEqual(["1\r\n", "2\n", "3\r4"], f.readlines()) - with self.open(file_path, mode="r", newline="\r\n") as f: + with self.open(file_path, mode="r", encoding="utf8", newline="\r\n") as f: self.assertEqual(["1\r\n", "2\n3\r4"], f.readlines()) @unittest.skipIf(sys.version_info >= (3, 10), "U flag no longer supported") @@ -1586,11 +1591,11 @@ def test_read_with_ignored_universal_newlines_flag(self): file_path = self.make_path("some_file") file_contents = b"1\r\n2\n3\r4" self.create_file(file_path, contents=file_contents) - with self.open(file_path, mode="r", newline="\r") as f: + with self.open(file_path, mode="r", encoding="utf8", newline="\r") as f: self.assertEqual("1\r\n2\n3\r4", f.read()) - with self.open(file_path, mode="r", newline="\r") as f: + with self.open(file_path, mode="r", encoding="utf8", newline="\r") as f: self.assertEqual("1\r\n2\n3\r4", f.read()) - with self.open(file_path, mode="U", newline="\r") as f: + with self.open(file_path, mode="U", encoding="utf8", newline="\r") as f: self.assertEqual("1\r\n2\n3\r4", f.read()) @unittest.skipIf(sys.version_info < (3, 11), "U flag still supported") @@ -1599,26 +1604,26 @@ def test_universal_newlines_flag_not_supported(self): file_contents = b"1\r\n2\n3\r4" self.create_file(file_path, contents=file_contents) with self.assertRaises(ValueError): - self.open(file_path, mode="U", newline="\r") + self.open(file_path, mode="U", encoding="utf8", newline="\r") def test_write_with_newline_arg(self): file_path = self.make_path("some_file") - with self.open(file_path, "w", newline="") as f: + with self.open(file_path, "w", encoding="utf8", newline="") as f: f.write("1\r\n2\n3\r4") with self.open(file_path, mode="rb") as f: self.assertEqual(b"1\r\n2\n3\r4", f.read()) - with self.open(file_path, "w", newline="\n") as f: + with self.open(file_path, "w", encoding="utf8", newline="\n") as f: f.write("1\r\n2\n3\r4") with self.open(file_path, mode="rb") as f: self.assertEqual(b"1\r\n2\n3\r4", f.read()) - with self.open(file_path, "w", newline="\r\n") as f: + with self.open(file_path, "w", encoding="utf8", newline="\r\n") as f: f.write("1\r\n2\n3\r4") with self.open(file_path, mode="rb") as f: self.assertEqual(b"1\r\r\n2\r\n3\r4", f.read()) - with self.open(file_path, "w", newline="\r") as f: + with self.open(file_path, "w", encoding="utf8", newline="\r") as f: f.write("1\r\n2\n3\r4") with self.open(file_path, mode="rb") as f: self.assertEqual(b"1\r\r2\r3\r4", f.read()) @@ -1742,16 +1747,16 @@ def test_open_with_file_descriptor(self): file_path = self.make_path("this", "file") self.create_file(file_path) fd = self.os.open(file_path, os.O_CREAT) - self.assertEqual(fd, self.open(fd, "r").fileno()) + self.assertEqual(fd, self.open(fd, "r", encoding="utf8").fileno()) def test_closefd_with_file_descriptor(self): file_path = self.make_path("this", "file") self.create_file(file_path) fd = self.os.open(file_path, os.O_CREAT) - fh = self.open(fd, "r", closefd=False) + fh = self.open(fd, "r", encoding="utf8", closefd=False) fh.close() self.assertIsNotNone(self.filesystem.open_files[fd]) - fh = self.open(fd, "r", closefd=True) + fh = self.open(fd, "r", encoding="utf8", closefd=True) fh.close() self.assertIsNone(self.filesystem.open_files[fd]) @@ -1768,7 +1773,10 @@ def setUp(self): self.file_contents = None def open_file(self, mode): - return self.open(self.file_path, mode=mode) + kwargs = {"mode": mode} + if "b" not in mode: + kwargs["encoding"] = "utf8" + return self.open(self.file_path, **kwargs) def open_file_and_seek(self, mode): fake_file = self.open(self.file_path, mode=mode) @@ -1878,16 +1886,16 @@ def use_real_fs(self): class ResolvePathTest(FakeFileOpenTestBase): def write_to_file(self, file_name): - with self.open(file_name, "w") as fh: + with self.open(file_name, "w", encoding="utf8") as fh: fh.write("x") def test_none_filepath_raises_type_error(self): with self.assertRaises(TypeError): - self.open(None, "w") + self.open(None, "w", encoding="utf8") def test_empty_filepath_raises_io_error(self): with self.assertRaises(OSError): - self.open("", "w") + self.open("", "w", encoding="utf8") def test_normal_path(self): file_path = self.make_path("foo") @@ -2016,7 +2024,7 @@ def test_read_link_to_link(self): self.create_symlink(link_path, "link") self.create_symlink(self.make_path("foo", "link"), "baz") self.write_to_file(self.make_path("foo", "baz")) - fh = self.open(link_path, "r") + fh = self.open(link_path, "r", encoding="utf8") self.assertEqual("x", fh.read()) def test_write_link_to_link(self): diff --git a/pyfakefs/tests/fake_os_test.py b/pyfakefs/tests/fake_os_test.py index 1c330978..09230682 100644 --- a/pyfakefs/tests/fake_os_test.py +++ b/pyfakefs/tests/fake_os_test.py @@ -188,9 +188,9 @@ def test_listdir_current(self): def test_fdopen(self): file_path1 = self.make_path("some_file1") self.create_file(file_path1, contents="contents here1") - with self.open(file_path1, "r") as fake_file1: + with self.open(file_path1, "r", encoding="utf8") as fake_file1: fileno = fake_file1.fileno() - fake_file2 = self.os.fdopen(fileno) + fake_file2 = self.os.fdopen(fileno, encoding="utf8") self.assertNotEqual(fake_file2, fake_file1) with self.assertRaises(TypeError): @@ -200,7 +200,8 @@ def test_fdopen(self): def test_out_of_range_fdopen(self): # test some file descriptor that is clearly out of range - self.assert_raises_os_error(errno.EBADF, self.os.fdopen, 500) + kwargs = {"encoding": "utf8"} + self.assert_raises_os_error(errno.EBADF, self.os.fdopen, 500, **kwargs) def test_closed_file_descriptor(self): first_path = self.make_path("some_file1") @@ -210,9 +211,9 @@ def test_closed_file_descriptor(self): self.create_file(second_path, contents="contents here2") self.create_file(third_path, contents="contents here3") - fake_file1 = self.open(first_path, "r") - fake_file2 = self.open(second_path, "r") - fake_file3 = self.open(third_path, "r") + fake_file1 = self.open(first_path, "r", encoding="utf8") + fake_file2 = self.open(second_path, "r", encoding="utf8") + fake_file3 = self.open(third_path, "r", encoding="utf8") fileno1 = fake_file1.fileno() fileno2 = fake_file2.fileno() fileno3 = fake_file3.fileno() @@ -222,33 +223,34 @@ def test_closed_file_descriptor(self): self.assertEqual(fileno1, fake_file1.fileno()) self.assertEqual(fileno3, fake_file3.fileno()) - with self.os.fdopen(fileno1) as f: + with self.os.fdopen(fileno1, encoding="utf8") as f: self.assertFalse(f is fake_file1) - with self.os.fdopen(fileno3) as f: + with self.os.fdopen(fileno3, encoding="utf8") as f: self.assertFalse(f is fake_file3) - self.assert_raises_os_error(errno.EBADF, self.os.fdopen, fileno2) + kwargs = {"encoding": "utf8"} + self.assert_raises_os_error(errno.EBADF, self.os.fdopen, fileno2, **kwargs) def test_fdopen_twice(self): file_path = self.make_path("some_file1") self.create_file(file_path, contents="contents here1") - fake_file = self.open(file_path, "r") + fake_file = self.open(file_path, "r", encoding="utf8") fd = fake_file.fileno() - self.open(fd) + self.open(fd, encoding="utf8") if not IS_PYPY: with self.assertRaises(OSError) as cm: - self.open(fd) + self.open(fd, encoding="utf8") self.assertEqual(errno.EBADF, cm.exception.errno) else: - self.open(fd) + self.open(fd, encoding="utf8") self.os.close(fd) def test_open_fd_write_mode_for_ro_file(self): # Create a writable file handle to a read-only file, see #967 file_path = self.make_path("file.txt") fd = self.os.open(file_path, os.O_CREAT | os.O_WRONLY, 0o555) - with self.open(fd, "w") as out: + with self.open(fd, "w", encoding="utf8") as out: out.write("hey") - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: assert f.read() == "hey" self.os.chmod(file_path, 0o655) @@ -278,7 +280,7 @@ def test_fstat(self): directory = self.make_path("xyzzy") file_path = self.os.path.join(directory, "plugh") self.create_file(file_path, contents="ABCDE") - with self.open(file_path) as file_obj: + with self.open(file_path, encoding="utf8") as file_obj: fileno = file_obj.fileno() self.assertTrue(stat.S_IFREG & self.os.fstat(fileno)[stat.ST_MODE]) self.assertTrue(stat.S_IFREG & self.os.fstat(fileno).st_mode) @@ -346,7 +348,7 @@ def test_stat_uses_open_fd_as_path(self): file_path = self.make_path("foo", "bar") self.create_file(file_path) - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: self.assertTrue(stat.S_IFREG & self.os.stat(f.filedes)[stat.ST_MODE]) def test_stat_no_follow_symlinks_posix(self): @@ -567,7 +569,7 @@ def test_lstat_uses_open_fd_as_path(self): self.create_file(file_path, contents=file_contents) self.create_symlink(link_path, file_path) - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: self.assertEqual(len(file_contents), self.os.lstat(f.filedes)[stat.ST_SIZE]) def test_stat_non_existent_file(self): @@ -944,7 +946,7 @@ def test_remove_open_file_fails_under_windows(self): self.check_windows_only() path = self.make_path("foo", "bar") self.create_file(path) - with self.open(path, "r"): + with self.open(path, "r", encoding="utf8"): self.assert_raises_os_error(errno.EACCES, self.os.remove, path) self.assertTrue(self.os.path.exists(path)) @@ -952,7 +954,7 @@ def test_remove_open_file_possible_under_posix(self): self.check_posix_only() path = self.make_path("foo", "bar") self.create_file(path) - self.open(path, "r") + self.open(path, "r", encoding="utf8") self.os.remove(path) self.assertFalse(self.os.path.exists(path)) @@ -1373,8 +1375,8 @@ def test_rename_symlink(self): def check_append_mode_tell_after_truncate(self, tell_result): file_path = self.make_path("baz") - with self.open(file_path, "w") as f0: - with self.open(file_path, "a") as f1: + with self.open(file_path, "w", encoding="utf8") as f0: + with self.open(file_path, "a", encoding="utf8") as f1: f1.write("abcde") f0.seek(2) f0.truncate() @@ -1395,14 +1397,14 @@ def test_append_mode_tell_macos(self): def test_tell_after_seek_in_append_mode(self): # Regression test for #363 file_path = self.make_path("foo") - with self.open(file_path, "a") as f: + with self.open(file_path, "a", encoding="utf8") as f: f.seek(1) self.assertEqual(1, f.tell()) def test_tell_after_seekback_in_append_mode(self): # Regression test for #414 file_path = self.make_path("foo") - with self.open(file_path, "a") as f: + with self.open(file_path, "a", encoding="utf8") as f: f.write("aa") f.seek(1) self.assertEqual(1, f.tell()) @@ -1923,7 +1925,7 @@ def test_fsync_pass_posix(self): self.check_posix_only() test_file_path = self.make_path("test_file") self.create_file(test_file_path, contents="dummy file contents") - with self.open(test_file_path, "r") as test_file: + with self.open(test_file_path, "r", encoding="utf8") as test_file: test_fd = test_file.fileno() # Test that this doesn't raise anything self.os.fsync(test_fd) @@ -1934,13 +1936,13 @@ def test_fsync_pass_windows(self): self.check_windows_only() test_file_path = self.make_path("test_file") self.create_file(test_file_path, contents="dummy file contents") - with self.open(test_file_path, "r+") as test_file: + with self.open(test_file_path, "r+", encoding="utf8") as test_file: test_fd = test_file.fileno() # Test that this doesn't raise anything self.os.fsync(test_fd) # And just for sanity, double-check that this still raises self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd + 500) - with self.open(test_file_path, "r") as test_file: + with self.open(test_file_path, "r", encoding="utf8") as test_file: test_fd = test_file.fileno() self.assert_raises_os_error(errno.EBADF, self.os.fsync, test_fd) @@ -1949,7 +1951,7 @@ def test_fdatasync_pass(self): self.check_linux_only() test_file_path = self.make_path("test_file") self.create_file(test_file_path, contents="dummy file contents") - test_file = self.open(test_file_path, "r") + test_file = self.open(test_file_path, "r", encoding="utf8") test_fd = test_file.fileno() # Test that this doesn't raise anything self.os.fdatasync(test_fd) @@ -2071,7 +2073,7 @@ def test_chmod_uses_open_fd_as_path(self): path = self.make_path("some_file") self.createTestFile(path) - with self.open(path) as f: + with self.open(path, encoding="utf8") as f: self.os.chmod(f.filedes, 0o6543) st = self.os.stat(path) self.assert_mode_equal(0o6543, st.st_mode) @@ -2176,7 +2178,7 @@ def test_chown_uses_open_fd_as_path(self): file_path = self.make_path("foo", "bar") self.create_file(file_path) - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: self.os.chown(f.filedes, 100, 101) st = self.os.stat(file_path) self.assertEqual(st[stat.ST_UID], 100) @@ -2239,7 +2241,7 @@ def test_fail_add_entry_to_readonly_dir(self): # adding a new entry to the readonly subdirectory should fail with self.assertRaises(PermissionError): - with self.open(f"{ro_dir}/file.txt", "w"): + with self.open(f"{ro_dir}/file.txt", "w", encoding="utf8"): pass file_path = self.make_path("file.txt") self.create_file(file_path) @@ -2584,7 +2586,7 @@ def check_link_path_ending_with_sep(self, error): self.skip_if_symlink_not_supported() file_path = self.make_path("foo") link_path = self.make_path("link") - with self.open(file_path, "w"): + with self.open(file_path, "w", encoding="utf8"): self.assert_raises_os_error( error, self.os.link, file_path + self.os.sep, link_path ) @@ -2602,7 +2604,7 @@ def test_link_to_path_ending_with_sep_posix(self): self.check_posix_only() path0 = self.make_path("foo") + self.os.sep path1 = self.make_path("bar") - with self.open(path1, "w"): + with self.open(path1, "w", encoding="utf8"): self.assert_raises_os_error(errno.ENOENT, self.os.link, path1, path0) def test_link_to_path_ending_with_sep_windows(self): @@ -2610,14 +2612,14 @@ def test_link_to_path_ending_with_sep_windows(self): self.skip_if_symlink_not_supported() path0 = self.make_path("foo") + self.os.sep path1 = self.make_path("bar") - with self.open(path1, "w"): + with self.open(path1, "w", encoding="utf8"): self.os.link(path1, path0) self.assertTrue(self.os.path.exists(path1)) def check_rename_to_path_ending_with_sep(self, error): # Regression tests for #400 file_path = self.make_path("foo") - with self.open(file_path, "w"): + with self.open(file_path, "w", encoding="utf8"): self.assert_raises_os_error( error, self.os.rename, file_path + self.os.sep, file_path ) @@ -2708,7 +2710,7 @@ def test_link_delete(self): self.os.unlink(file1_path) # assert that second file exists, and its contents are the same self.assertTrue(self.os.path.exists(file2_path)) - with self.open(file2_path) as f: + with self.open(file2_path, encoding="utf8") as f: self.assertEqual(f.read(), contents1) def test_link_update(self): @@ -2722,13 +2724,13 @@ def test_link_update(self): self.create_file(file1_path, contents=contents1) self.os.link(file1_path, file2_path) # assert that the second file contains contents1 - with self.open(file2_path) as f: + with self.open(file2_path, encoding="utf8") as f: self.assertEqual(f.read(), contents1) # update the first file - with self.open(file1_path, "w") as f: + with self.open(file1_path, "w", encoding="utf8") as f: f.write(contents2) # assert that second file contains contents2 - with self.open(file2_path) as f: + with self.open(file2_path, encoding="utf8") as f: self.assertEqual(f.read(), contents2) def test_link_non_existent_parent(self): @@ -2879,11 +2881,11 @@ def test_open_umask_applied(self): self.check_posix_only() self.os.umask(0o22) file1 = self.make_path("file1") - self.open(file1, "w").close() + self.open(file1, "w", encoding="utf8").close() self.assert_mode_equal(0o644, self.os.stat(file1).st_mode) self.os.umask(0o27) file2 = self.make_path("file2") - self.open(file2, "w").close() + self.open(file2, "w", encoding="utf8").close() self.assert_mode_equal(0o640, self.os.stat(file2).st_mode) def test_open_pipe(self): @@ -2925,7 +2927,7 @@ def test_open_existing_pipe(self): fds.append(self.os.open(path, os.O_CREAT)) file_path = self.make_path("file.txt") self.create_file(file_path) - with self.open(file_path): + with self.open(file_path, encoding="utf8"): read_fd, write_fd = self.os.pipe() with self.open(write_fd, "wb") as f: self.assertEqual(4, f.write(b"test")) @@ -2955,7 +2957,7 @@ def test_truncate(self): file_path = self.make_path("foo", "bar") self.create_file(file_path, contents="012345678901234567") self.os.truncate(file_path, 10) - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: self.assertEqual("0123456789", f.read()) def test_truncate_non_existing(self): @@ -2967,7 +2969,7 @@ def test_truncate_to_larger(self): fd = self.os.open(file_path, os.O_RDWR) self.os.truncate(fd, 20) self.assertEqual(20, self.os.stat(file_path).st_size) - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: self.assertEqual("0123456789" + "\0" * 10, f.read()) def test_truncate_with_fd(self): @@ -2980,7 +2982,7 @@ def test_truncate_with_fd(self): fd = self.os.open(file_path, os.O_RDWR) self.os.truncate(fd, 10) self.assertEqual(10, self.os.stat(file_path).st_size) - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: self.assertEqual("0123456789", f.read()) def test_ftruncate(self): @@ -2994,7 +2996,7 @@ def test_ftruncate(self): fd = self.os.open(file_path, os.O_RDWR) self.os.truncate(fd, 10) self.assertEqual(10, self.os.stat(file_path).st_size) - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: self.assertEqual("0123456789", f.read()) def test_capabilities(self): @@ -3133,7 +3135,7 @@ def test_listdir_possible_without_exe_permission(self): with self.assertRaises(PermissionError): self.os.stat(file_path) with self.assertRaises(PermissionError): - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: f.read() def test_listdir_impossible_without_read_permission(self): @@ -3154,7 +3156,7 @@ def test_listdir_impossible_without_read_permission(self): self.os.scandir(directory) # we can access the file if we know the file name assert self.os.stat(file_path).st_mode & 0o777 == 0o755 - with self.open(file_path) as f: + with self.open(file_path, encoding="utf8") as f: assert f.read() == "hey" def test_fdopen_twice(self): @@ -3162,15 +3164,15 @@ def test_fdopen_twice(self): file_path2 = self.make_path("SOME_file1") self.create_file(file_path1, contents="contents here1") - fake_file1 = self.open(file_path2, "r") + fake_file1 = self.open(file_path2, "r", encoding="utf8") fileno1 = fake_file1.fileno() - self.os.fdopen(fileno1) + self.os.fdopen(fileno1, encoding="utf8") if not IS_PYPY: with self.assertRaises(OSError) as cm: - self.open(fileno1) + self.open(fileno1, encoding="utf8") self.assertEqual(errno.EBADF, cm.exception.errno) else: - self.open(fileno1) + self.open(fileno1, encoding="utf8") self.os.close(fileno1) def test_stat(self): @@ -3329,7 +3331,7 @@ def test_remove_open_file_fails_under_windows(self): self.check_windows_only() path = self.make_path("foo", "bar") self.create_file(path) - with self.open(path, "r"): + with self.open(path, "r", encoding="utf8"): self.assert_raises_os_error(errno.EACCES, self.os.remove, path.upper()) self.assertTrue(self.os.path.exists(path)) @@ -3337,7 +3339,7 @@ def test_remove_open_file_possible_under_posix(self): self.check_posix_only() path = self.make_path("foo", "bar") self.create_file(path) - self.open(path, "r") + self.open(path, "r", encoding="utf8") self.os.remove(path.upper()) self.assertFalse(self.os.path.exists(path)) @@ -3983,7 +3985,7 @@ def test_makedirs_exist_ok(self): def test_fsync_pass(self): test_file_path = self.make_path("test_file") self.create_file(test_file_path, contents="dummy file contents") - test_file = self.open(test_file_path.upper(), "r+") + test_file = self.open(test_file_path.upper(), "r+", encoding="utf8") test_fd = test_file.fileno() # Test that this doesn't raise anything self.os.fsync(test_fd) @@ -4030,7 +4032,7 @@ def test_link_delete(self): self.os.unlink(file1_path) # assert that second file exists, and its contents are the same self.assertTrue(self.os.path.exists(file2_path)) - with self.open(file2_path.upper()) as f: + with self.open(file2_path.upper(), encoding="utf8") as f: self.assertEqual(f.read(), contents1) def test_link_is_existing_file(self): @@ -4180,7 +4182,7 @@ def test_utime_uses_open_fd_as_path(self): path = self.make_path("some_file") self.createTestFile(path) - with FakeFileOpen(self.filesystem)(path) as f: + with FakeFileOpen(self.filesystem)(path, encoding="utf8") as f: self.os.utime(f.filedes, times=(1, 2)) st = self.os.stat(path) self.assertEqual(1, st.st_atime) @@ -4629,7 +4631,7 @@ def test_sendfile_no_offset(self): self.os.sendfile(fd2, fd1, 0, 3) self.os.close(fd2) self.os.close(fd1) - with self.open(dst_file_path) as f: + with self.open(dst_file_path, encoding="utf8") as f: self.assertEqual("tes", f.read()) def test_sendfile_with_offset(self): @@ -4643,7 +4645,7 @@ def test_sendfile_with_offset(self): self.os.sendfile(fd2, fd1, 4, 4) self.os.close(fd2) self.os.close(fd1) - with self.open(dst_file_path) as f: + with self.open(dst_file_path, encoding="utf8") as f: self.assertEqual("cont", f.read()) def test_sendfile_twice(self): @@ -4658,7 +4660,7 @@ def test_sendfile_twice(self): self.os.sendfile(fd2, fd1, 4, 4) self.os.close(fd2) self.os.close(fd1) - with self.open(dst_file_path) as f: + with self.open(dst_file_path, encoding="utf8") as f: self.assertEqual("contcont", f.read()) def test_sendfile_offset_none(self): @@ -4673,7 +4675,7 @@ def test_sendfile_offset_none(self): self.os.sendfile(fd2, fd1, None, 3) self.os.close(fd2) self.os.close(fd1) - with self.open(dst_file_path) as f: + with self.open(dst_file_path, encoding="utf8") as f: self.assertEqual("testcon", f.read()) @unittest.skipIf(not TestCase.is_macos, "Testing MacOs only behavior") @@ -5168,7 +5170,7 @@ def test_file_size_updated_via_close(self): file_path = "xyzzy/close" content = "This is a test." self.os.mkdir(file_dir) - fh = self.open(file_path, "w") + fh = self.open(file_path, "w", encoding="utf8") self.assertEqual(0, self.os.stat(file_path)[stat.ST_SIZE]) self.assertEqual("", self.filesystem.get_object(file_path).contents) fh.write(content) @@ -5186,9 +5188,9 @@ def test_file_size_not_reset_after_close(self): # The file has size, but no content. When the file is opened for # reading, its size should be preserved. self.filesystem.create_file(file_path, st_size=size) - fh = self.open(file_path, "r") + fh = self.open(file_path, "r", encoding="utf8") fh.close() - self.assertEqual(size, self.open(file_path, "r").size()) + self.assertEqual(size, self.open(file_path, "r", encoding="utf8").size()) def test_file_size_after_write(self): file_path = "test_file" @@ -5197,11 +5199,13 @@ def test_file_size_after_write(self): self.filesystem.create_file(file_path, contents=original_content) added_content = "foo bar" expected_size = original_size + len(added_content) - fh = self.open(file_path, "a") + fh = self.open(file_path, "a", encoding="utf8") fh.write(added_content) self.assertEqual(original_size, fh.size()) fh.close() - self.assertEqual(expected_size, self.open(file_path, "r").size()) + self.assertEqual( + expected_size, self.open(file_path, "r", encoding="utf8").size() + ) def test_large_file_size_after_write(self): file_path = "test_file" @@ -5209,7 +5213,7 @@ def test_large_file_size_after_write(self): original_size = len(original_content) self.filesystem.create_file(file_path, st_size=original_size) added_content = "foo bar" - fh = self.open(file_path, "a") + fh = self.open(file_path, "a", encoding="utf8") self.assertRaises( fake_file.FakeLargeFileIoException, lambda: fh.write(added_content), @@ -5222,7 +5226,7 @@ def test_file_size_updated_via_flush(self): file_path = self.os.path.join(file_dir, file_name) content = "This might be a test." self.os.mkdir(file_dir) - fh = self.open(file_path, "w") + fh = self.open(file_path, "w", encoding="utf8") self.assertEqual(0, self.os.stat(file_path)[stat.ST_SIZE]) self.assertEqual("", self.filesystem.get_object(file_path).contents) fh.write(content) @@ -5243,14 +5247,14 @@ def test_file_size_truncation(self): # pre-create file with content self.os.mkdir(file_dir) - fh = self.open(file_path, "w") + fh = self.open(file_path, "w", encoding="utf8") fh.write(content) fh.close() self.assertEqual(len(content), self.os.stat(file_path)[stat.ST_SIZE]) self.assertEqual(content, self.filesystem.get_object(file_path).contents) # test file truncation - fh = self.open(file_path, "w") + fh = self.open(file_path, "w", encoding="utf8") self.assertEqual(0, self.os.stat(file_path)[stat.ST_SIZE]) self.assertEqual("", self.filesystem.get_object(file_path).contents) fh.close() diff --git a/pyfakefs/tests/fake_pathlib_test.py b/pyfakefs/tests/fake_pathlib_test.py index 7c36aeab..e8544450 100644 --- a/pyfakefs/tests/fake_pathlib_test.py +++ b/pyfakefs/tests/fake_pathlib_test.py @@ -480,7 +480,7 @@ def test_iterdir_and_glob_without_exe_permission(self): with self.assertRaises(PermissionError): file_path.stat() with self.assertRaises(PermissionError): - file_path.read_text() + file_path.read_text(encoding="utf8") def test_iterdir_impossible_without_read_permission(self): # regression test for #960 @@ -498,7 +498,7 @@ def test_iterdir_impossible_without_read_permission(self): assert len(list(directory.glob("*.txt"))) == 0 # we can access the file if we know the file name assert file_path.stat().st_mode & 0o777 == 0o755 - assert file_path.read_text() == "hey" + assert file_path.read_text(encoding="utf8") == "hey" def test_resolve_nonexisting_file(self): path = self.path(self.make_path("/path", "to", "file", "this can not exist")) @@ -567,14 +567,14 @@ def test_exists(self): def test_open(self): self.create_dir(self.make_path("foo")) with self.assertRaises(OSError): - self.path(self.make_path("foo", "bar.txt")).open() - self.path(self.make_path("foo", "bar.txt")).open("w").close() + self.path(self.make_path("foo", "bar.txt")).open(encoding="utf8") + self.path(self.make_path("foo", "bar.txt")).open("w", encoding="utf8").close() self.assertTrue(self.os.path.exists(self.make_path("foo", "bar.txt"))) def test_read_text(self): self.create_file(self.make_path("text_file"), contents="foo") file_path = self.path(self.make_path("text_file")) - self.assertEqual(file_path.read_text(), "foo") + self.assertEqual(file_path.read_text(encoding="utf8"), "foo") @unittest.skipIf( sys.version_info < (3, 12), @@ -595,7 +595,7 @@ def test_read_text_with_encoding(self): def test_write_text(self): path_name = self.make_path("text_file") file_path = self.path(path_name) - file_path.write_text(str("foo")) + file_path.write_text("foo", encoding="utf8") self.assertTrue(self.os.path.exists(path_name)) self.check_contents(path_name, "foo") @@ -609,13 +609,13 @@ def test_write_text_with_encoding(self): @unittest.skipIf(sys.version_info < (3, 10), "newline argument new in Python 3.10") def test_write_with_newline_arg(self): path = self.path(self.make_path("some_file")) - path.write_text("1\r\n2\n3\r4", newline="") + path.write_text("1\r\n2\n3\r4", newline="", encoding="utf8") self.check_contents(path, b"1\r\n2\n3\r4") - path.write_text("1\r\n2\n3\r4", newline="\n") + path.write_text("1\r\n2\n3\r4", newline="\n", encoding="utf8") self.check_contents(path, b"1\r\n2\n3\r4") - path.write_text("1\r\n2\n3\r4", newline="\r\n") + path.write_text("1\r\n2\n3\r4", newline="\r\n", encoding="utf8") self.check_contents(path, b"1\r\r\n2\r\n3\r4") - path.write_text("1\r\n2\n3\r4", newline="\r") + path.write_text("1\r\n2\n3\r4", newline="\r", encoding="utf8") self.check_contents(path, b"1\r\r2\r3\r4") def test_read_bytes(self): diff --git a/pyfakefs/tests/fake_stat_time_test.py b/pyfakefs/tests/fake_stat_time_test.py index dfdbaaff..23e8d514 100644 --- a/pyfakefs/tests/fake_stat_time_test.py +++ b/pyfakefs/tests/fake_stat_time_test.py @@ -61,14 +61,14 @@ def assertLessExceptPosix(self, time1, time2): def open_close_new_file(self): with self.mock_time(): - with self.open(self.file_path, self.mode): + with self.open(self.file_path, self.mode, encoding="utf8"): created = self.stat_time(self.file_path) closed = self.stat_time(self.file_path) return created, closed def open_write_close_new_file(self): with self.mock_time(): - with self.open(self.file_path, self.mode) as f: + with self.open(self.file_path, self.mode, encoding="utf8") as f: created = self.stat_time(self.file_path) f.write("foo") written = self.stat_time(self.file_path) @@ -81,7 +81,7 @@ def open_close(self): self.create_file(self.file_path) before = self.stat_time(self.file_path) - with self.open(self.file_path, self.mode): + with self.open(self.file_path, self.mode, encoding="utf8"): opened = self.stat_time(self.file_path) closed = self.stat_time(self.file_path) @@ -92,7 +92,7 @@ def open_write_close(self): self.create_file(self.file_path) before = self.stat_time(self.file_path) - with self.open(self.file_path, self.mode) as f: + with self.open(self.file_path, self.mode, encoding="utf8") as f: opened = self.stat_time(self.file_path) f.write("foo") written = self.stat_time(self.file_path) @@ -105,7 +105,7 @@ def open_flush_close(self): self.create_file(self.file_path) before = self.stat_time(self.file_path) - with self.open(self.file_path, self.mode) as f: + with self.open(self.file_path, self.mode, encoding="utf8") as f: opened = self.stat_time(self.file_path) f.flush() flushed = self.stat_time(self.file_path) @@ -118,7 +118,7 @@ def open_write_flush(self): self.create_file(self.file_path) before = self.stat_time(self.file_path) - with self.open(self.file_path, self.mode) as f: + with self.open(self.file_path, self.mode, encoding="utf8") as f: opened = self.stat_time(self.file_path) f.write("foo") written = self.stat_time(self.file_path) @@ -133,7 +133,7 @@ def open_read_flush(self): self.create_file(self.file_path) before = self.stat_time(self.file_path) - with self.open(self.file_path, "r") as f: + with self.open(self.file_path, "r", encoding="utf8") as f: opened = self.stat_time(self.file_path) f.read() read = self.stat_time(self.file_path) @@ -145,7 +145,7 @@ def open_read_flush(self): def open_read_close_new_file(self): with self.mock_time(): - with self.open(self.file_path, self.mode) as f: + with self.open(self.file_path, self.mode, encoding="utf8") as f: created = self.stat_time(self.file_path) f.read() read = self.stat_time(self.file_path) @@ -158,7 +158,7 @@ def open_read_close(self): self.create_file(self.file_path) before = self.stat_time(self.file_path) - with self.open(self.file_path, self.mode) as f: + with self.open(self.file_path, self.mode, encoding="utf8") as f: opened = self.stat_time(self.file_path) f.read() read = self.stat_time(self.file_path) @@ -411,7 +411,7 @@ def test_open_write_flush_close(self): self.check_open_write_flush_close_w_mode() def test_read_raises(self): - with self.open(self.file_path, "w") as f: + with self.open(self.file_path, "w", encoding="utf8") as f: with self.assertRaises(OSError): f.read() @@ -498,7 +498,7 @@ def test_open_write_flush_close(self): self.check_open_write_flush_close_non_w_mode() def test_read_raises(self): - with self.open(self.file_path, "a") as f: + with self.open(self.file_path, "a", encoding="utf8") as f: with self.assertRaises(OSError): f.read() diff --git a/pyfakefs/tests/fake_tempfile_test.py b/pyfakefs/tests/fake_tempfile_test.py index f208b56f..09d6a4fe 100644 --- a/pyfakefs/tests/fake_tempfile_test.py +++ b/pyfakefs/tests/fake_tempfile_test.py @@ -44,7 +44,7 @@ def test_named_temporary_file_no_delete(self): file_obj = self.fs.get_object(obj.name) contents = file_obj.contents self.assertEqual("foo", contents) - obj = tempfile.NamedTemporaryFile(mode="w", delete=False) + obj = tempfile.NamedTemporaryFile(mode="w", encoding="utf8", delete=False) obj.write("foo") obj.close() file_obj = self.fs.get_object(obj.name) diff --git a/pyfakefs/tests/import_as_example.py b/pyfakefs/tests/import_as_example.py index e83f9602..835a2c4d 100644 --- a/pyfakefs/tests/import_as_example.py +++ b/pyfakefs/tests/import_as_example.py @@ -96,12 +96,12 @@ def system_stat(filepath): def file_contents1(filepath): - with bltn_open(filepath) as f: + with bltn_open(filepath, encoding="utf8") as f: return f.read() def file_contents2(filepath): - with io_open(filepath) as f: + with io_open(filepath, encoding="utf8") as f: return f.read() @@ -112,7 +112,7 @@ def exists_this_file(): def open_this_file(): """Works only in real fs""" - with open(__file__): + with open(__file__, encoding="utf8"): pass diff --git a/pyfakefs/tests/test_utils.py b/pyfakefs/tests/test_utils.py index 4d5ae07c..e28fe5a3 100644 --- a/pyfakefs/tests/test_utils.py +++ b/pyfakefs/tests/test_utils.py @@ -347,10 +347,13 @@ def create_file( """ self.create_dir(self.os.path.dirname(file_path)) mode = "wb" if encoding is not None or is_byte_string(contents) else "w" + kwargs = {"mode": mode} if encoding is not None and contents is not None: contents = contents.encode(encoding) - with self.open(file_path, mode) as f: + if mode == "w": + kwargs["encoding"] = "utf8" + with self.open(file_path, **kwargs) as f: if contents is not None: f.write(contents) if apply_umask: @@ -371,7 +374,10 @@ def check_contents(self, file_path, contents): Asserts equality. """ mode = "rb" if is_byte_string(contents) else "r" - with self.open(file_path, mode) as f: + kwargs = {"mode": mode} + if mode == "r": + kwargs["encoding"] = "utf8" + with self.open(file_path, **kwargs) as f: self.assertEqual(contents, f.read()) def create_basepath(self):