Issue #9290: In IDLE the sys.std* streams now implement io.TextIOBase

interface and support all mandatory methods and properties.
This commit is contained in:
Serhiy Storchaka 2013-01-25 15:30:58 +02:00
parent 3e6e2ac31d
commit 39e70a4e83
3 changed files with 83 additions and 73 deletions

View file

@ -15,6 +15,8 @@ from idlelib import RemoteDebugger
from idlelib import RemoteObjectBrowser
from idlelib import StackViewer
from idlelib import rpc
from idlelib import PyShell
from idlelib import IOBinding
import __main__
@ -262,62 +264,23 @@ class MyRPCServer(rpc.RPCServer):
quitting = True
thread.interrupt_main()
class _RPCFile(io.TextIOBase):
"""Wrapper class for the RPC proxy to typecheck arguments
that may not support pickling. The base class is there only
to support type tests; all implementations come from the remote
object."""
def __init__(self, rpc):
super.__setattr__(self, 'rpc', rpc)
def __getattribute__(self, name):
# When accessing the 'rpc' attribute, or 'write', use ours
if name in ('rpc', 'write', 'writelines'):
return io.TextIOBase.__getattribute__(self, name)
# Else only look into the remote object only
return getattr(self.rpc, name)
def __setattr__(self, name, value):
return setattr(self.rpc, name, value)
@staticmethod
def _ensure_string(func):
def f(self, s):
if not isinstance(s, str):
raise TypeError('must be str, not ' + type(s).__name__)
return func(self, s)
return f
class _RPCOutputFile(_RPCFile):
@_RPCFile._ensure_string
def write(self, s):
if not isinstance(s, str):
raise TypeError('must be str, not ' + type(s).__name__)
return self.rpc.write(s)
class _RPCInputFile(_RPCFile):
@_RPCFile._ensure_string
def write(self, s):
raise io.UnsupportedOperation("not writable")
writelines = write
class MyHandler(rpc.RPCHandler):
def handle(self):
"""Override base method"""
executive = Executive(self)
self.register("exec", executive)
self.console = self.get_remote_proxy("stdin")
sys.stdin = _RPCInputFile(self.console)
sys.stdout = _RPCOutputFile(self.get_remote_proxy("stdout"))
sys.stderr = _RPCOutputFile(self.get_remote_proxy("stderr"))
self.console = self.get_remote_proxy("console")
sys.stdin = PyShell.PseudoInputFile(self.console, "stdin",
IOBinding.encoding)
sys.stdout = PyShell.PseudoOutputFile(self.console, "stdout",
IOBinding.encoding)
sys.stderr = PyShell.PseudoOutputFile(self.console, "stderr",
IOBinding.encoding)
# page help() text to shell.
import pydoc # import must be done here to capture i/o binding
pydoc.pager = pydoc.plainpager
from idlelib import IOBinding
sys.stdin.encoding = sys.stdout.encoding = \
sys.stderr.encoding = IOBinding.encoding
self.interp = self.get_remote_proxy("interp")
rpc.RPCHandler.getresponse(self, myseq=None, wait=0.05)