]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-110745: add a newline argument to pathlib.Path.read_text (#110880)
authorJunya Okabe <86868255+Okabe-Junya@users.noreply.github.com>
Tue, 21 Nov 2023 22:32:38 +0000 (07:32 +0900)
committerGitHub <noreply@github.com>
Tue, 21 Nov 2023 22:32:38 +0000 (22:32 +0000)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Barney Gale <barney.gale@gmail.com>
Doc/library/pathlib.rst
Lib/pathlib.py
Lib/test/test_pathlib.py
Misc/NEWS.d/next/Library/2023-10-15-08-08-26.gh-issue-110745.mxEkh0.rst [new file with mode: 0644]

index 8ee89a003a339aadbc95c12ec2d692589ed3b205..7ecfd120db8d154837c805540fcdbff672ad98f3 100644 (file)
@@ -1314,7 +1314,7 @@ call fails (for example because the path doesn't exist).
    .. versionadded:: 3.5
 
 
-.. method:: Path.read_text(encoding=None, errors=None)
+.. method:: Path.read_text(encoding=None, errors=None, newline=None)
 
    Return the decoded contents of the pointed-to file as a string::
 
@@ -1329,6 +1329,8 @@ call fails (for example because the path doesn't exist).
 
    .. versionadded:: 3.5
 
+   .. versionchanged:: 3.13
+      The *newline* parameter was added.
 
 .. method:: Path.readlink()
 
index 73f87b9acbf9d526a6eccd7989d3065efbf6fa32..9bce5320ef68e9608a7cf5c37409bbf79bf19bec 100644 (file)
@@ -950,12 +950,12 @@ class _PathBase(PurePath):
         with self.open(mode='rb') as f:
             return f.read()
 
-    def read_text(self, encoding=None, errors=None):
+    def read_text(self, encoding=None, errors=None, newline=None):
         """
         Open the file in text mode, read it, and close the file.
         """
         encoding = io.text_encoding(encoding)
-        with self.open(mode='r', encoding=encoding, errors=errors) as f:
+        with self.open(mode='r', encoding=encoding, errors=errors, newline=newline) as f:
             return f.read()
 
     def write_bytes(self, data):
index 7083e9ebba669059ef51f3d3a21b338f3d7326f8..e1121a9d76c0405ed9a6137e6f396cc238e490be 100644 (file)
@@ -1878,6 +1878,21 @@ class DummyPathTest(unittest.TestCase):
         self.assertRaises(TypeError, (p / 'fileA').write_text, b'somebytes')
         self.assertEqual((p / 'fileA').read_text(encoding='latin-1'), 'äbcdefg')
 
+    def test_read_text_with_newlines(self):
+        p = self.cls(BASE)
+        # Check that `\n` character change nothing
+        (p / 'fileA').write_bytes(b'abcde\r\nfghlk\n\rmnopq')
+        self.assertEqual((p / 'fileA').read_text(newline='\n'),
+                         'abcde\r\nfghlk\n\rmnopq')
+        # Check that `\r` character replaces `\n`
+        (p / 'fileA').write_bytes(b'abcde\r\nfghlk\n\rmnopq')
+        self.assertEqual((p / 'fileA').read_text(newline='\r'),
+                         'abcde\r\nfghlk\n\rmnopq')
+        # Check that `\r\n` character replaces `\n`
+        (p / 'fileA').write_bytes(b'abcde\r\nfghlk\n\rmnopq')
+        self.assertEqual((p / 'fileA').read_text(newline='\r\n'),
+                             'abcde\r\nfghlk\n\rmnopq')
+
     def test_write_text_with_newlines(self):
         p = self.cls(BASE)
         # Check that `\n` character change nothing
diff --git a/Misc/NEWS.d/next/Library/2023-10-15-08-08-26.gh-issue-110745.mxEkh0.rst b/Misc/NEWS.d/next/Library/2023-10-15-08-08-26.gh-issue-110745.mxEkh0.rst
new file mode 100644 (file)
index 0000000..b99f968
--- /dev/null
@@ -0,0 +1,2 @@
+Added *newline* parameter to :meth:`pathlib.Path.read_text`.
+Patch by Junya Okabe.