]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-23706: Add newline parameter to pathlib.Path.write_text (GH-22420) (GH-22420)
authorМаксим <maksvenberv@yandex.ru>
Wed, 21 Oct 2020 02:08:19 +0000 (05:08 +0300)
committerGitHub <noreply@github.com>
Wed, 21 Oct 2020 02:08:19 +0000 (19:08 -0700)
* Add _newline_ parameter to `pathlib.Path.write_text()`
* Update documentation of `pathlib.Path.write_text()`
* Add test case for `pathlib.Path.write_text()` calls with _newline_ parameter passed

Automerge-Triggered-By: GH:methane
Doc/library/pathlib.rst
Lib/pathlib.py
Lib/test/test_pathlib.py
Misc/NEWS.d/next/Library/2020-09-30-11-05-11.bpo-23706.dHTGjF.rst [new file with mode: 0644]

index 9526a03b053986c1b4a7ae38560c71b42a385dd0..9de72bb725c75832cf7aafed534ff35b29a0f388 100644 (file)
@@ -1166,7 +1166,7 @@ call fails (for example because the path doesn't exist).
    .. versionadded:: 3.5
 
 
-.. method:: Path.write_text(data, encoding=None, errors=None)
+.. method:: Path.write_text(data, encoding=None, errors=None, newline=None)
 
    Open the file pointed to in text mode, write *data* to it, and close the
    file::
@@ -1182,6 +1182,9 @@ call fails (for example because the path doesn't exist).
 
    .. versionadded:: 3.5
 
+   .. versionchanged:: 3.10
+      The *newline* parameter was added.
+
 Correspondence to tools in the :mod:`os` module
 -----------------------------------------------
 
index 147be2ff0dddfccba0a7969140eafbffc1cfedbe..178c5b981d8e503eb633fd0f78548a331ed86755 100644 (file)
@@ -1264,14 +1264,14 @@ class Path(PurePath):
         with self.open(mode='wb') as f:
             return f.write(view)
 
-    def write_text(self, data, encoding=None, errors=None):
+    def write_text(self, data, encoding=None, errors=None, newline=None):
         """
         Open the file in text mode, write to it, and close the file.
         """
         if not isinstance(data, str):
             raise TypeError('data must be str, not %s' %
                             data.__class__.__name__)
-        with self.open(mode='w', encoding=encoding, errors=errors) as f:
+        with self.open(mode='w', encoding=encoding, errors=errors, newline=newline) as f:
             return f.write(data)
 
     def readlink(self):
index 2cb6738a295b649203d44281cae944fbd13965b4..17292dc1abf73ff08dc66764996fbfcb7613941b 100644 (file)
@@ -1510,6 +1510,26 @@ class _BasePathTest(object):
         self.assertRaises(TypeError, (p / 'fileA').write_text, b'somebytes')
         self.assertEqual((p / 'fileA').read_text(encoding='latin-1'), 'äbcdefg')
 
+    def test_write_text_with_newlines(self):
+        p = self.cls(BASE)
+        # Check that `\n` character change nothing
+        (p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\n')
+        self.assertEqual((p / 'fileA').read_bytes(),
+                         b'abcde\r\nfghlk\n\rmnopq')
+        # Check that `\r` character replaces `\n`
+        (p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\r')
+        self.assertEqual((p / 'fileA').read_bytes(),
+                         b'abcde\r\rfghlk\r\rmnopq')
+        # Check that `\r\n` character replaces `\n`
+        (p / 'fileA').write_text('abcde\r\nfghlk\n\rmnopq', newline='\r\n')
+        self.assertEqual((p / 'fileA').read_bytes(),
+                         b'abcde\r\r\nfghlk\r\n\rmnopq')
+        # Check that no argument passed will change `\n` to `os.linesep`
+        os_linesep_byte = bytes(os.linesep, encoding='ascii')
+        (p / 'fileA').write_text('abcde\nfghlk\n\rmnopq')
+        self.assertEqual((p / 'fileA').read_bytes(),
+                          b'abcde' + os_linesep_byte + b'fghlk' + os_linesep_byte + b'\rmnopq')
+
     def test_iterdir(self):
         P = self.cls
         p = P(BASE)
diff --git a/Misc/NEWS.d/next/Library/2020-09-30-11-05-11.bpo-23706.dHTGjF.rst b/Misc/NEWS.d/next/Library/2020-09-30-11-05-11.bpo-23706.dHTGjF.rst
new file mode 100644 (file)
index 0000000..b9a69a5
--- /dev/null
@@ -0,0 +1 @@
+Added *newline* parameter to ``pathlib.Path.write_text()``.