|
45 | 45 | # internal warnings to the console. ScriptBinding.check_syntax() will |
46 | 46 | # temporarily redirect the stream to the shell window to display warnings when |
47 | 47 | # checking user's code. |
48 | | -global warning_stream |
49 | | -warning_stream = sys.__stderr__ |
50 | | -try: |
51 | | - import warnings |
52 | | -except ImportError: |
53 | | - pass |
54 | | -else: |
55 | | - def idle_showwarning(message, category, filename, lineno, |
56 | | - file=None, line=None): |
57 | | - if file is None: |
58 | | - file = warning_stream |
59 | | - try: |
60 | | - file.write(warnings.formatwarning(message, category, filename, |
61 | | - lineno, line=line)) |
62 | | - except OSError: |
63 | | - pass ## file (probably __stderr__) is invalid, warning dropped. |
64 | | - warnings.showwarning = idle_showwarning |
65 | | - def idle_formatwarning(message, category, filename, lineno, line=None): |
66 | | - """Format warnings the IDLE way""" |
67 | | - s = "\nWarning (from warnings module):\n" |
68 | | - s += ' File \"%s\", line %s\n' % (filename, lineno) |
69 | | - if line is None: |
70 | | - line = linecache.getline(filename, lineno) |
71 | | - line = line.strip() |
72 | | - if line: |
73 | | - s += " %s\n" % line |
74 | | - s += "%s: %s\n>>> " % (category.__name__, message) |
75 | | - return s |
76 | | - warnings.formatwarning = idle_formatwarning |
| 48 | +warning_stream = sys.__stderr__ # None, at least on Windows, if no console. |
| 49 | +import warnings |
| 50 | + |
| 51 | +def idle_formatwarning(message, category, filename, lineno, line=None): |
| 52 | + """Format warnings the IDLE way.""" |
| 53 | + |
| 54 | + s = "\nWarning (from warnings module):\n" |
| 55 | + s += ' File \"%s\", line %s\n' % (filename, lineno) |
| 56 | + if line is None: |
| 57 | + line = linecache.getline(filename, lineno) |
| 58 | + line = line.strip() |
| 59 | + if line: |
| 60 | + s += " %s\n" % line |
| 61 | + s += "%s: %s\n" % (category.__name__, message) |
| 62 | + return s |
| 63 | + |
| 64 | +def idle_showwarning( |
| 65 | + message, category, filename, lineno, file=None, line=None): |
| 66 | + """Show Idle-format warning (after replacing warnings.showwarning). |
| 67 | +
|
| 68 | + The differences are the formatter called, the file=None replacement, |
| 69 | + which can be None, the capture of the consequence AttributeError, |
| 70 | + and the output of a hard-coded prompt. |
| 71 | + """ |
| 72 | + if file is None: |
| 73 | + file = warning_stream |
| 74 | + try: |
| 75 | + file.write(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 | 98 | def extended_linecache_checkcache(filename=None, |
79 | 99 | orig_checkcache=linecache.checkcache): |
@@ -1425,6 +1445,7 @@ def close(self): |
1425 | 1445 | def main(): |
1426 | 1446 | global flist, root, use_subprocess |
1427 | 1447 |
|
| 1448 | + capture_warnings(True) |
1428 | 1449 | use_subprocess = True |
1429 | 1450 | enable_shell = False |
1430 | 1451 | enable_edit = False |
@@ -1559,7 +1580,10 @@ def main(): |
1559 | 1580 | while flist.inversedict: # keep IDLE running while files are open. |
1560 | 1581 | root.mainloop() |
1561 | 1582 | root.destroy() |
| 1583 | + capture_warnings(False) |
1562 | 1584 |
|
1563 | 1585 | if __name__ == "__main__": |
1564 | 1586 | sys.modules['PyShell'] = sys.modules['__main__'] |
1565 | 1587 | main() |
| 1588 | + |
| 1589 | +capture_warnings(False) # Make sure turned off; see issue 18081 |
0 commit comments