]> granicus.if.org Git - python/commitdiff
Issue #15318: Prevent writing to sys.stdin.
authorMartin v. Löwis <martin@v.loewis.de>
Wed, 25 Jul 2012 08:56:22 +0000 (10:56 +0200)
committerMartin v. Löwis <martin@v.loewis.de>
Wed, 25 Jul 2012 08:56:22 +0000 (10:56 +0200)
Patch by Roger Serwy and myself.

Lib/idlelib/NEWS.txt
Lib/idlelib/PyShell.py
Lib/idlelib/run.py

index 83d2f4825f4bbc52bd333928e2bdd9b3d18f4102..06f7d5b5713563864f6b01c7ee8fcbfd3eaef440 100644 (file)
@@ -1,6 +1,8 @@
 What's New in IDLE 2.7.4?
 =========================
 
+- Issue #15318: Prevent writing to sys.stdin.
+
 - Issue #13532, #15319: Check that arguments to sys.stdout.write are strings.
 
 - Issue # 12510: Attempt to get certain tool tips no longer crashes IDLE.
index 2bf97a589ec14b16c94877500d5dd192a3bbeae7..25eec899b30257cd68577d533598fad68df9bedb 100644 (file)
@@ -11,6 +11,7 @@ import time
 import threading
 import traceback
 import types
+import io
 
 import linecache
 from code import InteractiveInterpreter
@@ -422,6 +423,9 @@ class ModifiedInterpreter(InteractiveInterpreter):
         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("stdout", self.tkconsole.stdout)
         self.rpcclt.register("stderr", self.tkconsole.stderr)
@@ -875,13 +879,14 @@ class PyShell(OutputWindow):
         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)
         if not use_subprocess:
             sys.stdout = self.stdout
             sys.stderr = self.stderr
-            sys.stdin = self
+            sys.stdin = self.stdin
         #
         self.history = self.History(self.text)
         #
@@ -1279,6 +1284,15 @@ class PseudoFile(object):
     def isatty(self):
         return True
 
+class PseudoInputFile(object):
+    def __init__(self, shell):
+        self.readline = shell.readline
+        self.isatty = shell.isatty
+
+    def write(self, s):
+        raise io.UnsupportedOperation("not writable")
+    writelines = write
+
 
 usage_msg = """\
 
index e1e6380c8f260c8abb6d0e8c8fb9551c75d8dcd0..8e9037044fa25fb3968d789b1aef39ee320de4ba 100644 (file)
@@ -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