test_ok1/_pytest/assertion/reinterpret.py

47 lines
1.7 KiB
Python
Raw Normal View History

2011-05-26 06:54:02 +08:00
import sys
import py
from _pytest.assertion.util import BuiltinAssertionError
2011-05-26 06:54:02 +08:00
2011-05-26 06:54:02 +08:00
class AssertionError(BuiltinAssertionError):
def __init__(self, *args):
BuiltinAssertionError.__init__(self, *args)
if args:
try:
self.msg = py.builtin._totext(args[0])
except Exception:
self.msg = py.builtin._totext(
"<[broken __repr__] %s at %0xd> %s"
% (args[0].__class__, id(args[0])))
2011-05-26 06:54:02 +08:00
else:
f = py.code.Frame(sys._getframe(1))
try:
source = f.code.fullsource
if source is not None:
try:
source = source.getstatement(f.lineno, assertion=True)
except IndexError:
source = None
else:
source = str(source.deindent()).strip()
except py.error.ENOENT:
source = None
# this can also occur during reinterpretation, when the
# co_filename is set to "<run>".
if source:
self.msg = reinterpret(source, f, should_fail=True)
else:
self.msg = "<could not determine information>"
if not self.args:
self.args = (self.msg,)
if sys.version_info > (3, 0):
AssertionError.__module__ = "builtins"
reinterpret_old = "old reinterpretation not available for py3"
else:
from _pytest.assertion.oldinterpret import interpret as reinterpret_old
if sys.version_info >= (2, 6) or (sys.platform.startswith("java")):
from _pytest.assertion.newinterpret import interpret as reinterpret
else:
reinterpret = reinterpret_old