diff options
author | Guido van Rossum <guido@python.org> | 2007-04-07 02:59:27 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-04-07 02:59:27 (GMT) |
commit | 9be5597abfeaa13a1b79c1d888ce6f120458b6ae (patch) | |
tree | 58c1cc812d9a8dbd3d0dc1788b39863809a84508 | |
parent | 76c5d4d72d284125d62f8688e3e2654e01e35ad6 (diff) | |
download | cpython-9be5597abfeaa13a1b79c1d888ce6f120458b6ae.zip cpython-9be5597abfeaa13a1b79c1d888ce6f120458b6ae.tar.gz cpython-9be5597abfeaa13a1b79c1d888ce6f120458b6ae.tar.bz2 |
Add some backwards compatibility stuff.
This now appears to work when io.open is substituted for the real open
in fileinput.py -- at least the latter's unit tests pass.
-rw-r--r-- | Lib/io.py | 33 |
1 files changed, 29 insertions, 4 deletions
@@ -54,6 +54,7 @@ def open(filename, mode="r", buffering=None, *, encoding=None): 'b': binary mode 't': text mode (default) '+': open a disk file for updating (implies reading and writing) + 'U': universal newline mode (for backwards compatibility) Constraints: - encoding must not be given when a binary mode is given @@ -64,12 +65,12 @@ def open(filename, mode="r", buffering=None, *, encoding=None): binary stream, a buffered binary stream, or a buffered text stream, open for reading and/or writing. """ - assert isinstance(filename, str) - assert isinstance(mode, str) + assert isinstance(filename, basestring) + assert isinstance(mode, basestring) assert buffering is None or isinstance(buffering, int) - assert encoding is None or isinstance(encoding, str) + assert encoding is None or isinstance(encoding, basestring) modes = set(mode) - if modes - set("arwb+t") or len(mode) > len(modes): + if modes - set("arwb+tU") or len(mode) > len(modes): raise ValueError("invalid mode: %r" % mode) reading = "r" in modes writing = "w" in modes @@ -77,6 +78,8 @@ def open(filename, mode="r", buffering=None, *, encoding=None): updating = "+" in modes text = "t" in modes binary = "b" in modes + if not reading and not writing and not appending and "U" in modes: + reading = True if text and binary: raise ValueError("can't have text and binary mode at once") if reading + writing + appending > 1: @@ -716,6 +719,25 @@ class TextIOBase(BufferedIOBase): raise StopIteration return line + # The following are provided for backwards compatibility + + def readlines(self, hint=None): + if hint is None: + return list(self) + n = 0 + lines = [] + while not lines or n < hint: + line = self.readline() + if not line: + break + lines.append(line) + n += len(line) + return lines + + def writelines(self, lines): + for line in lines: + self.write(line) + class TextIOWrapper(TextIOBase): @@ -742,6 +764,9 @@ class TextIOWrapper(TextIOBase): self._decoder = None self._pending = '' + def fileno(self): + return self.buffer.fileno() + def write(self, s: str): return self.buffer.write(s.encode(self._encoding)) |