~mars/zope.testing/3.9.4-p0

« back to all changes in this revision

Viewing changes to src/zope/testing/testrunner/debug.py

  • Committer: Maris Fogels
  • Date: 2010-06-04 14:58:44 UTC
  • Revision ID: maris.fogels@canonical.com-20100604145844-mb48c1ig9gty2kao
Initial import of zope.testing's 3.9.4 SVN tag

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2004-2008 Zope Foundation and Contributors.
 
4
# All Rights Reserved.
 
5
#
 
6
# This software is subject to the provisions of the Zope Public License,
 
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
 
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
 
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 
11
# FOR A PARTICULAR PURPOSE.
 
12
#
 
13
##############################################################################
 
14
"""Debug functions
 
15
 
 
16
$Id: __init__.py 86232 2008-05-03 15:09:33Z ctheune $
 
17
 
 
18
"""
 
19
 
 
20
import doctest
 
21
import sys
 
22
import pdb
 
23
 
 
24
import zope.testing.testrunner.interfaces
 
25
 
 
26
 
 
27
def post_mortem(exc_info):
 
28
    err = exc_info[1]
 
29
    if isinstance(err, (doctest.UnexpectedException, doctest.DocTestFailure)):
 
30
 
 
31
        if isinstance(err, doctest.UnexpectedException):
 
32
            exc_info = err.exc_info
 
33
 
 
34
            # Print out location info if the error was in a doctest
 
35
            if exc_info[2].tb_frame.f_code.co_filename == '<string>':
 
36
                print_doctest_location(err)
 
37
 
 
38
        else:
 
39
            print_doctest_location(err)
 
40
            # Hm, we have a DocTestFailure exception.  We need to
 
41
            # generate our own traceback
 
42
            try:
 
43
                exec ('raise ValueError'
 
44
                      '("Expected and actual output are different")'
 
45
                      ) in err.test.globs
 
46
            except:
 
47
                exc_info = sys.exc_info()
 
48
 
 
49
    print "%s:" % (exc_info[0], )
 
50
    print exc_info[1]
 
51
    pdb.post_mortem(exc_info[2])
 
52
    raise zope.testing.testrunner.interfaces.EndRun()
 
53
 
 
54
 
 
55
def print_doctest_location(err):
 
56
    # This mimics pdb's output, which gives way cool results in emacs :)
 
57
    filename = err.test.filename
 
58
    if filename.endswith('.pyc'):
 
59
        filename = filename[:-1]
 
60
    print "> %s(%s)_()" % (filename, err.test.lineno+err.example.lineno+1)