Skip to content

Commit 41f1dc8

Browse files
committed
Add "newline" argument to PathPlus.write_text to match Python 3.10
1 parent f668f0a commit 41f1dc8

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

domdf_python_tools/paths.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ def write_text(
488488
data: str,
489489
encoding: Optional[str] = "UTF-8",
490490
errors: Optional[str] = None,
491+
newline: Optional[str] = NEWLINE_DEFAULT,
491492
) -> int:
492493
"""
493494
Open the file in text mode, write to it, and close the file.
@@ -497,9 +498,20 @@ def write_text(
497498
:param data:
498499
:param encoding: The encoding to write to the file in.
499500
:param errors:
501+
:param newline:
502+
:default newline: `universal newlines <https://docs.python.org/3/glossary.html#term-universal-newlines>`__ for reading, Unix line endings (``LF``) for writing.
503+
504+
.. versionchanged:: 3.1.0
505+
506+
Added the ``newline`` argument to match Python 3.10.
507+
(see :github:pull:`22420 <python/cpython>`)
500508
"""
501509

502-
return super().write_text(data, encoding=encoding, errors=errors)
510+
if not isinstance(data, str):
511+
raise TypeError(f'data must be str, not {data.__class__.__name__}')
512+
513+
with self.open(mode='w', encoding=encoding, errors=errors, newline=newline) as f:
514+
return f.write(data)
503515

504516
def write_lines(
505517
self,

tests/test_paths.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,3 +927,35 @@ def test_sort_paths():
927927
@pytest.mark.parametrize("left_type", [pathlib.PurePath, pathlib.Path, PathPlus])
928928
def test_pathplus_from_uri(path: str, left_type: Type):
929929
assert PathPlus.from_uri(left_type(path).as_uri()).as_posix() == path
930+
931+
932+
def test_write_text_line_endings(tmp_pathplus: PathPlus):
933+
the_file = (tmp_pathplus / "foo.md")
934+
the_file.write_text("Hello\nWorld")
935+
assert the_file.read_bytes() == b"Hello\nWorld"
936+
937+
with the_file.open('w') as fp:
938+
fp.write("Hello\nWorld")
939+
940+
assert the_file.read_bytes() == b"Hello\nWorld"
941+
942+
with the_file.open('w', newline="\r\n") as fp:
943+
fp.write("Hello\nWorld")
944+
945+
assert the_file.read_bytes() == b"Hello\r\nWorld"
946+
947+
# The following from https://github.com/python/cpython/pull/22420/files
948+
949+
# Check that `\n` character change nothing
950+
the_file.write_text('abcde\r\nfghlk\n\rmnopq', newline='\n')
951+
assert the_file.read_bytes() == b'abcde\r\nfghlk\n\rmnopq'
952+
# Check that `\r` character replaces `\n`
953+
the_file.write_text('abcde\r\nfghlk\n\rmnopq', newline='\r')
954+
assert the_file.read_bytes() == b'abcde\r\rfghlk\r\rmnopq'
955+
# Check that `\r\n` character replaces `\n`
956+
the_file.write_text('abcde\r\nfghlk\n\rmnopq', newline='\r\n')
957+
assert the_file.read_bytes() == b'abcde\r\r\nfghlk\r\n\rmnopq'
958+
# Check that no argument passed will change `\n` to `os.linesep`
959+
os_linesep_byte = bytes(os.linesep, encoding="ascii")
960+
the_file.write_text('abcde\nfghlk\n\rmnopq')
961+
assert the_file.read_bytes() == b'abcde' + os_linesep_byte + b'fghlk' + os_linesep_byte + b'\rmnopq'

0 commit comments

Comments
 (0)