Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2778,7 +2778,8 @@ def readline(self, size=None):

@property
def newlines(self):
return self._decoder.newlines if self._decoder else None
# Without universal newlines, the decoder has no newlines attribute.
return getattr(self._decoder, "newlines", None)

def _dealloc_warn(self, source):
if dealloc_warn := getattr(self.buffer, "_dealloc_warn", None):
Expand Down
6 changes: 6 additions & 0 deletions Lib/test/test_io/test_memoryio.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,12 @@ def force_decode():
force_decode()
self.assertEqual(memio.newlines, ("\r", "\n", "\r\n"))

def test_newlines_property_without_universal_newlines(self):
for newline in ("\n", "\r", "\r\n"):
memio = self.ioclass("a\r\nb\rc\n", newline=newline)
memio.read()
self.assertIsNone(memio.newlines)

def test_relative_seek(self):
memio = self.ioclass()

Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_io/test_textio.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ def test_newlines_input(self):
buf = self.BytesIO(testdata)
txt = self.TextIOWrapper(buf, encoding="ascii", newline=newline)
self.assertEqual(txt.readlines(), expected)
self.assertEqual(txt.newlines,
None if newline else ("\r", "\n", "\r\n"))
txt.seek(0)
self.assertEqual(txt.read(), "".join(expected))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :attr:`!_pyio.TextIOWrapper.newlines` raising :exc:`AttributeError`
instead of returning ``None`` when *newline* is ``'\n'``, ``'\r'`` or
``'\r\n'``.
Loading