Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(2)

Side by Side Diff: Lib/_pyio.py

Issue 20435: Discrepancy between io.StringIO and _pyio.StringIO with univeral newlines
Patch Set: Created 5 years, 12 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Lib/test/test_memoryio.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """ 1 """
2 Python implementation of the io module. 2 Python implementation of the io module.
3 """ 3 """
4 4
5 import os 5 import os
6 import abc 6 import abc
7 import codecs 7 import codecs
8 import errno 8 import errno
9 # Import _thread instead of threading to reduce startup cost 9 # Import _thread instead of threading to reduce startup cost
10 try: 10 try:
(...skipping 2032 matching lines...) Expand 10 before | Expand all | Expand 10 after
2043 2043
2044 2044
2045 class StringIO(TextIOWrapper): 2045 class StringIO(TextIOWrapper):
2046 """Text I/O implementation using an in-memory buffer. 2046 """Text I/O implementation using an in-memory buffer.
2047 2047
2048 The initial_value argument sets the value of object. The newline 2048 The initial_value argument sets the value of object. The newline
2049 argument is like the one of TextIOWrapper's constructor. 2049 argument is like the one of TextIOWrapper's constructor.
2050 """ 2050 """
2051 2051
2052 def __init__(self, initial_value="", newline="\n"): 2052 def __init__(self, initial_value="", newline="\n"):
2053 super(StringIO, self).__init__(BytesIO(), 2053 if initial_value is not None:
2054 if not isinstance(initial_value, str):
2055 raise TypeError("initial_value must be str or None, not {0}"
2056 .format(type(initial_value).__name__))
2057 initial_value = initial_value.encode('utf-8', 'surrogatepass')
2058 else:
2059 initial_value = b''
2060 super(StringIO, self).__init__(BytesIO(initial_value),
2054 encoding="utf-8", 2061 encoding="utf-8",
2055 errors="surrogatepass", 2062 errors="surrogatepass",
2056 newline=newline) 2063 newline=newline)
2057 # Issue #5645: make universal newlines semantics the same as in the 2064 # Issue #5645: make universal newlines semantics the same as in the
2058 # C version, even under Windows. 2065 # C version, even under Windows.
2059 if newline is None: 2066 if newline is None:
2060 self._writetranslate = False 2067 self._writetranslate = False
2061 if initial_value is not None:
2062 if not isinstance(initial_value, str):
2063 raise TypeError("initial_value must be str or None, not {0}"
2064 .format(type(initial_value).__name__))
2065 self.write(initial_value)
2066 self.seek(0)
2067 2068
2068 def getvalue(self): 2069 def getvalue(self):
2069 self.flush() 2070 self.flush()
2070 return self.buffer.getvalue().decode(self._encoding, self._errors) 2071 return self.buffer.getvalue().decode(self._encoding, self._errors)
2071 2072
2072 def __repr__(self): 2073 def __repr__(self):
2073 # TextIOWrapper tells the encoding in its repr. In StringIO, 2074 # TextIOWrapper tells the encoding in its repr. In StringIO,
2074 # that's a implementation detail. 2075 # that's a implementation detail.
2075 return object.__repr__(self) 2076 return object.__repr__(self)
2076 2077
2077 @property 2078 @property
2078 def errors(self): 2079 def errors(self):
2079 return None 2080 return None
2080 2081
2081 @property 2082 @property
2082 def encoding(self): 2083 def encoding(self):
2083 return None 2084 return None
2084 2085
2085 def detach(self): 2086 def detach(self):
2086 # This doesn't make sense on StringIO. 2087 # This doesn't make sense on StringIO.
2087 self._unsupported("detach") 2088 self._unsupported("detach")
OLDNEW
« no previous file with comments | « no previous file | Lib/test/test_memoryio.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+