-
Notifications
You must be signed in to change notification settings - Fork 4
/
explain_output_differences.py
executable file
·432 lines (407 loc) · 16.2 KB
/
explain_output_differences.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
"""Explain output differences.
This module explains the differences between expect and actual output
in a way comprehensible to a novice programmer.
Todo:
* Docstrings need to be added
* This code needs extensive rewriting
"""
import difflib
from termcolor import colored as termcolor_colored
from collections import defaultdict
def explain_output_differences(
name,
expected,
canonical_expected,
canonical_expected_plus_newlines,
actual,
canonical_actual,
canonical_actual_plus_newlines,
show_expected_output=True,
show_actual_output=True,
show_diff=True,
max_lines_shown=32,
show_all_lines=False,
max_line_length_shown=1024,
debug=False,
**parameters,
):
max_lines_shown = int(max_lines_shown)
max_line_length_shown = int(max_line_length_shown)
colored = (
termcolor_colored if parameters["colorize_output"] else lambda x, *a, **kw: x
)
if canonical_expected and not actual:
if name == "output":
return colored("Your program produced no output\n", "red")
word = "on" if name == "stderr" else "in"
explanation = colored(f"Your program produced no output {word} {name}\n", "red")
if show_expected_output:
explanation += f"\nThe correct {name} for this test was:\n"
explanation += colored(
sanitize_string(
expected,
max_lines_shown=max_lines_shown,
show_all_lines=show_all_lines,
max_line_length_shown=max_line_length_shown,
**parameters,
),
"green",
)
return explanation
if debug:
print(f"explain_output_differences({name}, '{expected}', '{actual}')")
# check if output is correct but just missing a character
# but don't use this for short outputs
if canonical_expected[0:-1] == canonical_actual and (
len(canonical_actual) > 8
or (canonical_actual and canonical_actual[-1] in ". \n")
):
missing_char = colored(repr(canonical_expected[-1]), "red")
return f"Your program's {name} was correct except it was missing a {missing_char} character on the end.\n"
# check if output is correct but has an extra character
# but don't use this for short outputs
# there is a special case where an extra '\n' will cause trailing spaces on the last line of expected output
# to be stripped actual[0:-1] == canonical_expected remedies that in some cases
if (
canonical_actual[0:-1] == canonical_expected
or actual[0:-1] == canonical_expected
) and (
len(canonical_actual) > 8
or (canonical_actual and canonical_actual[-1] in ". \n")
):
extra_char = colored(repr(canonical_actual[-1]), "red")
suffix = ""
if "\ufffd" == canonical_actual[-1]:
extra_char = colored("'\\xff'", "red")
suffix = (
"This can result from printing the EOF value returned by getchar.\n"
)
return f"Your program's {name} was correct except it had an extra {extra_char} character on the end.\n{suffix}"
actual_line_color = defaultdict(lambda: "green")
explanation = ""
actual_lines = actual.splitlines()
n_actual_lines = len(actual_lines)
expected_lines = expected.splitlines()
canonical_actual_lines = canonical_actual_plus_newlines.splitlines()
canonical_expected_lines = canonical_expected_plus_newlines.splitlines()
n_canonical_expected_lines = len(canonical_expected_lines)
if n_actual_lines > 1:
actual_description = (
f"Your program produced these {n_actual_lines} lines of {name}:\n"
)
else:
actual_description = f"Your program produced this line of {name}:\n"
if not expected and actual:
explanation = colored(
f"No {name} was expected for this test and your program produced {n_actual_lines} lines of {name}\n",
"red",
)
if show_actual_output:
explanation += actual_description
explanation += sanitize_string(
actual,
max_lines_shown=max_lines_shown,
show_all_lines=show_all_lines,
max_line_length_shown=max_line_length_shown,
line_color=defaultdict(
lambda: "red" if parameters["colorize_output"] else ""
),
**parameters,
)
return explanation
if (
len(canonical_expected_lines) == len(canonical_actual_lines)
and 2 < len(canonical_actual_lines) < 1000
and sorted(canonical_expected_lines) == sorted(canonical_actual_lines)
):
explanation += colored(
f"\nYour program produced the correct {name} lines but in the wrong order.\n",
"red",
)
# FIXME changes canonical_actual_lines, canonical_expected_lines, actual_lines, expected_lines
diff_explanation = create_diff(
canonical_actual_lines,
canonical_expected_lines,
actual_lines,
expected_lines,
name,
colored,
max_lines_shown,
show_all_lines,
debug,
actual_line_color,
)
if not parameters["colorize_output"]:
actual_line_color = defaultdict(lambda: "")
if show_actual_output:
if actual_lines:
explanation += actual_description
explanation += sanitize_string(
actual,
max_lines_shown=max_lines_shown,
show_all_lines=show_all_lines,
max_line_length_shown=max_line_length_shown,
line_color=actual_line_color,
**parameters,
)
if expected and actual and actual[-1] != "\n" and expected[-1] == "\n":
explanation += "Last line of output above was not terminated with a newline('\\n') character\n"
if show_expected_output and canonical_expected_lines:
explanation += f"\nThe correct {n_canonical_expected_lines} lines of {name} for this test were:\n"
explanation += colored(
sanitize_string(
expected,
max_lines_shown=max_lines_shown,
show_all_lines=show_all_lines,
max_line_length_shown=max_line_length_shown,
**parameters,
),
"green",
)
if not actual_lines or not show_diff:
return explanation
# check if the difference is a simple character transliteration
if 8 < len(canonical_expected) < 1000:
actual_chars = set(canonical_actual)
expected_chars = set(canonical_expected)
actual_not_expected = actual_chars - expected_chars
expected_not_actual = expected_chars - actual_chars
if len(actual_not_expected) == 1 and len(expected_not_actual) == 1:
actual_char = actual_not_expected.pop()
expected_char = expected_not_actual.pop()
if (
canonical_actual.replace(actual_char, expected_char)
== canonical_expected
):
n = canonical_actual.count(actual_char)
if n == 1:
format_str = "a '%s' with a '%s' character."
else:
format_str = "all '%s' characters with '%s' characters."
explanation += (
f"\nYour program's {name} would be correct if you replaced "
)
explanation += format_str % (
colored(actual_char, "red"),
colored(expected_char, "green"),
)
explanation += "\n"
return explanation
if len(actual_not_expected) == 1 and len(expected_not_actual) == 0:
actual_char = actual_not_expected.pop()
if canonical_actual.replace(actual_char, "") == canonical_expected:
n = canonical_actual.count(actual_char)
if n == 1:
format_str = "a '%s' character."
else:
format_str = "all '%s' characters."
explanation += f"Your program's {name} would be correct if you removed "
explanation += format_str % (colored(actual_char, "red"))
explanation += "\n"
return explanation
if diff_explanation:
if show_all_lines:
explanation += "\n" + "\n".join(diff_explanation) + "\n"
else:
explanation += "\n" + "\n".join(diff_explanation[0:max_lines_shown]) + "\n"
return explanation
def create_diff(
canonical_actual_lines,
canonical_expected_lines,
actual_lines,
expected_lines,
name,
colored,
max_lines_shown,
show_all_lines,
debug,
actual_line_color,
):
prefix_removed = 0
# difflib.ndiff is horrendously slow so clear matching prefix & suffix
# but leave 1 line of matching prefix and suffix for context
if (
canonical_actual_lines
and canonical_expected_lines
and canonical_actual_lines[0] == canonical_expected_lines[0]
and not show_all_lines
):
while (
len(canonical_actual_lines) > 1
and len(canonical_expected_lines) > 1
and canonical_actual_lines[1] == canonical_expected_lines[1]
):
actual_lines.pop(0)
expected_lines.pop(0)
canonical_actual_lines.pop(0)
canonical_expected_lines.pop(0)
prefix_removed += 1
# don't leave empty prefixes - this triggers a difflib bug?
if canonical_actual_lines[0:1] == [""] and canonical_expected_lines[0:1] == [""]:
actual_lines.pop(0)
expected_lines.pop(0)
canonical_actual_lines.pop(0)
canonical_expected_lines.pop(0)
prefix_removed += 1
suffix_removed = 0
if (
canonical_actual_lines
and canonical_expected_lines
and canonical_actual_lines[-1] == canonical_expected_lines[-1]
and not show_all_lines
):
while (
len(canonical_actual_lines) > 1
and len(canonical_expected_lines) > 1
and canonical_actual_lines[-2] == canonical_expected_lines[-2]
):
actual_lines.pop()
expected_lines.pop()
canonical_actual_lines.pop()
canonical_expected_lines.pop()
suffix_removed += 1
# don't leave empty suffix in case this also triggers a difflib bug?
if canonical_actual_lines[-1:] == [""] and canonical_expected_lines[-1:] == [""]:
actual_lines.pop()
expected_lines.pop()
canonical_actual_lines.pop()
canonical_expected_lines.pop()
suffix_removed += 1
maximum_len_for_diff = 128
diff_truncated = False
if not show_all_lines and (
len(canonical_expected_lines) > maximum_len_for_diff
or len(canonical_actual_lines) > maximum_len_for_diff
):
actual_lines = actual_lines[0:maximum_len_for_diff]
expected_lines = expected_lines[0:maximum_len_for_diff]
canonical_actual_lines = canonical_actual_lines[0:maximum_len_for_diff]
canonical_expected_lines = canonical_expected_lines[0:maximum_len_for_diff]
diff_truncated = True
expected_line_number = 0
actual_line_number = 0
diff = difflib.ndiff(canonical_actual_lines, canonical_expected_lines)
diff_explanation = [
f"The difference between your {name}({colored('-', 'red')})"
+ f" and the correct {name}({colored('+', 'green')}) is:"
]
if prefix_removed:
diff_explanation.append("...")
try:
d = ""
last_line_in_diff = False
context_line = None
dotdotdot_added = False
for diff_line in diff:
if not diff_line: # Why would this happen
continue
last_d = d
d = diff_line[0]
if (
d in "-+"
and diff_explanation
and context_line
and diff_explanation[-1] == "..."
):
diff_explanation[-1] = context_line
context_line = None
if d == "-":
diff_explanation.append(
colored("- " + actual_lines[actual_line_number], "red")
)
actual_line_color[prefix_removed + actual_line_number] = "red"
actual_line_number += 1
elif d == "+":
diff_explanation.append(
colored("+ " + expected_lines[expected_line_number], "green")
)
expected_line_number += 1
elif d == "?":
# include column marker only if line has not been changed before diff
if last_d == "+":
if (
canonical_expected_lines[expected_line_number - 1]
== expected_lines[expected_line_number - 1]
):
diff_explanation.append(diff_line)
elif last_d == "-":
if (
canonical_actual_lines[actual_line_number - 1]
== actual_lines[actual_line_number - 1]
):
diff_explanation.append(diff_line)
elif d == " ":
context_line = " " + actual_lines[actual_line_number]
if last_line_in_diff:
diff_explanation.append(context_line)
elif not dotdotdot_added:
if not show_all_lines:
diff_explanation.append("...")
dotdotdot_added = True
else:
diff_explanation.append(context_line)
actual_line_number += 1
expected_line_number += 1
if d in "-+":
dotdotdot_added = False
last_line_in_diff = True
else:
last_line_in_diff = False
if len(diff_explanation) > 2 * max_lines_shown and not show_all_lines:
break
if (diff_truncated or suffix_removed) and diff_explanation[-1] != "...":
diff_explanation.append("...")
except IndexError:
if debug:
print("IndexError: unexpected diff output")
# unexpected diff output might break above code
return diff_explanation
def sanitize_string(
unsanitized_string,
leave_tabs=False,
leave_colorization=False,
max_lines_shown=32,
show_all_lines=False,
max_line_length_shown=1024,
# pylint: disable=dangerous-default-value
line_color=defaultdict(lambda: ""),
**parameters,
):
max_lines_shown = int(max_lines_shown)
max_line_length_shown = int(max_line_length_shown)
lines = unsanitized_string.splitlines()
append_repeat_message = False
if len(lines) >= max_lines_shown and not show_all_lines:
last_line_index = len(lines) - 1
last_line = lines[last_line_index]
repeats = 1
while (
repeats <= last_line_index and lines[last_line_index - repeats] == last_line
):
repeats += 1
if repeats > max_lines_shown / 2 and len(lines) - repeats < max_lines_shown - 1:
append_repeat_message = True
lines = lines[0 : last_line_index + 2 - repeats]
sanitized_lines = []
for line_number, line in enumerate(lines):
if line_number >= max_lines_shown and not show_all_lines:
sanitized_lines.append("...\n")
break
if len(line) > max_line_length_shown:
line = line[0:max_line_length_shown] + " ..."
line = line.encode("unicode_escape").decode("ascii")
if leave_colorization:
line = line.replace(r"\x1b", "\x1b")
if leave_tabs:
line = line.replace(r"\t", "\t")
line = line.replace(r"\\", "\\")
color = line_color[line_number]
if color:
line = termcolor_colored(line, color)
sanitized_lines.append(line)
if append_repeat_message:
repeat_message = f"<last line repeated {repeats} times>"
if parameters["colorize_output"]:
repeat_message = termcolor_colored(repeat_message, "red")
sanitized_lines.append(repeat_message)
return "\n".join(sanitized_lines) + "\n"