Skip to content

Commit cd49d53

Browse files
committed
Issue #14200 — now displayhook for IDLE works in non-subprocess mode as well as subprecess.
1 parent 1a7742e commit cd49d53

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

Lib/idlelib/PyShell.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,8 @@ def begin(self):
999999
return False
10001000
else:
10011001
nosub = "==== No Subprocess ===="
1002+
sys.displayhook = rpc.displayhook
1003+
10021004
self.write("Python %s on %s\n%s\n%s" %
10031005
(sys.version, sys.platform, self.COPYRIGHT, nosub))
10041006
self.showprompt()

Lib/idlelib/rpc.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import copyreg
4141
import types
4242
import marshal
43+
import builtins
4344

4445

4546
def unpickle_code(ms):
@@ -603,3 +604,21 @@ def __call__(self, *args, **kwargs):
603604

604605
# XXX KBK 09Sep03 We need a proper unit test for this module. Previously
605606
# existing test code was removed at Rev 1.27 (r34098).
607+
608+
def displayhook(value):
609+
"""Override standard display hook to use non-locale encoding"""
610+
if value is None:
611+
return
612+
# Set '_' to None to avoid recursion
613+
builtins._ = None
614+
text = repr(value)
615+
try:
616+
sys.stdout.write(text)
617+
except UnicodeEncodeError:
618+
# let's use ascii while utf8-bmp codec doesn't present
619+
encoding = 'ascii'
620+
bytes = text.encode(encoding, 'backslashreplace')
621+
text = bytes.decode(encoding, 'strict')
622+
sys.stdout.write(text)
623+
sys.stdout.write("\n")
624+
builtins._ = value

Lib/idlelib/run.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import _thread as thread
77
import threading
88
import queue
9-
import builtins
109

1110
from idlelib import CallTips
1211
from idlelib import AutoComplete
@@ -262,25 +261,6 @@ def handle_error(self, request, client_address):
262261
thread.interrupt_main()
263262

264263

265-
def displayhook(value):
266-
"""Override standard display hook to use non-locale encoding"""
267-
if value is None:
268-
return
269-
# Set '_' to None to avoid recursion
270-
builtins._ = None
271-
text = repr(value)
272-
try:
273-
sys.stdout.write(text)
274-
except UnicodeEncodeError:
275-
# let's use ascii while utf8-bmp codec doesn't present
276-
encoding = 'ascii'
277-
bytes = text.encode(encoding, 'backslashreplace')
278-
text = bytes.decode(encoding, 'strict')
279-
sys.stdout.write(text)
280-
sys.stdout.write("\n")
281-
builtins._ = value
282-
283-
284264
class MyHandler(rpc.RPCHandler):
285265

286266
def handle(self):
@@ -290,7 +270,7 @@ def handle(self):
290270
sys.stdin = self.console = self.get_remote_proxy("stdin")
291271
sys.stdout = self.get_remote_proxy("stdout")
292272
sys.stderr = self.get_remote_proxy("stderr")
293-
sys.displayhook = displayhook
273+
sys.displayhook = rpc.displayhook
294274
# page help() text to shell.
295275
import pydoc # import must be done here to capture i/o binding
296276
pydoc.pager = pydoc.plainpager

0 commit comments

Comments
 (0)