Issue #22420: Avoid 'write to None' crashes by using print instead.
For 2,.7, add print_function __future__ import and convert print statements
to print functions. Based on 3.x patch by Serhiy Storchaka.
diff --git a/Lib/idlelib/PyShell.py b/Lib/idlelib/PyShell.py
index 1e86292..e20f70b 100755
--- a/Lib/idlelib/PyShell.py
+++ b/Lib/idlelib/PyShell.py
@@ -1,4 +1,5 @@
#! /usr/bin/env python
+from __future__ import print_function
import os
import os.path
@@ -20,8 +21,8 @@
try:
from Tkinter import *
except ImportError:
- print>>sys.__stderr__, "** IDLE can't import Tkinter. " \
- "Your Python may not be configured for Tk. **"
+ print("** IDLE can't import Tkinter.\n"
+ "Your Python may not be configured for Tk. **", file=sys.__stderr__)
sys.exit(1)
import tkMessageBox
@@ -587,14 +588,14 @@
console = self.tkconsole.console
if how == "OK":
if what is not None:
- print >>console, repr(what)
+ print(repr(what), file=console)
elif how == "EXCEPTION":
if self.tkconsole.getvar("<<toggle-jit-stack-viewer>>"):
self.remote_stack_viewer()
elif how == "ERROR":
errmsg = "PyShell.ModifiedInterpreter: Subprocess ERROR:\n"
- print >>sys.__stderr__, errmsg, what
- print >>console, errmsg, what
+ print(errmsg, what, file=sys.__stderr__)
+ print(errmsg, what, file=console)
# we received a response to the currently active seq number:
try:
self.tkconsole.endexecuting()
@@ -658,9 +659,9 @@
code = compile(source, filename, "exec")
except (OverflowError, SyntaxError):
self.tkconsole.resetoutput()
- tkerr = self.tkconsole.stderr
- print>>tkerr, '*** Error in script or command!\n'
- print>>tkerr, 'Traceback (most recent call last):'
+ print('*** Error in script or command!\n'
+ 'Traceback (most recent call last):',
+ file=self.tkconsole.stderr)
InteractiveInterpreter.showsyntaxerror(self, filename)
self.tkconsole.showprompt()
else:
@@ -810,14 +811,14 @@
raise
except:
if use_subprocess:
- print >>self.tkconsole.stderr, \
- "IDLE internal error in runcode()"
+ print("IDLE internal error in runcode()",
+ file=self.tkconsole.stderr)
self.showtraceback()
self.tkconsole.endexecuting()
else:
if self.tkconsole.canceled:
self.tkconsole.canceled = False
- print >>self.tkconsole.stderr, "KeyboardInterrupt"
+ print("KeyboardInterrupt", file=self.tkconsole.stderr)
else:
self.showtraceback()
finally:
@@ -1480,8 +1481,7 @@
try:
opts, args = getopt.getopt(sys.argv[1:], "c:deihnr:st:")
except getopt.error as msg:
- sys.stderr.write("Error: %s\n" % str(msg))
- sys.stderr.write(usage_msg)
+ print("Error: %s\n%s" % (msg, usage_msg), file=sys.stderr)
sys.exit(2)
for o, a in opts:
if o == '-c':
@@ -1504,7 +1504,7 @@
if os.path.isfile(script):
pass
else:
- print "No script file: ", script
+ print("No script file: ", script, file=sys.stderr)
sys.exit()
enable_shell = True
if o == '-s':
diff --git a/Lib/idlelib/configHandler.py b/Lib/idlelib/configHandler.py
index 173751e..9091258 100644
--- a/Lib/idlelib/configHandler.py
+++ b/Lib/idlelib/configHandler.py
@@ -15,8 +15,8 @@
the retrieval of config information. When a default is returned instead of
a requested config value, a message is printed to stderr to aid in
configuration problem notification and resolution.
-
"""
+from __future__ import print_function
import os
import sys
import string
@@ -202,9 +202,9 @@
if userDir != '~': # expanduser() found user home dir
if not os.path.exists(userDir):
warn = ('\n Warning: os.path.expanduser("~") points to\n '+
- userDir+',\n but the path does not exist.\n')
+ userDir+',\n but the path does not exist.')
try:
- sys.stderr.write(warn)
+ print(warn, file=sys.stderr)
except IOError:
pass
userDir = '~'
@@ -217,8 +217,8 @@
os.mkdir(userDir)
except (OSError, IOError):
warn = ('\n Warning: unable to create user config directory\n'+
- userDir+'\n Check path and permissions.\n Exiting!\n\n')
- sys.stderr.write(warn)
+ userDir+'\n Check path and permissions.\n Exiting!\n')
+ print(warn, file=sys.stderr)
raise SystemExit
return userDir
@@ -243,12 +243,12 @@
except ValueError:
warning = ('\n Warning: configHandler.py - IdleConf.GetOption -\n'
' invalid %r value for configuration option %r\n'
- ' from section %r: %r\n' %
+ ' from section %r: %r' %
(type, option, section,
self.userCfg[configType].Get(section, option,
raw=raw)))
try:
- sys.stderr.write(warning)
+ print(warning, file=sys.stderr)
except IOError:
pass
try:
@@ -262,10 +262,10 @@
warning = ('\n Warning: configHandler.py - IdleConf.GetOption -\n'
' problem retrieving configuration option %r\n'
' from section %r.\n'
- ' returning default value: %r\n' %
+ ' returning default value: %r' %
(option, section, default))
try:
- sys.stderr.write(warning)
+ print(warning, file=sys.stderr)
except IOError:
pass
return default
@@ -374,10 +374,10 @@
warning=('\n Warning: configHandler.py - IdleConf.GetThemeDict'
' -\n problem retrieving theme element %r'
'\n from theme %r.\n'
- ' returning default value: %r\n' %
+ ' returning default value: %r' %
(element, themeName, theme[element]))
try:
- sys.stderr.write(warning)
+ print(warning, file=sys.stderr)
except IOError:
pass
colour=cfgParser.Get(themeName,element,default=theme[element])
@@ -634,10 +634,10 @@
warning=('\n Warning: configHandler.py - IdleConf.GetCoreKeys'
' -\n problem retrieving key binding for event %r'
'\n from key set %r.\n'
- ' returning default value: %r\n' %
+ ' returning default value: %r' %
(event, keySetName, keyBindings[event]))
try:
- sys.stderr.write(warning)
+ print(warning, file=sys.stderr)
except IOError:
pass
return keyBindings
@@ -704,18 +704,18 @@
### module test
if __name__ == '__main__':
def dumpCfg(cfg):
- print '\n',cfg,'\n'
+ print('\n', cfg, '\n')
for key in cfg.keys():
sections=cfg[key].sections()
- print key
- print sections
+ print(key)
+ print(sections)
for section in sections:
options=cfg[key].options(section)
- print section
- print options
+ print(section)
+ print(options)
for option in options:
- print option, '=', cfg[key].Get(section,option)
+ print(option, '=', cfg[key].Get(section,option))
dumpCfg(idleConf.defaultCfg)
dumpCfg(idleConf.userCfg)
- print idleConf.userCfg['main'].Get('Theme','name')
- #print idleConf.userCfg['highlight'].GetDefHighlight('Foo','normal')
+ print(idleConf.userCfg['main'].Get('Theme','name'))
+ #print(idleConf.userCfg['highlight'].GetDefHighlight('Foo','normal'))