[Python-checkins] cpython (3.2): Issue #13532: Check that arguments to sys.stdout.write are strings.

martin.v.loewis python-checkins at python.org
Mon Jul 9 20:53:18 CEST 2012


http://hg.python.org/cpython/rev/422242dbce30
changeset:   78031:422242dbce30
branch:      3.2
parent:      78019:eea379307efa
user:        Martin v. Löwis <martin at v.loewis.de>
date:        Mon Jul 09 20:46:11 2012 +0200
summary:
  Issue #13532: Check that arguments to sys.stdout.write are strings.

files:
  Lib/idlelib/NEWS.txt   |   2 ++
  Lib/idlelib/PyShell.py |   2 ++
  Lib/idlelib/run.py     |  24 +++++++++++++++++++++---
  3 files changed, 25 insertions(+), 3 deletions(-)


diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt
--- a/Lib/idlelib/NEWS.txt
+++ b/Lib/idlelib/NEWS.txt
@@ -1,6 +1,8 @@
 What's New in IDLE 3.2.4?
 =========================
 
+- Issue #13532: Check that arguments to sys.stdout.write are strings.
+
 - Issue # 12510: Attempt to get certain tool tips no longer crashes IDLE.
   Erroneous tool tips have been corrected. Default added for callables.
 
diff --git a/Lib/idlelib/PyShell.py b/Lib/idlelib/PyShell.py
--- a/Lib/idlelib/PyShell.py
+++ b/Lib/idlelib/PyShell.py
@@ -1242,6 +1242,8 @@
         self.encoding = encoding
 
     def write(self, s):
+        if not isinstance(s, str):
+            raise TypeError('must be str, not ' + type(s).__name__)
         self.shell.write(s, self.tags)
 
     def writelines(self, lines):
diff --git a/Lib/idlelib/run.py b/Lib/idlelib/run.py
--- a/Lib/idlelib/run.py
+++ b/Lib/idlelib/run.py
@@ -1,4 +1,5 @@
 import sys
+import io
 import linecache
 import time
 import socket
@@ -244,6 +245,23 @@
             quitting = True
             thread.interrupt_main()
 
+class _RPCFile(io.TextIOBase):
+    """Wrapper class for the RPC proxy to typecheck arguments 
+    that may not support pickling."""
+
+    def __init__(self, rpc):
+        super.__setattr__(self, 'rpc', rpc)
+
+    def __getattr__(self, name):
+        return getattr(self.rpc, name)
+
+    def __setattr__(self, name, value):
+        return setattr(self.rpc, name, value)
+
+    def write(self, s):
+        if not isinstance(s, str):
+            raise TypeError('must be str, not ' + type(s).__name__)
+        return self.rpc.write(s)
 
 class MyHandler(rpc.RPCHandler):
 
@@ -251,9 +269,9 @@
         """Override base method"""
         executive = Executive(self)
         self.register("exec", executive)
-        sys.stdin = self.console = self.get_remote_proxy("stdin")
-        sys.stdout = self.get_remote_proxy("stdout")
-        sys.stderr = self.get_remote_proxy("stderr")
+        sys.stdin = self.console = _RPCFile(self.get_remote_proxy("stdin"))
+        sys.stdout = _RPCFile(self.get_remote_proxy("stdout"))
+        sys.stderr = _RPCFile(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

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list