diff options
author | Georg Brandl <georg@python.org> | 2014-10-01 17:12:33 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2014-10-01 17:12:33 (GMT) |
commit | ea6839835557784433669a43c763c296ce9afd21 (patch) | |
tree | e2fe412eb90b05e915de41ae6c6aecbab2e6870d /Lib/test | |
parent | 5c4725e5bc4c25ff3f9771bf8985bcb52fea23e7 (diff) | |
download | cpython-ea6839835557784433669a43c763c296ce9afd21.zip cpython-ea6839835557784433669a43c763c296ce9afd21.tar.gz cpython-ea6839835557784433669a43c763c296ce9afd21.tar.bz2 |
Closes #20218: Added convenience methods read_text/write_text and read_bytes/
write_bytes to pathlib.Path objects.
Thanks to Christopher Welborn and Ram Rachum for original patches.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_pathlib.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 4f76217..8839888 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -1310,6 +1310,23 @@ class _BasePathTest(object): self.assertIsInstance(f, io.RawIOBase) self.assertEqual(f.read().strip(), b"this is file A") + def test_read_write_bytes(self): + p = self.cls(BASE) + (p / 'fileA').write_bytes(b'abcdefg') + self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg') + # check that trying to write str does not truncate the file + self.assertRaises(TypeError, (p / 'fileA').write_bytes, 'somestr') + self.assertEqual((p / 'fileA').read_bytes(), b'abcdefg') + + def test_read_write_text(self): + p = self.cls(BASE) + (p / 'fileA').write_text('äbcdefg', encoding='latin-1') + self.assertEqual((p / 'fileA').read_text( + encoding='utf-8', errors='ignore'), 'bcdefg') + # check that trying to write bytes does not truncate the file + self.assertRaises(TypeError, (p / 'fileA').write_text, b'somebytes') + self.assertEqual((p / 'fileA').read_text(encoding='latin-1'), 'äbcdefg') + def test_iterdir(self): P = self.cls p = P(BASE) |