[Python-checkins] r74447 - in python/trunk/Lib/idlelib: PyShell.py run.py

guilherme.polo python-checkins at python.org
Fri Aug 14 16:03:08 CEST 2009


Author: guilherme.polo
Date: Fri Aug 14 16:03:07 2009
New Revision: 74447

Log:
Issue #3926: Fix the usage of the new showwarnings and formatwarning.

Modified:
   python/trunk/Lib/idlelib/PyShell.py
   python/trunk/Lib/idlelib/run.py

Modified: python/trunk/Lib/idlelib/PyShell.py
==============================================================================
--- python/trunk/Lib/idlelib/PyShell.py	(original)
+++ python/trunk/Lib/idlelib/PyShell.py	Fri Aug 14 16:03:07 2009
@@ -58,20 +58,21 @@
 else:
     def idle_showwarning(message, category, filename, lineno,
                          file=None, line=None):
-        file = warning_stream
+        if file is None:
+            file = warning_stream
         try:
-            file.write(warnings.formatwarning(message, category, filename,\
+            file.write(warnings.formatwarning(message, category, filename,
                                               lineno, file=file, line=line))
         except IOError:
             pass  ## file (probably __stderr__) is invalid, warning dropped.
     warnings.showwarning = idle_showwarning
-    def idle_formatwarning(message, category, filename, lineno,
-                           file=None, line=None):
+    def idle_formatwarning(message, category, filename, lineno, line=None):
         """Format warnings the IDLE way"""
         s = "\nWarning (from warnings module):\n"
         s += '  File \"%s\", line %s\n' % (filename, lineno)
-        line = linecache.getline(filename, lineno).strip() \
-            if line is None else line
+        if line is None:
+            line = linecache.getline(filename, lineno)
+        line = line.strip()
         if line:
             s += "    %s\n" % line
         s += "%s: %s\n>>> " % (category.__name__, message)
@@ -84,18 +85,17 @@
 
     Rather than repeating the linecache code, patch it to save the
     <pyshell#...> entries, call the original linecache.checkcache()
-    (which destroys them), and then restore the saved entries.
+    (skipping them), and then restore the saved entries.
 
     orig_checkcache is bound at definition time to the original
     method, allowing it to be patched.
-
     """
     cache = linecache.cache
     save = {}
-    for filename in cache.keys():
-        if filename[:1] + filename[-1:] == '<>':
-            save[filename] = cache[filename]
-    orig_checkcache()
+    for key in list(cache):
+        if key[:1] + key[-1:] == '<>':
+            save[key] = cache.pop(key)
+    orig_checkcache(filename)
     cache.update(save)
 
 # Patch linecache.checkcache():

Modified: python/trunk/Lib/idlelib/run.py
==============================================================================
--- python/trunk/Lib/idlelib/run.py	(original)
+++ python/trunk/Lib/idlelib/run.py	Fri Aug 14 16:03:07 2009
@@ -25,12 +25,13 @@
     pass
 else:
     def idle_formatwarning_subproc(message, category, filename, lineno,
-                                   file=None, line=None):
+                                   line=None):
         """Format warnings the IDLE way"""
         s = "\nWarning (from warnings module):\n"
         s += '  File \"%s\", line %s\n' % (filename, lineno)
-        line = linecache.getline(filename, lineno).strip() \
-            if line is None else line
+        if line is None:
+            line = linecache.getline(filename, lineno)
+        line = line.strip()
         if line:
             s += "    %s\n" % line
         s += "%s: %s\n" % (category.__name__, message)


More information about the Python-checkins mailing list