2011-05-27 02:17:26 +08:00
|
|
|
"""Utilities for assertion debugging"""
|
2014-08-01 06:13:40 +08:00
|
|
|
import pprint
|
2011-05-27 02:17:26 +08:00
|
|
|
|
|
|
|
import py
|
2013-08-01 20:48:34 +08:00
|
|
|
try:
|
2013-12-18 21:56:45 +08:00
|
|
|
from collections import Sequence
|
2013-08-01 20:48:34 +08:00
|
|
|
except ImportError:
|
2013-12-18 21:56:45 +08:00
|
|
|
Sequence = list
|
2011-05-27 02:17:26 +08:00
|
|
|
|
2012-04-13 18:41:02 +08:00
|
|
|
BuiltinAssertionError = py.builtin.builtins.AssertionError
|
2013-11-22 20:28:59 +08:00
|
|
|
u = py.builtin._totext
|
2011-05-27 02:17:26 +08:00
|
|
|
|
|
|
|
# The _reprcompare attribute on the util module is used by the new assertion
|
|
|
|
# interpretation code and assertion rewriter to detect this plugin was
|
|
|
|
# loaded and in turn call the hooks defined here as part of the
|
|
|
|
# DebugInterpreter.
|
|
|
|
_reprcompare = None
|
|
|
|
|
2013-04-29 03:56:56 +08:00
|
|
|
|
2011-05-27 02:17:26 +08:00
|
|
|
def format_explanation(explanation):
|
|
|
|
"""This formats an explanation
|
|
|
|
|
|
|
|
Normally all embedded newlines are escaped, however there are
|
|
|
|
three exceptions: \n{, \n} and \n~. The first two are intended
|
|
|
|
cover nested explanations, see function and attribute explanations
|
|
|
|
for examples (.visit_Call(), visit_Attribute()). The last one is
|
|
|
|
for when one explanation needs to span multiple lines, e.g. when
|
|
|
|
displaying diffs.
|
|
|
|
"""
|
2013-11-22 20:28:59 +08:00
|
|
|
explanation = _collapse_false(explanation)
|
|
|
|
lines = _split_explanation(explanation)
|
|
|
|
result = _format_lines(lines)
|
|
|
|
return u('\n').join(result)
|
|
|
|
|
|
|
|
|
|
|
|
def _collapse_false(explanation):
|
|
|
|
"""Collapse expansions of False
|
|
|
|
|
|
|
|
So this strips out any "assert False\n{where False = ...\n}"
|
|
|
|
blocks.
|
|
|
|
"""
|
2011-06-13 11:41:58 +08:00
|
|
|
where = 0
|
|
|
|
while True:
|
|
|
|
start = where = explanation.find("False\n{False = ", where)
|
|
|
|
if where == -1:
|
|
|
|
break
|
|
|
|
level = 0
|
|
|
|
for i, c in enumerate(explanation[start:]):
|
|
|
|
if c == "{":
|
|
|
|
level += 1
|
|
|
|
elif c == "}":
|
|
|
|
level -= 1
|
|
|
|
if not level:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise AssertionError("unbalanced braces: %r" % (explanation,))
|
|
|
|
end = start + i
|
|
|
|
where = end
|
|
|
|
if explanation[end - 1] == '\n':
|
|
|
|
explanation = (explanation[:start] + explanation[start+15:end-1] +
|
|
|
|
explanation[end+1:])
|
|
|
|
where -= 17
|
2013-11-22 20:28:59 +08:00
|
|
|
return explanation
|
|
|
|
|
|
|
|
|
|
|
|
def _split_explanation(explanation):
|
|
|
|
"""Return a list of individual lines in the explanation
|
|
|
|
|
|
|
|
This will return a list of lines split on '\n{', '\n}' and '\n~'.
|
|
|
|
Any other newlines will be escaped and appear in the line as the
|
|
|
|
literal '\n' characters.
|
|
|
|
"""
|
|
|
|
raw_lines = (explanation or u('')).split('\n')
|
2011-05-27 02:17:26 +08:00
|
|
|
lines = [raw_lines[0]]
|
|
|
|
for l in raw_lines[1:]:
|
2014-08-24 00:14:25 +08:00
|
|
|
if l and l[0] in ['{', '}', '~', '>']:
|
2011-05-27 02:17:26 +08:00
|
|
|
lines.append(l)
|
|
|
|
else:
|
|
|
|
lines[-1] += '\\n' + l
|
2013-11-22 20:28:59 +08:00
|
|
|
return lines
|
|
|
|
|
|
|
|
|
|
|
|
def _format_lines(lines):
|
|
|
|
"""Format the individual lines
|
2011-05-27 02:17:26 +08:00
|
|
|
|
2013-11-22 20:28:59 +08:00
|
|
|
This will replace the '{', '}' and '~' characters of our mini
|
|
|
|
formatting language with the proper 'where ...', 'and ...' and ' +
|
|
|
|
...' text, taking care of indentation along the way.
|
|
|
|
|
|
|
|
Return a list of formatted lines.
|
|
|
|
"""
|
2011-05-27 02:17:26 +08:00
|
|
|
result = lines[:1]
|
|
|
|
stack = [0]
|
|
|
|
stackcnt = [0]
|
|
|
|
for line in lines[1:]:
|
|
|
|
if line.startswith('{'):
|
|
|
|
if stackcnt[-1]:
|
2013-11-22 20:28:59 +08:00
|
|
|
s = u('and ')
|
2011-05-27 02:17:26 +08:00
|
|
|
else:
|
2013-11-22 20:28:59 +08:00
|
|
|
s = u('where ')
|
2011-05-27 02:17:26 +08:00
|
|
|
stack.append(len(result))
|
|
|
|
stackcnt[-1] += 1
|
|
|
|
stackcnt.append(0)
|
2013-11-22 20:28:59 +08:00
|
|
|
result.append(u(' +') + u(' ')*(len(stack)-1) + s + line[1:])
|
2011-05-27 02:17:26 +08:00
|
|
|
elif line.startswith('}'):
|
|
|
|
stack.pop()
|
|
|
|
stackcnt.pop()
|
|
|
|
result[stack[-1]] += line[1:]
|
|
|
|
else:
|
2014-08-24 00:14:25 +08:00
|
|
|
assert line[0] in ['~', '>']
|
|
|
|
stack[-1] += 1
|
|
|
|
indent = len(stack) if line.startswith('~') else len(stack) - 1
|
|
|
|
result.append(u(' ')*indent + line[1:])
|
2011-05-27 02:17:26 +08:00
|
|
|
assert len(stack) == 1
|
2013-11-22 20:28:59 +08:00
|
|
|
return result
|
2011-05-27 02:17:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Provide basestring in python3
|
|
|
|
try:
|
|
|
|
basestring = basestring
|
|
|
|
except NameError:
|
|
|
|
basestring = str
|
|
|
|
|
|
|
|
|
2013-03-28 09:39:01 +08:00
|
|
|
def assertrepr_compare(config, op, left, right):
|
|
|
|
"""Return specialised explanations for some operators/operands"""
|
2013-04-29 03:56:56 +08:00
|
|
|
width = 80 - 15 - len(op) - 2 # 15 chars indentation, 1 space around op
|
2013-11-29 08:29:14 +08:00
|
|
|
left_repr = py.io.saferepr(left, maxsize=int(width/2))
|
2011-05-27 02:17:26 +08:00
|
|
|
right_repr = py.io.saferepr(right, maxsize=width-len(left_repr))
|
2013-11-22 20:28:59 +08:00
|
|
|
summary = u('%s %s %s') % (left_repr, op, right_repr)
|
2011-05-27 02:17:26 +08:00
|
|
|
|
2013-08-01 20:48:34 +08:00
|
|
|
issequence = lambda x: (isinstance(x, (list, tuple, Sequence))
|
|
|
|
and not isinstance(x, basestring))
|
2011-05-27 02:17:26 +08:00
|
|
|
istext = lambda x: isinstance(x, basestring)
|
|
|
|
isdict = lambda x: isinstance(x, dict)
|
2013-04-29 03:59:10 +08:00
|
|
|
isset = lambda x: isinstance(x, (set, frozenset))
|
2011-05-27 02:17:26 +08:00
|
|
|
|
2013-03-28 09:39:01 +08:00
|
|
|
verbose = config.getoption('verbose')
|
2011-05-27 02:17:26 +08:00
|
|
|
explanation = None
|
|
|
|
try:
|
|
|
|
if op == '==':
|
|
|
|
if istext(left) and istext(right):
|
2013-03-28 09:39:01 +08:00
|
|
|
explanation = _diff_text(left, right, verbose)
|
2011-05-27 02:17:26 +08:00
|
|
|
elif issequence(left) and issequence(right):
|
2013-03-28 09:39:01 +08:00
|
|
|
explanation = _compare_eq_sequence(left, right, verbose)
|
2011-05-27 02:17:26 +08:00
|
|
|
elif isset(left) and isset(right):
|
2013-03-28 09:39:01 +08:00
|
|
|
explanation = _compare_eq_set(left, right, verbose)
|
2011-05-27 02:17:26 +08:00
|
|
|
elif isdict(left) and isdict(right):
|
2013-03-28 09:39:01 +08:00
|
|
|
explanation = _compare_eq_dict(left, right, verbose)
|
2011-05-27 02:17:26 +08:00
|
|
|
elif op == 'not in':
|
|
|
|
if istext(left) and istext(right):
|
2013-03-28 09:39:01 +08:00
|
|
|
explanation = _notin_text(left, right, verbose)
|
2013-11-22 20:28:59 +08:00
|
|
|
except Exception:
|
2013-04-29 03:56:56 +08:00
|
|
|
explanation = [
|
2013-11-22 20:28:59 +08:00
|
|
|
u('(pytest_assertion plugin: representation of details failed. '
|
|
|
|
'Probably an object has a faulty __repr__.)'),
|
2014-04-02 21:34:36 +08:00
|
|
|
u(py.code.ExceptionInfo())]
|
2011-05-27 02:17:26 +08:00
|
|
|
|
|
|
|
if not explanation:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return [summary] + explanation
|
|
|
|
|
|
|
|
|
2013-03-28 09:39:01 +08:00
|
|
|
def _diff_text(left, right, verbose=False):
|
2014-01-29 08:42:58 +08:00
|
|
|
"""Return the explanation for the diff between text or bytes
|
2011-05-27 02:17:26 +08:00
|
|
|
|
2013-03-28 09:39:01 +08:00
|
|
|
Unless --verbose is used this will skip leading and trailing
|
|
|
|
characters which are identical to keep the diff minimal.
|
2014-01-29 08:42:58 +08:00
|
|
|
|
|
|
|
If the input are bytes they will be safely converted to text.
|
2011-05-27 02:17:26 +08:00
|
|
|
"""
|
2014-08-01 06:13:40 +08:00
|
|
|
from difflib import ndiff
|
2011-05-27 02:17:26 +08:00
|
|
|
explanation = []
|
2014-01-29 08:42:58 +08:00
|
|
|
if isinstance(left, py.builtin.bytes):
|
|
|
|
left = u(repr(left)[1:-1]).replace(r'\n', '\n')
|
|
|
|
if isinstance(right, py.builtin.bytes):
|
|
|
|
right = u(repr(right)[1:-1]).replace(r'\n', '\n')
|
2013-03-28 09:39:01 +08:00
|
|
|
if not verbose:
|
2013-04-29 03:56:56 +08:00
|
|
|
i = 0 # just in case left or right has zero length
|
2013-03-28 09:39:01 +08:00
|
|
|
for i in range(min(len(left), len(right))):
|
|
|
|
if left[i] != right[i]:
|
2011-05-27 02:17:26 +08:00
|
|
|
break
|
|
|
|
if i > 42:
|
2013-03-28 09:39:01 +08:00
|
|
|
i -= 10 # Provide some context
|
2013-11-22 20:28:59 +08:00
|
|
|
explanation = [u('Skipping %s identical leading '
|
|
|
|
'characters in diff, use -v to show') % i]
|
2013-03-28 09:39:01 +08:00
|
|
|
left = left[i:]
|
|
|
|
right = right[i:]
|
|
|
|
if len(left) == len(right):
|
|
|
|
for i in range(len(left)):
|
|
|
|
if left[-i] != right[-i]:
|
|
|
|
break
|
|
|
|
if i > 42:
|
|
|
|
i -= 10 # Provide some context
|
2013-11-22 20:28:59 +08:00
|
|
|
explanation += [u('Skipping %s identical trailing '
|
|
|
|
'characters in diff, use -v to show') % i]
|
2013-03-28 09:39:01 +08:00
|
|
|
left = left[:-i]
|
|
|
|
right = right[:-i]
|
2011-05-27 02:17:26 +08:00
|
|
|
explanation += [line.strip('\n')
|
2014-08-01 06:13:40 +08:00
|
|
|
for line in ndiff(left.splitlines(),
|
|
|
|
right.splitlines())]
|
2011-05-27 02:17:26 +08:00
|
|
|
return explanation
|
|
|
|
|
|
|
|
|
2013-03-28 09:39:01 +08:00
|
|
|
def _compare_eq_sequence(left, right, verbose=False):
|
2011-05-27 02:17:26 +08:00
|
|
|
explanation = []
|
|
|
|
for i in range(min(len(left), len(right))):
|
|
|
|
if left[i] != right[i]:
|
2013-11-22 20:28:59 +08:00
|
|
|
explanation += [u('At index %s diff: %r != %r')
|
|
|
|
% (i, left[i], right[i])]
|
2011-05-27 02:17:26 +08:00
|
|
|
break
|
|
|
|
if len(left) > len(right):
|
2013-11-22 20:28:59 +08:00
|
|
|
explanation += [u('Left contains more items, first extra item: %s')
|
|
|
|
% py.io.saferepr(left[len(right)],)]
|
2011-05-27 02:17:26 +08:00
|
|
|
elif len(left) < len(right):
|
2013-04-29 03:56:56 +08:00
|
|
|
explanation += [
|
2013-11-22 20:28:59 +08:00
|
|
|
u('Right contains more items, first extra item: %s') %
|
2013-04-29 03:56:56 +08:00
|
|
|
py.io.saferepr(right[len(left)],)]
|
2014-08-01 06:13:40 +08:00
|
|
|
return explanation # + _diff_text(pprint.pformat(left),
|
|
|
|
# pprint.pformat(right))
|
2011-05-27 02:17:26 +08:00
|
|
|
|
|
|
|
|
2013-03-28 09:39:01 +08:00
|
|
|
def _compare_eq_set(left, right, verbose=False):
|
2011-05-27 02:17:26 +08:00
|
|
|
explanation = []
|
|
|
|
diff_left = left - right
|
|
|
|
diff_right = right - left
|
|
|
|
if diff_left:
|
2013-11-22 20:28:59 +08:00
|
|
|
explanation.append(u('Extra items in the left set:'))
|
2011-05-27 02:17:26 +08:00
|
|
|
for item in diff_left:
|
|
|
|
explanation.append(py.io.saferepr(item))
|
|
|
|
if diff_right:
|
2013-11-22 20:28:59 +08:00
|
|
|
explanation.append(u('Extra items in the right set:'))
|
2011-05-27 02:17:26 +08:00
|
|
|
for item in diff_right:
|
|
|
|
explanation.append(py.io.saferepr(item))
|
|
|
|
return explanation
|
|
|
|
|
|
|
|
|
2013-03-28 09:39:01 +08:00
|
|
|
def _compare_eq_dict(left, right, verbose=False):
|
|
|
|
explanation = []
|
|
|
|
common = set(left).intersection(set(right))
|
|
|
|
same = dict((k, left[k]) for k in common if left[k] == right[k])
|
|
|
|
if same and not verbose:
|
2013-11-22 20:28:59 +08:00
|
|
|
explanation += [u('Omitting %s identical items, use -v to show') %
|
2013-03-28 09:39:01 +08:00
|
|
|
len(same)]
|
|
|
|
elif same:
|
2013-11-22 20:28:59 +08:00
|
|
|
explanation += [u('Common items:')]
|
2014-08-01 06:13:40 +08:00
|
|
|
explanation += pprint.pformat(same).splitlines()
|
2013-03-28 09:39:01 +08:00
|
|
|
diff = set(k for k in common if left[k] != right[k])
|
|
|
|
if diff:
|
2013-11-22 20:28:59 +08:00
|
|
|
explanation += [u('Differing items:')]
|
2013-03-28 09:39:01 +08:00
|
|
|
for k in diff:
|
|
|
|
explanation += [py.io.saferepr({k: left[k]}) + ' != ' +
|
|
|
|
py.io.saferepr({k: right[k]})]
|
|
|
|
extra_left = set(left) - set(right)
|
|
|
|
if extra_left:
|
2013-11-22 20:28:59 +08:00
|
|
|
explanation.append(u('Left contains more items:'))
|
2014-08-01 06:13:40 +08:00
|
|
|
explanation.extend(pprint.pformat(
|
2013-04-29 03:56:56 +08:00
|
|
|
dict((k, left[k]) for k in extra_left)).splitlines())
|
2013-03-28 09:39:01 +08:00
|
|
|
extra_right = set(right) - set(left)
|
|
|
|
if extra_right:
|
2013-11-22 20:28:59 +08:00
|
|
|
explanation.append(u('Right contains more items:'))
|
2014-08-01 06:13:40 +08:00
|
|
|
explanation.extend(pprint.pformat(
|
2013-04-29 03:56:56 +08:00
|
|
|
dict((k, right[k]) for k in extra_right)).splitlines())
|
2013-03-28 09:39:01 +08:00
|
|
|
return explanation
|
|
|
|
|
|
|
|
|
|
|
|
def _notin_text(term, text, verbose=False):
|
2011-05-27 02:17:26 +08:00
|
|
|
index = text.find(term)
|
|
|
|
head = text[:index]
|
|
|
|
tail = text[index+len(term):]
|
|
|
|
correct_text = head + tail
|
2013-03-28 09:39:01 +08:00
|
|
|
diff = _diff_text(correct_text, text, verbose)
|
2013-11-22 20:28:59 +08:00
|
|
|
newdiff = [u('%s is contained here:') % py.io.saferepr(term, maxsize=42)]
|
2011-05-27 02:17:26 +08:00
|
|
|
for line in diff:
|
2013-11-22 20:28:59 +08:00
|
|
|
if line.startswith(u('Skipping')):
|
2011-05-27 02:17:26 +08:00
|
|
|
continue
|
2013-11-22 20:28:59 +08:00
|
|
|
if line.startswith(u('- ')):
|
2011-05-27 02:17:26 +08:00
|
|
|
continue
|
2013-11-22 20:28:59 +08:00
|
|
|
if line.startswith(u('+ ')):
|
|
|
|
newdiff.append(u(' ') + line[2:])
|
2011-05-27 02:17:26 +08:00
|
|
|
else:
|
|
|
|
newdiff.append(line)
|
|
|
|
return newdiff
|