diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2012-07-25 08:47:20 (GMT) |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2012-07-25 08:47:20 (GMT) |
commit | c882b7c51ab4b76bdf44cf4dab5ca1b55ccb2155 (patch) | |
tree | 5886ffcc7f01739b27f72b807c69601773d33ab7 /Lib/idlelib/run.py | |
parent | 186396dce60feb6c215fbf5a3763baeebe8d8b0d (diff) | |
download | cpython-c882b7c51ab4b76bdf44cf4dab5ca1b55ccb2155.zip cpython-c882b7c51ab4b76bdf44cf4dab5ca1b55ccb2155.tar.gz cpython-c882b7c51ab4b76bdf44cf4dab5ca1b55ccb2155.tar.bz2 |
Issue #15318: Prevent writing to sys.stdin.
Patch by Roger Serwy and myself.
Diffstat (limited to 'Lib/idlelib/run.py')
-rw-r--r-- | Lib/idlelib/run.py | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/Lib/idlelib/run.py b/Lib/idlelib/run.py index cbfa8f8..5365680 100644 --- a/Lib/idlelib/run.py +++ b/Lib/idlelib/run.py @@ -256,7 +256,7 @@ class _RPCFile(io.TextIOBase): def __getattribute__(self, name): # When accessing the 'rpc' attribute, or 'write', use ours - if name in ('rpc', 'write'): + 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) @@ -264,20 +264,37 @@ class _RPCFile(io.TextIOBase): 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) - sys.stdin = self.console = self.get_remote_proxy("stdin") - sys.stdout = _RPCFile(self.get_remote_proxy("stdout")) - sys.stderr = _RPCFile(self.get_remote_proxy("stderr")) + 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")) # page help() text to shell. import pydoc # import must be done here to capture i/o binding pydoc.pager = pydoc.plainpager |