summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2014-02-02 22:37:29 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2014-02-02 22:37:29 (GMT)
commit57839a6349d7443855bc04d7f5c32f9f38929111 (patch)
tree86465f1dfcd80566048522ed571c46d950227c09
parent6bb21c48bcfac71a9c301b34b71e703dadd2e6ae (diff)
downloadcpython-57839a6349d7443855bc04d7f5c32f9f38929111.zip
cpython-57839a6349d7443855bc04d7f5c32f9f38929111.tar.gz
cpython-57839a6349d7443855bc04d7f5c32f9f38929111.tar.bz2
Issue #20435: Fix _pyio.StringIO.getvalue() to take into account newline translation settings.
-rw-r--r--Lib/_pyio.py8
-rw-r--r--Lib/test/test_memoryio.py9
-rw-r--r--Misc/NEWS3
3 files changed, 19 insertions, 1 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index a9b0064..9a2a1aa 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -2060,7 +2060,13 @@ class StringIO(TextIOWrapper):
def getvalue(self):
self.flush()
- return self.buffer.getvalue().decode(self._encoding, self._errors)
+ decoder = self._decoder or self._get_decoder()
+ old_state = decoder.getstate()
+ decoder.reset()
+ try:
+ return decoder.decode(self.buffer.getvalue(), final=True)
+ finally:
+ decoder.setstate(old_state)
def __repr__(self):
# TextIOWrapper tells the encoding in its repr. In StringIO,
diff --git a/Lib/test/test_memoryio.py b/Lib/test/test_memoryio.py
index 50c91ab..6a902b3 100644
--- a/Lib/test/test_memoryio.py
+++ b/Lib/test/test_memoryio.py
@@ -551,6 +551,7 @@ class TextIOTestMixin:
self.assertEqual(3, memio.write("c\rd"))
memio.seek(0)
self.assertEqual(memio.read(), "a\nb\nc\nd")
+ self.assertEqual(memio.getvalue(), "a\nb\nc\nd")
memio = self.ioclass("a\r\nb", newline=None)
self.assertEqual(memio.read(3), "a\nb")
@@ -562,6 +563,7 @@ class TextIOTestMixin:
self.assertEqual(memio.read(4), "a\nb\r")
self.assertEqual(memio.read(2), "\nc")
self.assertEqual(memio.read(1), "\r")
+ self.assertEqual(memio.getvalue(), "a\nb\r\nc\rd")
memio = self.ioclass(newline="")
self.assertEqual(2, memio.write("a\n"))
self.assertEqual(2, memio.write("b\r"))
@@ -581,6 +583,9 @@ class TextIOTestMixin:
self.assertEqual(memio.read(), "a\rb\r\rc\rd")
memio.seek(0)
self.assertEqual(list(memio), ["a\r", "b\r", "\r", "c\r", "d"])
+ memio.seek(0)
+ self.assertEqual(memio.readlines(), ["a\r", "b\r", "\r", "c\r", "d"])
+ self.assertEqual(memio.getvalue(), "a\rb\r\rc\rd")
def test_newline_crlf(self):
# newline="\r\n"
@@ -588,11 +593,15 @@ class TextIOTestMixin:
self.assertEqual(memio.read(), "a\r\nb\r\r\nc\rd")
memio.seek(0)
self.assertEqual(list(memio), ["a\r\n", "b\r\r\n", "c\rd"])
+ memio.seek(0)
+ self.assertEqual(memio.readlines(), ["a\r\n", "b\r\r\n", "c\rd"])
+ self.assertEqual(memio.getvalue(), "a\r\nb\r\r\nc\rd")
def test_issue5265(self):
# StringIO can duplicate newlines in universal newlines mode
memio = self.ioclass("a\r\nb\r\n", newline=None)
self.assertEqual(memio.read(5), "a\nb\n")
+ self.assertEqual(memio.getvalue(), "a\nb\n")
def test_newline_argument(self):
self.assertRaises(TypeError, self.ioclass, newline=b"\n")
diff --git a/Misc/NEWS b/Misc/NEWS
index eccd615..6613727 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -45,6 +45,9 @@ Core and Builtins
Library
-------
+- Issue #20435: Fix _pyio.StringIO.getvalue() to take into account newline
+ translation settings.
+
- Issue #20288: fix handling of invalid numeric charrefs in HTMLParser.
- Issue #20424: Python implementation of io.StringIO now supports lone surrogates.