diff options
author | Martin v. Löwis <martin@v.loewis.de> | 2012-07-25 10:56:22 +0200 |
---|---|---|
committer | Martin v. Löwis <martin@v.loewis.de> | 2012-07-25 10:56:22 +0200 |
commit | e2b5624ee8cc4ac505162e6b3d99cfb4c2e8aa77 (patch) | |
tree | 2adfe2bc1066cd26707b0fd83870e94662dd8bc8 /Lib/idlelib/run.py | |
parent | 7009845c625268952fbdabf397701f71c7e8852e (diff) | |
download | cpython-git-e2b5624ee8cc4ac505162e6b3d99cfb4c2e8aa77.tar.gz |
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 | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/Lib/idlelib/run.py b/Lib/idlelib/run.py index e1e6380c8f..8e9037044f 100644 --- a/Lib/idlelib/run.py +++ b/Lib/idlelib/run.py @@ -260,7 +260,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) @@ -268,20 +268,35 @@ 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, basestring): + 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, (basestring, bytearray)): - raise TypeError('must be string, 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")) from idlelib import IOBinding sys.stdin.encoding = sys.stdout.encoding = \ sys.stderr.encoding = IOBinding.encoding |