mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
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.
This commit is contained in:
parent
c5ceb0aaaf
commit
05bab93339
4 changed files with 40 additions and 0 deletions
|
@ -1221,6 +1221,16 @@ class PyShell(OutputWindow):
|
||||||
self.set_line_and_column()
|
self.set_line_and_column()
|
||||||
|
|
||||||
def write(self, s, tags=()):
|
def write(self, s, tags=()):
|
||||||
|
if isinstance(s, str) and len(s) and max(s) > '\uffff':
|
||||||
|
# Tk doesn't support outputting non-BMP characters
|
||||||
|
# Let's assume what printed string is not very long,
|
||||||
|
# find first non-BMP character and construct informative
|
||||||
|
# UnicodeEncodeError exception.
|
||||||
|
for start, char in enumerate(s):
|
||||||
|
if char > '\uffff':
|
||||||
|
break
|
||||||
|
raise UnicodeEncodeError("UCS-2", char, start, start+1,
|
||||||
|
'Non-BMP character not supported in Tk')
|
||||||
try:
|
try:
|
||||||
self.text.mark_gravity("iomark", "right")
|
self.text.mark_gravity("iomark", "right")
|
||||||
OutputWindow.write(self, s, tags, "iomark")
|
OutputWindow.write(self, s, tags, "iomark")
|
||||||
|
|
|
@ -196,8 +196,12 @@ class SocketIO(object):
|
||||||
return ("ERROR", "Unsupported message type: %s" % how)
|
return ("ERROR", "Unsupported message type: %s" % how)
|
||||||
except SystemExit:
|
except SystemExit:
|
||||||
raise
|
raise
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
raise
|
||||||
except socket.error:
|
except socket.error:
|
||||||
raise
|
raise
|
||||||
|
except Exception as ex:
|
||||||
|
return ("CALLEXC", ex)
|
||||||
except:
|
except:
|
||||||
msg = "*** Internal Error: rpc.py:SocketIO.localcall()\n\n"\
|
msg = "*** Internal Error: rpc.py:SocketIO.localcall()\n\n"\
|
||||||
" Object: %s \n Method: %s \n Args: %s\n"
|
" Object: %s \n Method: %s \n Args: %s\n"
|
||||||
|
@ -257,6 +261,9 @@ class SocketIO(object):
|
||||||
if how == "ERROR":
|
if how == "ERROR":
|
||||||
self.debug("decoderesponse: Internal ERROR:", what)
|
self.debug("decoderesponse: Internal ERROR:", what)
|
||||||
raise RuntimeError(what)
|
raise RuntimeError(what)
|
||||||
|
if how == "CALLEXC":
|
||||||
|
self.debug("decoderesponse: Call Exception:", what)
|
||||||
|
raise what
|
||||||
raise SystemError(how, what)
|
raise SystemError(how, what)
|
||||||
|
|
||||||
def decode_interrupthook(self):
|
def decode_interrupthook(self):
|
||||||
|
|
|
@ -6,6 +6,7 @@ import traceback
|
||||||
import _thread as thread
|
import _thread as thread
|
||||||
import threading
|
import threading
|
||||||
import queue
|
import queue
|
||||||
|
import builtins
|
||||||
|
|
||||||
from idlelib import CallTips
|
from idlelib import CallTips
|
||||||
from idlelib import AutoComplete
|
from idlelib import AutoComplete
|
||||||
|
@ -261,6 +262,25 @@ class MyRPCServer(rpc.RPCServer):
|
||||||
thread.interrupt_main()
|
thread.interrupt_main()
|
||||||
|
|
||||||
|
|
||||||
|
def displayhook(value):
|
||||||
|
"""Override standard display hook to use non-locale encoding"""
|
||||||
|
if value is None:
|
||||||
|
return
|
||||||
|
# Set '_' to None to avoid recursion
|
||||||
|
builtins._ = None
|
||||||
|
text = repr(value)
|
||||||
|
try:
|
||||||
|
sys.stdout.write(text)
|
||||||
|
except UnicodeEncodeError:
|
||||||
|
# let's use ascii while utf8-bmp codec doesn't present
|
||||||
|
encoding = 'ascii'
|
||||||
|
bytes = text.encode(encoding, 'backslashreplace')
|
||||||
|
text = bytes.decode(encoding, 'strict')
|
||||||
|
sys.stdout.write(text)
|
||||||
|
sys.stdout.write("\n")
|
||||||
|
builtins._ = value
|
||||||
|
|
||||||
|
|
||||||
class MyHandler(rpc.RPCHandler):
|
class MyHandler(rpc.RPCHandler):
|
||||||
|
|
||||||
def handle(self):
|
def handle(self):
|
||||||
|
@ -270,6 +290,7 @@ class MyHandler(rpc.RPCHandler):
|
||||||
sys.stdin = self.console = self.get_remote_proxy("stdin")
|
sys.stdin = self.console = self.get_remote_proxy("stdin")
|
||||||
sys.stdout = self.get_remote_proxy("stdout")
|
sys.stdout = self.get_remote_proxy("stdout")
|
||||||
sys.stderr = self.get_remote_proxy("stderr")
|
sys.stderr = self.get_remote_proxy("stderr")
|
||||||
|
sys.displayhook = displayhook
|
||||||
# page help() text to shell.
|
# page help() text to shell.
|
||||||
import pydoc # import must be done here to capture i/o binding
|
import pydoc # import must be done here to capture i/o binding
|
||||||
pydoc.pager = pydoc.plainpager
|
pydoc.pager = pydoc.plainpager
|
||||||
|
|
|
@ -24,6 +24,8 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #14200: Idle shell crash on printing non-BMP unicode character.
|
||||||
|
|
||||||
- Issue #14291: Email now defaults to utf-8 for non-ASCII unicode headers
|
- Issue #14291: Email now defaults to utf-8 for non-ASCII unicode headers
|
||||||
instead of raising an error. This fixes a regression relative to 2.7.
|
instead of raising an error. This fixes a regression relative to 2.7.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue