[Zope-Checkins] CVS: Zope3 - test.py:1.4

Jim Fulton jim@zope.com
Wed, 19 Jun 2002 19:13:33 -0400


Update of /cvs-repository/Zope3
In directory cvs.zope.org:/tmp/cvs-serv32555

Modified Files:
	test.py 
Log Message:
Added some extra output for import errors in tests, showing the file,
line number, and line with the bad import in a format that works with
emacs next-error.


=== Zope3/test.py 1.3 => 1.4 ===
 import traceback
 import unittest
+import linecache
 from os.path import join
 
 from distutils.util import get_platform
@@ -214,6 +215,8 @@
     except ImportError, err:
         # print traceback
         print "Error importing %s\n%s" % (modname, err)
+        print_tb_last()
+        print
         if debug:
             raise
         return None
@@ -343,5 +346,32 @@
         raise
 
 
+
+def print_tb_last():
+    """Print up to 'limit' stack trace entries from the traceback 'tb'.
+
+    If 'limit' is omitted or None, all entries are printed.  If 'file'
+    is omitted or None, the output goes to sys.stderr; otherwise
+    'file' should be an open file or file-like object with a write()
+    method.
+    """
+    tb = sys.exc_info()[2]
+    file = sys.stderr
+    while 1:
+        f = tb.tb_frame
+        lineno = traceback.tb_lineno(tb)
+        tb = tb.tb_next
+        if tb is not None:
+            continue
+
+        co = f.f_code
+        filename = co.co_filename
+        name = co.co_name
+        file.write('  File "%s", line %d, in %s\n' % (filename,lineno,name))
+        line = linecache.getline(filename, lineno)
+        if line: file.write('    %s\n' % line.strip())
+        break
+
 if __name__ == "__main__":
     process_args()
+