2011-05-26 06:54:02 +08:00
|
|
|
import sys
|
|
|
|
import py
|
2012-04-13 18:41:02 +08:00
|
|
|
from _pytest.assertion.util import BuiltinAssertionError
|
2013-12-12 13:41:48 +08:00
|
|
|
u = py.builtin._totext
|
2011-05-26 06:54:02 +08:00
|
|
|
|
2013-11-25 01:45:48 +08:00
|
|
|
|
2011-05-26 06:54:02 +08:00
|
|
|
class AssertionError(BuiltinAssertionError):
|
|
|
|
def __init__(self, *args):
|
|
|
|
BuiltinAssertionError.__init__(self, *args)
|
|
|
|
if args:
|
2013-12-12 13:41:48 +08:00
|
|
|
# on Python2.6 we get len(args)==2 for: assert 0, (x,y)
|
|
|
|
# on Python2.7 and above we always get len(args) == 1
|
|
|
|
# with args[0] being the (x,y) tuple.
|
|
|
|
if len(args) > 1:
|
|
|
|
toprint = args
|
|
|
|
else:
|
|
|
|
toprint = args[0]
|
2011-05-26 06:54:02 +08:00
|
|
|
try:
|
2013-12-12 13:41:48 +08:00
|
|
|
self.msg = u(toprint)
|
2013-11-25 01:45:48 +08:00
|
|
|
except Exception:
|
2013-12-12 13:41:48 +08:00
|
|
|
self.msg = u(
|
2013-11-29 08:29:14 +08:00
|
|
|
"<[broken __repr__] %s at %0xd>"
|
2013-12-12 13:41:48 +08:00
|
|
|
% (toprint.__class__, id(toprint)))
|
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"
|
2014-07-17 08:21:18 +08:00
|
|
|
|
2014-07-17 14:35:36 +08:00
|
|
|
if sys.version_info >= (2, 6) or sys.platform.startswith("java"):
|
2011-05-26 06:54:02 +08:00
|
|
|
from _pytest.assertion.newinterpret import interpret as reinterpret
|
|
|
|
else:
|
2014-07-17 08:21:18 +08:00
|
|
|
from _pytest.assertion.oldinterpret import interpret as reinterpret
|