summaryrefslogtreecommitdiff
path: root/Lib/test/test_pathlib.py
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2014-10-01 19:12:33 +0200
committerGeorg Brandl <georg@python.org>2014-10-01 19:12:33 +0200
commit46b4370c701955798e6351fcb051af18c60faa41 (patch)
treec92830f19e856a42364965a75a3446c41aab7f5f /Lib/test/test_pathlib.py
parentd28cdf518f690fccb7a685420b872619532019aa (diff)
downloadcpython-46b4370c701955798e6351fcb051af18c60faa41.tar.gz
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/test_pathlib.py')
-rw-r--r--Lib/test/test_pathlib.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 4f762176ba..8839888c6a 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)