cpython: c06b94c5c609 (original) (raw)
Mercurial > cpython
changeset 75641:c06b94c5c609
Issue #14200: Idle shell crash on printing non-BMP unicode character. UnicodeEncodeError is raised for strings contains non-BMP characters. For eval results unicode escaping is used, print() calls display exception with traceback as usual. [#14200]
Andrew Svetlov andrew.svetlov@gmail.com | |
---|---|
date | Wed, 14 Mar 2012 13:22:12 -0700 |
parents | 7cbc48324938 |
children | e6baf63f2f15 |
files | Lib/idlelib/PyShell.py Lib/idlelib/rpc.py Lib/idlelib/run.py Misc/NEWS |
diffstat | 4 files changed, 40 insertions(+), 0 deletions(-)[+] [-] Lib/idlelib/PyShell.py 10 Lib/idlelib/rpc.py 7 Lib/idlelib/run.py 21 Misc/NEWS 2 |
line wrap: on
line diff
--- a/Lib/idlelib/PyShell.py +++ b/Lib/idlelib/PyShell.py @@ -1221,6 +1221,16 @@ class PyShell(OutputWindow): self.set_line_and_column() def write(self, s, tags=()):
if isinstance(s, str) and len(s) and max(s) > '\uffff':[](#l1.7)
# Tk doesn't support outputting non-BMP characters[](#l1.8)
# Let's assume what printed string is not very long,[](#l1.9)
# find first non-BMP character and construct informative[](#l1.10)
# UnicodeEncodeError exception.[](#l1.11)
for start, char in enumerate(s):[](#l1.12)
if char > '\uffff':[](#l1.13)
break[](#l1.14)
raise UnicodeEncodeError("UCS-2", char, start, start+1,[](#l1.15)
'Non-BMP character not supported in Tk')[](#l1.16) try:[](#l1.17) self.text.mark_gravity("iomark", "right")[](#l1.18) OutputWindow.write(self, s, tags, "iomark")[](#l1.19)
--- a/Lib/idlelib/rpc.py +++ b/Lib/idlelib/rpc.py @@ -196,8 +196,12 @@ class SocketIO(object): return ("ERROR", "Unsupported message type: %s" % how) except SystemExit: raise
except KeyboardInterrupt:[](#l2.7)
raise[](#l2.8) except socket.error:[](#l2.9) raise[](#l2.10)
except Exception as ex:[](#l2.11)
return ("CALLEXC", ex)[](#l2.12) except:[](#l2.13) msg = "*** Internal Error: rpc.py:SocketIO.localcall()\n\n"\[](#l2.14) " Object: %s \n Method: %s \n Args: %s\n"[](#l2.15)
@@ -257,6 +261,9 @@ class SocketIO(object): if how == "ERROR": self.debug("decoderesponse: Internal ERROR:", what) raise RuntimeError(what)
if how == "CALLEXC":[](#l2.20)
self.debug("decoderesponse: Call Exception:", what)[](#l2.21)
raise what[](#l2.22) raise SystemError(how, what)[](#l2.23)
def decode_interrupthook(self):
--- a/Lib/idlelib/run.py +++ b/Lib/idlelib/run.py @@ -6,6 +6,7 @@ import traceback import _thread as thread import threading import queue +import builtins from idlelib import CallTips from idlelib import AutoComplete @@ -261,6 +262,25 @@ class MyRPCServer(rpc.RPCServer): thread.interrupt_main() +def displayhook(value):
- """Override standard display hook to use non-locale encoding"""
- if value is None:
return[](#l3.18)
Set '_' to None to avoid recursion
- builtins._ = None
- text = repr(value)
- try:
sys.stdout.write(text)[](#l3.23)
- except UnicodeEncodeError:
# let's use ascii while utf8-bmp codec doesn't present[](#l3.25)
encoding = 'ascii'[](#l3.26)
bytes = text.encode(encoding, 'backslashreplace')[](#l3.27)
text = bytes.decode(encoding, 'strict')[](#l3.28)
sys.stdout.write(text)[](#l3.29)
- sys.stdout.write("\n")
- builtins._ = value
+ + class MyHandler(rpc.RPCHandler): def handle(self): @@ -270,6 +290,7 @@ class MyHandler(rpc.RPCHandler): sys.stdin = self.console = self.get_remote_proxy("stdin") sys.stdout = self.get_remote_proxy("stdout") sys.stderr = self.get_remote_proxy("stderr")
sys.displayhook = displayhook[](#l3.41) # page help() text to shell.[](#l3.42) import pydoc # import must be done here to capture i/o binding[](#l3.43) pydoc.pager = pydoc.plainpager[](#l3.44)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -24,6 +24,8 @@ Core and Builtins Library ------- +- Issue #14200: Idle shell crash on printing non-BMP unicode character. +