]> granicus.if.org Git - python/commitdiff
SF #737473: Show up-to-date source code in tracebacks always.
authorHye-Shik Chang <hyeshik@gmail.com>
Tue, 26 Oct 2004 09:16:42 +0000 (09:16 +0000)
committerHye-Shik Chang <hyeshik@gmail.com>
Tue, 26 Oct 2004 09:16:42 +0000 (09:16 +0000)
And add an optional argument 'filename' to linecache.checkcache()
to enable checking caches per-file.

Doc/lib/liblinecache.tex
Lib/linecache.py
Lib/test/test_traceback.py
Lib/traceback.py
Misc/NEWS

index 8a9b914eb39d4101f21ffee0bac7fb9366f08844..c022ba9eac30638d4b39b7c6cb65fa9dcc03989d 100644 (file)
@@ -31,9 +31,10 @@ Clear the cache.  Use this function if you no longer need lines from
 files previously read using \function{getline()}.
 \end{funcdesc}
 
-\begin{funcdesc}{checkcache}{}
+\begin{funcdesc}{checkcache}{\optional{filename}}
 Check the cache for validity.  Use this function if files in the cache 
-may have changed on disk, and you require the updated version.
+may have changed on disk, and you require the updated version.  If
+\var{filename} is omitted, it will check the whole cache entries.
 \end{funcdesc}
 
 Example:
index 1b70c7c230546bf54b19cf70a369f73d9fde8b13..2ccc6c67cb1248f0f2a686072153e3cd19539e8e 100644 (file)
@@ -40,11 +40,19 @@ def getlines(filename):
         return updatecache(filename)
 
 
-def checkcache():
+def checkcache(filename=None):
     """Discard cache entries that are out of date.
     (This is not checked upon each call!)"""
 
-    for filename in cache.keys():
+    if filename is None:
+        filenames = cache.keys()
+    else:
+        if filename in cache:
+            filenames = [filename]
+        else:
+            return
+
+    for filename in filenames:
         size, mtime, lines, fullname = cache[filename]
         try:
             stat = os.stat(fullname)
index dc22c752943dd4225441f8e3f97e39c5b35003dd..681209590c22a91b7b0f9aa8ce8ad52607b749a9 100644 (file)
@@ -40,6 +40,47 @@ class TracebackCases(unittest.TestCase):
         self.assert_(len(err) == 3)
         self.assert_(err[1].strip() == "[x for x in x] = x")
 
+    def test_bug737473(self):
+        import sys, os, tempfile
+        savedpath = sys.path[:]
+        testdir = tempfile.mkdtemp()
+        try:
+            sys.path.insert(0, testdir)
+            testfile = os.path.join(testdir, 'test_bug737473.py')
+            print >> open(testfile, 'w'), """\
+def test():
+    raise ValueError"""
+
+            if hasattr(os, 'utime'):
+                os.utime(testfile, (0, 0))
+            else:
+                import time
+                time.sleep(3) # not to stay in same mtime.
+
+            if 'test_bug737473' in sys.modules:
+                del sys.modules['test_bug737473']
+            import test_bug737473
+
+            try:
+                test_bug737473.test()
+            except ValueError:
+                # this loads source code to linecache 
+                traceback.extract_tb(sys.exc_traceback)
+
+            print >> open(testfile, 'w'), """\
+def test():
+    raise NotImplementedError"""
+            reload(test_bug737473)
+            try:
+                test_bug737473.test()
+            except NotImplementedError:
+                src = traceback.extract_tb(sys.exc_traceback)[-1][-1]
+                self.failUnlessEqual(src, 'raise NotImplementedError')
+        finally:
+            sys.path[:] = savedpath
+            for f in os.listdir(testdir):
+                os.unlink(os.path.join(testdir, f))
+            os.rmdir(testdir)
 
 def test_main():
     run_unittest(TracebackCases)
index cf78648c5339787bfafdf7b53f062dc6cc189bae..95cde2bd75ca792c6a54bda38c64cfcfe0dd1262 100644 (file)
@@ -65,6 +65,7 @@ def print_tb(tb, limit=None, file=None):
         name = co.co_name
         _print(file,
                '  File "%s", line %d, in %s' % (filename,lineno,name))
+        linecache.checkcache(filename)
         line = linecache.getline(filename, lineno)
         if line: _print(file, '    ' + line.strip())
         tb = tb.tb_next
@@ -96,6 +97,7 @@ def extract_tb(tb, limit = None):
         co = f.f_code
         filename = co.co_filename
         name = co.co_name
+        linecache.checkcache(filename)
         line = linecache.getline(filename, lineno)
         if line: line = line.strip()
         else: line = None
@@ -277,6 +279,7 @@ def extract_stack(f=None, limit = None):
         co = f.f_code
         filename = co.co_filename
         name = co.co_name
+        linecache.checkcache(filename)
         line = linecache.getline(filename, lineno)
         if line: line = line.strip()
         else: line = None
index 6c0935ea774f3ad8520bd5ba0453035b94036f29..59c3ef6835bd11f887673f16ead3493e0f6b480d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -59,6 +59,9 @@ Library
 
 - Bug #1017553: fix bug in tarfile.filemode()
 
+- Bug #737473: fix bug that old source code is shown in tracebacks even if
+  the source code is updated and reloaded.
+
 Build
 -----