| 1 |
from pypy.interpreter.gateway import unwrap_spec |
| 2 |
|
| 3 |
def saferepr(space, w_obj): |
| 4 |
"""Get a safe repr of an object for assertion error messages. |
| 5 |
|
| 6 |
The assertion formatting (util.format_explanation()) requires |
| 7 |
newlines to be escaped since they are a special character for it. |
| 8 |
Normally assertion.util.format_explanation() does this but for a |
| 9 |
custom repr it is possible to contain one of the special escape |
| 10 |
sequences, especially '\n{' and '\n}' are likely to be present in |
| 11 |
JSON reprs. |
| 12 |
|
| 13 |
""" |
| 14 |
return space.newtext(space.text_w(space.repr(w_obj)).replace('\n', '\\n')) |
| 15 |
|
| 16 |
def format_assertmsg(space, __args__): |
| 17 |
"""Format the custom assertion message given. |
| 18 |
|
| 19 |
For strings this simply replaces newlines with '\n~' so that |
| 20 |
util.format_explanation() will preserve them instead of escaping |
| 21 |
newlines. For other objects py.io.saferepr() is used first. |
| 22 |
|
| 23 |
""" |
| 24 |
obj = space.text_w(__args__.firstarg()) |
| 25 |
return space.newtext(obj.replace("\n", "\n~").replace("%", "%%")) |
| 26 |
|
| 27 |
|
| 28 |
def _split_explanation(explanation): |
| 29 |
"""Return a list of individual lines in the explanation |
| 30 |
|
| 31 |
This will return a list of lines split on '\n{', '\n}' and '\n~'. |
| 32 |
Any other newlines will be escaped and appear in the line as the |
| 33 |
literal '\n' characters. |
| 34 |
""" |
| 35 |
raw_lines = (explanation or '').split('\n') |
| 36 |
lines = [raw_lines[0]] |
| 37 |
for values in raw_lines[1:]: |
| 38 |
if values and values[0] in ['{', '}', '~', '>']: |
| 39 |
lines.append(values) |
| 40 |
else: |
| 41 |
lines[-1] += '\\n' + values |
| 42 |
return lines |
| 43 |
|
| 44 |
def _format_lines(lines): |
| 45 |
"""Format the individual lines |
| 46 |
|
| 47 |
This will replace the '{', '}' and '~' characters of our mini |
| 48 |
formatting language with the proper 'where ...', 'and ...' and ' + |
| 49 |
...' text, taking care of indentation along the way. |
| 50 |
|
| 51 |
Return a list of formatted lines. |
| 52 |
""" |
| 53 |
result = lines[:1] |
| 54 |
stack = [0] |
| 55 |
stackcnt = [0] |
| 56 |
for line in lines[1:]: |
| 57 |
if line.startswith('{'): |
| 58 |
if stackcnt[-1]: |
| 59 |
s = 'and ' |
| 60 |
else: |
| 61 |
s = 'where ' |
| 62 |
stack.append(len(result)) |
| 63 |
stackcnt[-1] += 1 |
| 64 |
stackcnt.append(0) |
| 65 |
result.append(' +' + ' ' * (len(stack) - 1) + s + line[1:]) |
| 66 |
elif line.startswith('}'): |
| 67 |
stack.pop() |
| 68 |
stackcnt.pop() |
| 69 |
result[stack[-1]] += line[1:] |
| 70 |
else: |
| 71 |
assert line[0] in ['~', '>'] |
| 72 |
stack[-1] += 1 |
| 73 |
indent = len(stack) if line.startswith('~') else len(stack) - 1 |
| 74 |
result.append(' ' * indent + line[1:]) |
| 75 |
assert len(stack) == 1 |
| 76 |
return result |
| 77 |
|
| 78 |
@unwrap_spec(explanation="text") |
| 79 |
def format_explanation(space, explanation): |
| 80 |
"""This formats an explanation |
| 81 |
|
| 82 |
Normally all embedded newlines are escaped, however there are |
| 83 |
three exceptions: \n{, \n} and \n~. The first two are intended |
| 84 |
cover nested explanations, see function and attribute explanations |
| 85 |
for examples (.visit_Call(), visit_Attribute()). The last one is |
| 86 |
for when one explanation needs to span multiple lines, e.g. when |
| 87 |
displaying diffs. |
| 88 |
""" |
| 89 |
lines = _split_explanation(explanation) |
| 90 |
result = _format_lines(lines) |
| 91 |
return space.newtext('\n'.join(result)) |
| 92 |
|
| 93 |
def should_repr_global_name(space, w_obj): |
| 94 |
return space.newbool( |
| 95 |
space.findattr(w_obj, space.newtext("__name__")) is None and |
| 96 |
not space.callable_w(w_obj)) |
| 97 |
|
| 98 |
|
| 99 |
@unwrap_spec(is_or=bool) |
| 100 |
def format_boolop(space, w_explanations, is_or): |
| 101 |
explanations_w = space.unpackiterable(w_explanations) |
| 102 |
explanation = "(" + (is_or and " or " or " and ").join([space.text_w(w_e) for w_e in explanations_w]) + ")" |
| 103 |
return space.newtext(explanation.replace('%', '%%')) |
| 104 |
|
| 105 |
|
| 106 |
def call_reprcompare(space, w_ops, w_results, w_expls, w_each_obj): |
| 107 |
ops_w = space.unpackiterable(w_ops) |
| 108 |
results_w = space.unpackiterable(w_results) |
| 109 |
expls_w = space.unpackiterable(w_expls) |
| 110 |
each_obj_w = space.unpackiterable(w_each_obj) |
| 111 |
for i, w_res, w_expl in zip(range(len(ops_w)), results_w, expls_w): |
| 112 |
try: |
| 113 |
done = space.is_true(space.not_(w_res)) |
| 114 |
except Exception: |
| 115 |
done = True |
| 116 |
if done: |
| 117 |
break |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
return w_expl |
| 123 |
|