Mercurial > cpython-lru_cache-weakref
diff Lib/idlelib/PyShell.py @ 81718:def4cd7e0a9f 2.7
Issue #9290: In IDLE the sys.std* streams now implement io.TextIOBase
interface and support all mandatory methods and properties.
| author | Serhiy Storchaka <storchaka@gmail.com> |
|---|---|
| date | Fri, 25 Jan 2013 15:30:35 +0200 |
| parents | 639dd6e62de4 |
| children | 225022955c65 |
line wrap: on
line diff
--- a/Lib/idlelib/PyShell.py Fri Jan 25 13:08:27 2013 +0100 +++ b/Lib/idlelib/PyShell.py Fri Jan 25 15:30:35 2013 +0200 @@ -428,10 +428,8 @@ except socket.timeout, err: self.display_no_subprocess_error() return None - # Can't regiter self.tkconsole.stdin, since run.py wants to - # call non-TextIO methods on it (such as getvar) - # XXX should be renamed to "console" - self.rpcclt.register("stdin", self.tkconsole) + self.rpcclt.register("console", self.tkconsole) + self.rpcclt.register("stdin", self.tkconsole.stdin) self.rpcclt.register("stdout", self.tkconsole.stdout) self.rpcclt.register("stderr", self.tkconsole.stderr) self.rpcclt.register("flist", self.tkconsole.flist) @@ -884,10 +882,10 @@ self.save_stderr = sys.stderr self.save_stdin = sys.stdin from idlelib import IOBinding - self.stdin = PseudoInputFile(self) - self.stdout = PseudoFile(self, "stdout", IOBinding.encoding) - self.stderr = PseudoFile(self, "stderr", IOBinding.encoding) - self.console = PseudoFile(self, "console", IOBinding.encoding) + self.stdin = PseudoInputFile(self, "stdin", IOBinding.encoding) + self.stdout = PseudoOutputFile(self, "stdout", IOBinding.encoding) + self.stderr = PseudoOutputFile(self, "stderr", IOBinding.encoding) + self.console = PseudoOutputFile(self, "console", IOBinding.encoding) if not use_subprocess: sys.stdout = self.stdout sys.stderr = self.stderr @@ -1279,37 +1277,83 @@ return 'disabled' return super(PyShell, self).rmenu_check_paste() -class PseudoFile(object): +class PseudoFile(io.TextIOBase): def __init__(self, shell, tags, encoding=None): self.shell = shell self.tags = tags self.softspace = 0 - self.encoding = encoding - - def write(self, s): - if not isinstance(s, (basestring, bytearray)): - raise TypeError('must be string, not ' + type(s).__name__) - self.shell.write(s, self.tags) + self._encoding = encoding - def writelines(self, lines): - for line in lines: - self.write(line) + @property + def encoding(self): + return self._encoding - def flush(self): - pass + @property + def name(self): + return '<%s>' % self.tags def isatty(self): return True -class PseudoInputFile(object): - def __init__(self, shell): - self.readline = shell.readline - self.isatty = shell.isatty + +class PseudoOutputFile(PseudoFile): + + def writable(self): + return True def write(self, s): - raise io.UnsupportedOperation("not writable") - writelines = write + if self.closed: + raise ValueError("write to closed file") + if not isinstance(s, (basestring, bytearray)): + raise TypeError('must be string, not ' + type(s).__name__) + return self.shell.write(s, self.tags) + + +class PseudoInputFile(PseudoFile): + + def __init__(self, shell, tags, encoding=None): + PseudoFile.__init__(self, shell, tags, encoding) + self._line_buffer = '' + + def readable(self): + return True + + def read(self, size=-1): + if self.closed: + raise ValueError("read from closed file") + if size is None: + size = -1 + elif not isinstance(size, int): + raise TypeError('must be int, not ' + type(size).__name__) + result = self._line_buffer + self._line_buffer = '' + if size < 0: + while True: + line = self.shell.readline() + if not line: break + result += line + else: + while len(result) < size: + line = self.shell.readline() + if not line: break + result += line + self._line_buffer = result[size:] + result = result[:size] + return result + + def readline(self, size=-1): + if self.closed: + raise ValueError("read from closed file") + if size is None: + size = -1 + elif not isinstance(size, int): + raise TypeError('must be int, not ' + type(size).__name__) + line = self._line_buffer or self.shell.readline() + if size < 0: + size = len(line) + self._line_buffer = line[size:] + return line[:size] usage_msg = """\
