comparison Lib/idlelib/PyShell.py @ 84368:373d629bd5fc

Merge with 3.3
author Terry Jan Reedy <tjreedy@udel.edu>
date Fri, 28 Jun 2013 23:52:05 -0400
parents 2176c1867b34 c15d0baac3d6
children b0cc8c9ab912
comparison
equal deleted inserted replaced
84365:2176c1867b34 84368:373d629bd5fc
43 43
44 # Override warnings module to write to warning_stream. Initialize to send IDLE 44 # Override warnings module to write to warning_stream. Initialize to send IDLE
45 # internal warnings to the console. ScriptBinding.check_syntax() will 45 # internal warnings to the console. ScriptBinding.check_syntax() will
46 # temporarily redirect the stream to the shell window to display warnings when 46 # temporarily redirect the stream to the shell window to display warnings when
47 # checking user's code. 47 # checking user's code.
48 global warning_stream 48 warning_stream = sys.__stderr__ # None, at least on Windows, if no console.
49 warning_stream = sys.__stderr__ 49 import warnings
50 try: 50
51 import warnings 51 def idle_formatwarning(message, category, filename, lineno, line=None):
52 except ImportError: 52 """Format warnings the IDLE way."""
53 pass 53
54 else: 54 s = "\nWarning (from warnings module):\n"
55 def idle_showwarning(message, category, filename, lineno, 55 s += ' File \"%s\", line %s\n' % (filename, lineno)
56 file=None, line=None): 56 if line is None:
57 if file is None: 57 line = linecache.getline(filename, lineno)
58 file = warning_stream 58 line = line.strip()
59 try: 59 if line:
60 file.write(warnings.formatwarning(message, category, filename, 60 s += " %s\n" % line
61 lineno, line=line)) 61 s += "%s: %s\n" % (category.__name__, message)
62 except OSError: 62 return s
63 pass ## file (probably __stderr__) is invalid, warning dropped. 63
64 warnings.showwarning = idle_showwarning 64 def idle_showwarning(
65 def idle_formatwarning(message, category, filename, lineno, line=None): 65 message, category, filename, lineno, file=None, line=None):
66 """Format warnings the IDLE way""" 66 """Show Idle-format warning (after replacing warnings.showwarning).
67 s = "\nWarning (from warnings module):\n" 67
68 s += ' File \"%s\", line %s\n' % (filename, lineno) 68 The differences are the formatter called, the file=None replacement,
69 if line is None: 69 which can be None, the capture of the consequence AttributeError,
70 line = linecache.getline(filename, lineno) 70 and the output of a hard-coded prompt.
71 line = line.strip() 71 """
72 if line: 72 if file is None:
73 s += " %s\n" % line 73 file = warning_stream
74 s += "%s: %s\n>>> " % (category.__name__, message) 74 try:
75 return s 75 file.write(idle_formatwarning(
76 warnings.formatwarning = idle_formatwarning 76 message, category, filename, lineno, line=line))
77 file.write(">>> ")
78 except (AttributeError, OSError):
79 pass # if file (probably __stderr__) is invalid, skip warning.
80
81 _warnings_showwarning = None
82
83 def capture_warnings(capture):
84 "Replace warning.showwarning with idle_showwarning, or reverse."
85
86 global _warnings_showwarning
87 if capture:
88 if _warnings_showwarning is None:
89 _warnings_showwarning = warnings.showwarning
90 warnings.showwarning = idle_showwarning
91 else:
92 if _warnings_showwarning is not None:
93 warnings.showwarning = _warnings_showwarning
94 _warnings_showwarning = None
95
96 capture_warnings(True)
77 97
78 def extended_linecache_checkcache(filename=None, 98 def extended_linecache_checkcache(filename=None,
79 orig_checkcache=linecache.checkcache): 99 orig_checkcache=linecache.checkcache):
80 """Extend linecache.checkcache to preserve the <pyshell#...> entries 100 """Extend linecache.checkcache to preserve the <pyshell#...> entries
81 101
1423 """ 1443 """
1424 1444
1425 def main(): 1445 def main():
1426 global flist, root, use_subprocess 1446 global flist, root, use_subprocess
1427 1447
1448 capture_warnings(True)
1428 use_subprocess = True 1449 use_subprocess = True
1429 enable_shell = False 1450 enable_shell = False
1430 enable_edit = False 1451 enable_edit = False
1431 debug = False 1452 debug = False
1432 cmd = None 1453 cmd = None
1557 shell.interp.runcommand(''.join(("print('", tkversionwarning, "')"))) 1578 shell.interp.runcommand(''.join(("print('", tkversionwarning, "')")))
1558 1579
1559 while flist.inversedict: # keep IDLE running while files are open. 1580 while flist.inversedict: # keep IDLE running while files are open.
1560 root.mainloop() 1581 root.mainloop()
1561 root.destroy() 1582 root.destroy()
1583 capture_warnings(False)
1562 1584
1563 if __name__ == "__main__": 1585 if __name__ == "__main__":
1564 sys.modules['PyShell'] = sys.modules['__main__'] 1586 sys.modules['PyShell'] = sys.modules['__main__']
1565 main() 1587 main()
1588
1589 capture_warnings(False) # Make sure turned off; see issue 18081