From 08fb7da0db7763f21d961149663e2a37b8b38138 Mon Sep 17 00:00:00 2001 From: Junya Okabe Date: Sun, 15 Oct 2023 04:42:15 +0900 Subject: [PATCH 1/9] feat: #110745: add newline args --- Lib/pathlib.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 5c1c71ecec2805..cf932ce8c98257 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -1023,12 +1023,12 @@ def read_bytes(self): 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): From 84fff0b684a8b9c609053d2380b4e81a3e8bdfce Mon Sep 17 00:00:00 2001 From: Junya Okabe Date: Sun, 15 Oct 2023 04:42:49 +0900 Subject: [PATCH 2/9] update: docs --- Doc/library/pathlib.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst index 8ee89a003a339a..08d6c70c4e89c5 100644 --- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -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.12 + The *newline* parameter was added. .. method:: Path.readlink() From 294fc402b10a6b00afcef5ec4f709898c5734bd8 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 15 Oct 2023 08:08:27 +0000 Subject: [PATCH 3/9] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-10-15-08-08-26.gh-issue-110745.mxEkh0.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-10-15-08-08-26.gh-issue-110745.mxEkh0.rst 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 index 00000000000000..0a4834f5bc92cb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-10-15-08-08-26.gh-issue-110745.mxEkh0.rst @@ -0,0 +1 @@ +Added *newline* parameter to ``pathlib.Path.read_text()``. From 3e690725dad2cf050471d7fa1ac571c877867433 Mon Sep 17 00:00:00 2001 From: Junya Okabe Date: Sat, 21 Oct 2023 18:57:35 +0000 Subject: [PATCH 4/9] add: unit test --- Lib/test/test_pathlib.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 2781e019220353..45de6426392c8d 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -1896,6 +1896,26 @@ def test_read_write_text(self): 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') + # Check that no argument passed will change `\n` to `os.linesep` + os_linesep = os.linesep + (p / 'fileA').write_bytes(b'abcde\nfghlk\n\rmnopq') + self.assertEqual((p / 'fileA').read_text(), + 'abcde' + os_linesep + 'fghlk' + os_linesep + '\nmnopq') + def test_write_text_with_newlines(self): p = self.cls(BASE) # Check that `\n` character change nothing From e13b75be9d136d9d314d5aa64e4ba70d36afb105 Mon Sep 17 00:00:00 2001 From: Junya Okabe Date: Tue, 24 Oct 2023 01:53:13 +0000 Subject: [PATCH 5/9] del: test case --- Lib/test/test_pathlib.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 45de6426392c8d..bfb7ab3a1f79b1 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -1910,11 +1910,6 @@ def test_read_text_with_newlines(self): (p / 'fileA').write_bytes(b'abcde\r\nfghlk\n\rmnopq') self.assertEqual((p / 'fileA').read_text(newline='\r\n'), 'abcde\r\nfghlk\n\rmnopq') - # Check that no argument passed will change `\n` to `os.linesep` - os_linesep = os.linesep - (p / 'fileA').write_bytes(b'abcde\nfghlk\n\rmnopq') - self.assertEqual((p / 'fileA').read_text(), - 'abcde' + os_linesep + 'fghlk' + os_linesep + '\nmnopq') def test_write_text_with_newlines(self): p = self.cls(BASE) From 18d829cf3e459df6aa8fc84fba3e2fe19ed70672 Mon Sep 17 00:00:00 2001 From: Junya Okabe Date: Tue, 31 Oct 2023 01:28:48 +0000 Subject: [PATCH 6/9] update: test --- Lib/test/test_pathlib.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index bfb7ab3a1f79b1..87f1f5fd2b13a5 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -1898,18 +1898,11 @@ def test_read_write_text(self): 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') + self.assertEqual(p.read_text(newline=None), 'abcde\nfghlk\n\nmnopq') + self.assertEqual(p.read_text(newline=''), 'abcde\r\nfghlk\n\rmnopq') + self.assertEqual(p.read_text(newline='\r'), 'abcde\r\nfghlk\n\rmnopq') + self.assertEqual(p.read_text(newline='\n'), 'abcde\r\nfghlk\n\rmnopq') + self.assertEqual(p.read_text(newline='\r\n'), 'abcde\r\nfghlk\n\rmnopq') def test_write_text_with_newlines(self): p = self.cls(BASE) From 07941c905e2a119f8c097cdacaf4f7bee0e833bc Mon Sep 17 00:00:00 2001 From: Junya Okabe Date: Tue, 31 Oct 2023 05:06:46 +0000 Subject: [PATCH 7/9] update: test --- Lib/test/test_pathlib.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 87f1f5fd2b13a5..5eee885feec333 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -1898,11 +1898,18 @@ def test_read_write_text(self): def test_read_text_with_newlines(self): p = self.cls(BASE) - self.assertEqual(p.read_text(newline=None), 'abcde\nfghlk\n\nmnopq') - self.assertEqual(p.read_text(newline=''), 'abcde\r\nfghlk\n\rmnopq') - self.assertEqual(p.read_text(newline='\r'), 'abcde\r\nfghlk\n\rmnopq') - self.assertEqual(p.read_text(newline='\n'), 'abcde\r\nfghlk\n\rmnopq') - self.assertEqual(p.read_text(newline='\r\n'), 'abcde\r\nfghlk\n\rmnopq') + # 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) From 5c199d1f4a2224963032762a29eeee6de4b9d027 Mon Sep 17 00:00:00 2001 From: Junya Okabe <86868255+Okabe-Junya@users.noreply.github.com> Date: Tue, 21 Nov 2023 13:49:22 +0900 Subject: [PATCH 8/9] update: Doc/library/pathlib.rst Co-authored-by: Barney Gale --- Doc/library/pathlib.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst index 08d6c70c4e89c5..7ecfd120db8d15 100644 --- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -1329,7 +1329,7 @@ call fails (for example because the path doesn't exist). .. versionadded:: 3.5 - .. versionchanged:: 3.12 + .. versionchanged:: 3.13 The *newline* parameter was added. .. method:: Path.readlink() From 2b974607701ffeedffc7598afdc85d047da51c88 Mon Sep 17 00:00:00 2001 From: Junya Okabe <86868255+Okabe-Junya@users.noreply.github.com> Date: Tue, 21 Nov 2023 13:50:09 +0900 Subject: [PATCH 9/9] update: Misc/NEWS.d/next/Library/2023-10-15-08-08-26.gh-issue-110745.mxEkh0.rst Co-authored-by: Barney Gale --- .../Library/2023-10-15-08-08-26.gh-issue-110745.mxEkh0.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index 0a4834f5bc92cb..b99f968dc20ae9 100644 --- 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 @@ -1 +1,2 @@ -Added *newline* parameter to ``pathlib.Path.read_text()``. +Added *newline* parameter to :meth:`pathlib.Path.read_text`. +Patch by Junya Okabe.