Commit bee003cf authored by Terry Jan Reedy's avatar Terry Jan Reedy

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.
parent 175b1a7b
#! /usr/bin/env python #! /usr/bin/env python
from __future__ import print_function
import os import os
import os.path import os.path
...@@ -20,8 +21,8 @@ from platform import python_version, system ...@@ -20,8 +21,8 @@ from platform import python_version, system
try: try:
from Tkinter import * from Tkinter import *
except ImportError: except ImportError:
print>>sys.__stderr__, "** IDLE can't import Tkinter. " \ print("** IDLE can't import Tkinter.\n"
"Your Python may not be configured for Tk. **" "Your Python may not be configured for Tk. **", file=sys.__stderr__)
sys.exit(1) sys.exit(1)
import tkMessageBox import tkMessageBox
...@@ -587,14 +588,14 @@ class ModifiedInterpreter(InteractiveInterpreter): ...@@ -587,14 +588,14 @@ class ModifiedInterpreter(InteractiveInterpreter):
console = self.tkconsole.console console = self.tkconsole.console
if how == "OK": if how == "OK":
if what is not None: if what is not None:
print >>console, repr(what) print(repr(what), file=console)
elif how == "EXCEPTION": elif how == "EXCEPTION":
if self.tkconsole.getvar("<<toggle-jit-stack-viewer>>"): if self.tkconsole.getvar("<<toggle-jit-stack-viewer>>"):
self.remote_stack_viewer() self.remote_stack_viewer()
elif how == "ERROR": elif how == "ERROR":
errmsg = "PyShell.ModifiedInterpreter: Subprocess ERROR:\n" errmsg = "PyShell.ModifiedInterpreter: Subprocess ERROR:\n"
print >>sys.__stderr__, errmsg, what print(errmsg, what, file=sys.__stderr__)
print >>console, errmsg, what print(errmsg, what, file=console)
# we received a response to the currently active seq number: # we received a response to the currently active seq number:
try: try:
self.tkconsole.endexecuting() self.tkconsole.endexecuting()
...@@ -658,9 +659,9 @@ class ModifiedInterpreter(InteractiveInterpreter): ...@@ -658,9 +659,9 @@ class ModifiedInterpreter(InteractiveInterpreter):
code = compile(source, filename, "exec") code = compile(source, filename, "exec")
except (OverflowError, SyntaxError): except (OverflowError, SyntaxError):
self.tkconsole.resetoutput() self.tkconsole.resetoutput()
tkerr = self.tkconsole.stderr print('*** Error in script or command!\n'
print>>tkerr, '*** Error in script or command!\n' 'Traceback (most recent call last):',
print>>tkerr, 'Traceback (most recent call last):' file=self.tkconsole.stderr)
InteractiveInterpreter.showsyntaxerror(self, filename) InteractiveInterpreter.showsyntaxerror(self, filename)
self.tkconsole.showprompt() self.tkconsole.showprompt()
else: else:
...@@ -810,14 +811,14 @@ class ModifiedInterpreter(InteractiveInterpreter): ...@@ -810,14 +811,14 @@ class ModifiedInterpreter(InteractiveInterpreter):
raise raise
except: except:
if use_subprocess: if use_subprocess:
print >>self.tkconsole.stderr, \ print("IDLE internal error in runcode()",
"IDLE internal error in runcode()" file=self.tkconsole.stderr)
self.showtraceback() self.showtraceback()
self.tkconsole.endexecuting() self.tkconsole.endexecuting()
else: else:
if self.tkconsole.canceled: if self.tkconsole.canceled:
self.tkconsole.canceled = False self.tkconsole.canceled = False
print >>self.tkconsole.stderr, "KeyboardInterrupt" print("KeyboardInterrupt", file=self.tkconsole.stderr)
else: else:
self.showtraceback() self.showtraceback()
finally: finally:
...@@ -1480,8 +1481,7 @@ def main(): ...@@ -1480,8 +1481,7 @@ def main():
try: try:
opts, args = getopt.getopt(sys.argv[1:], "c:deihnr:st:") opts, args = getopt.getopt(sys.argv[1:], "c:deihnr:st:")
except getopt.error as msg: except getopt.error as msg:
sys.stderr.write("Error: %s\n" % str(msg)) print("Error: %s\n%s" % (msg, usage_msg), file=sys.stderr)
sys.stderr.write(usage_msg)
sys.exit(2) sys.exit(2)
for o, a in opts: for o, a in opts:
if o == '-c': if o == '-c':
...@@ -1504,7 +1504,7 @@ def main(): ...@@ -1504,7 +1504,7 @@ def main():
if os.path.isfile(script): if os.path.isfile(script):
pass pass
else: else:
print "No script file: ", script print("No script file: ", script, file=sys.stderr)
sys.exit() sys.exit()
enable_shell = True enable_shell = True
if o == '-s': if o == '-s':
......
...@@ -15,8 +15,8 @@ idle. This is to allow IDLE to continue to function in spite of errors in ...@@ -15,8 +15,8 @@ idle. This is to allow IDLE to continue to function in spite of errors in
the retrieval of config information. When a default is returned instead of 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 a requested config value, a message is printed to stderr to aid in
configuration problem notification and resolution. configuration problem notification and resolution.
""" """
from __future__ import print_function
import os import os
import sys import sys
import string import string
...@@ -202,9 +202,9 @@ class IdleConf: ...@@ -202,9 +202,9 @@ class IdleConf:
if userDir != '~': # expanduser() found user home dir if userDir != '~': # expanduser() found user home dir
if not os.path.exists(userDir): if not os.path.exists(userDir):
warn = ('\n Warning: os.path.expanduser("~") points to\n '+ 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: try:
sys.stderr.write(warn) print(warn, file=sys.stderr)
except IOError: except IOError:
pass pass
userDir = '~' userDir = '~'
...@@ -217,8 +217,8 @@ class IdleConf: ...@@ -217,8 +217,8 @@ class IdleConf:
os.mkdir(userDir) os.mkdir(userDir)
except (OSError, IOError): except (OSError, IOError):
warn = ('\n Warning: unable to create user config directory\n'+ warn = ('\n Warning: unable to create user config directory\n'+
userDir+'\n Check path and permissions.\n Exiting!\n\n') userDir+'\n Check path and permissions.\n Exiting!\n')
sys.stderr.write(warn) print(warn, file=sys.stderr)
raise SystemExit raise SystemExit
return userDir return userDir
...@@ -243,12 +243,12 @@ class IdleConf: ...@@ -243,12 +243,12 @@ class IdleConf:
except ValueError: except ValueError:
warning = ('\n Warning: configHandler.py - IdleConf.GetOption -\n' warning = ('\n Warning: configHandler.py - IdleConf.GetOption -\n'
' invalid %r value for configuration option %r\n' ' invalid %r value for configuration option %r\n'
' from section %r: %r\n' % ' from section %r: %r' %
(type, option, section, (type, option, section,
self.userCfg[configType].Get(section, option, self.userCfg[configType].Get(section, option,
raw=raw))) raw=raw)))
try: try:
sys.stderr.write(warning) print(warning, file=sys.stderr)
except IOError: except IOError:
pass pass
try: try:
...@@ -262,10 +262,10 @@ class IdleConf: ...@@ -262,10 +262,10 @@ class IdleConf:
warning = ('\n Warning: configHandler.py - IdleConf.GetOption -\n' warning = ('\n Warning: configHandler.py - IdleConf.GetOption -\n'
' problem retrieving configuration option %r\n' ' problem retrieving configuration option %r\n'
' from section %r.\n' ' from section %r.\n'
' returning default value: %r\n' % ' returning default value: %r' %
(option, section, default)) (option, section, default))
try: try:
sys.stderr.write(warning) print(warning, file=sys.stderr)
except IOError: except IOError:
pass pass
return default return default
...@@ -374,10 +374,10 @@ class IdleConf: ...@@ -374,10 +374,10 @@ class IdleConf:
warning=('\n Warning: configHandler.py - IdleConf.GetThemeDict' warning=('\n Warning: configHandler.py - IdleConf.GetThemeDict'
' -\n problem retrieving theme element %r' ' -\n problem retrieving theme element %r'
'\n from theme %r.\n' '\n from theme %r.\n'
' returning default value: %r\n' % ' returning default value: %r' %
(element, themeName, theme[element])) (element, themeName, theme[element]))
try: try:
sys.stderr.write(warning) print(warning, file=sys.stderr)
except IOError: except IOError:
pass pass
colour=cfgParser.Get(themeName,element,default=theme[element]) colour=cfgParser.Get(themeName,element,default=theme[element])
...@@ -634,10 +634,10 @@ class IdleConf: ...@@ -634,10 +634,10 @@ class IdleConf:
warning=('\n Warning: configHandler.py - IdleConf.GetCoreKeys' warning=('\n Warning: configHandler.py - IdleConf.GetCoreKeys'
' -\n problem retrieving key binding for event %r' ' -\n problem retrieving key binding for event %r'
'\n from key set %r.\n' '\n from key set %r.\n'
' returning default value: %r\n' % ' returning default value: %r' %
(event, keySetName, keyBindings[event])) (event, keySetName, keyBindings[event]))
try: try:
sys.stderr.write(warning) print(warning, file=sys.stderr)
except IOError: except IOError:
pass pass
return keyBindings return keyBindings
...@@ -704,18 +704,18 @@ idleConf=IdleConf() ...@@ -704,18 +704,18 @@ idleConf=IdleConf()
### module test ### module test
if __name__ == '__main__': if __name__ == '__main__':
def dumpCfg(cfg): def dumpCfg(cfg):
print '\n',cfg,'\n' print('\n', cfg, '\n')
for key in cfg.keys(): for key in cfg.keys():
sections=cfg[key].sections() sections=cfg[key].sections()
print key print(key)
print sections print(sections)
for section in sections: for section in sections:
options=cfg[key].options(section) options=cfg[key].options(section)
print section print(section)
print options print(options)
for option in options: for option in options:
print option, '=', cfg[key].Get(section,option) print(option, '=', cfg[key].Get(section,option))
dumpCfg(idleConf.defaultCfg) dumpCfg(idleConf.defaultCfg)
dumpCfg(idleConf.userCfg) dumpCfg(idleConf.userCfg)
print idleConf.userCfg['main'].Get('Theme','name') print(idleConf.userCfg['main'].Get('Theme','name'))
#print idleConf.userCfg['highlight'].GetDefHighlight('Foo','normal') #print(idleConf.userCfg['highlight'].GetDefHighlight('Foo','normal'))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment