Skip to content

Commit adf5fce

Browse files
authored
Merge branch 'py-ast' of github.com:cs50/check50 into py-ast
2 parents 88d4cfb + 20e3f7c commit adf5fce

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

check50/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ def _setup_translation():
2323
_set_version()
2424
_setup_translation()
2525

26+
# Discourage use of check50 in the interactive mode, due to a naming conflict of
27+
# the `_` variable. check50 uses it for translations, but Python stores the
28+
# result of the last expression in a variable called `_`.
29+
import sys
30+
if hasattr(sys, 'ps1') or sys.flags.interactive:
31+
import warnings
32+
warnings.warn(_("check50 is not intended for use in interactive mode. "
33+
"Some behavior may not function as expected."))
34+
2635
from ._api import (
2736
import_checks,
2837
data, _data,

check50/_api.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,10 @@ class Mismatch(Failure):
455455
"""
456456

457457
def __init__(self, expected, actual, help=None):
458-
expected, actual = _truncate(expected, actual), _truncate(actual, expected)
458+
def _safe_truncate(x, y):
459+
return _truncate(x, y) if x not in (EOF, TIMEOUT) else x
460+
461+
expected, actual = _safe_truncate(expected, actual), _safe_truncate(actual, expected)
459462

460463
rationale = _("expected: {}\n actual: {}").format(
461464
_raw(expected),
@@ -464,12 +467,6 @@ def __init__(self, expected, actual, help=None):
464467

465468
super().__init__(rationale=rationale, help=help)
466469

467-
if expected == EOF:
468-
expected = "EOF"
469-
470-
if actual == EOF:
471-
actual = "EOF"
472-
473470
self.payload.update({"expected": expected, "actual": actual})
474471

475472

@@ -503,11 +500,13 @@ def wrapper(*args, **kwargs):
503500
return decorator
504501

505502
def _truncate(s, other, max_len=10):
503+
def normalize(obj):
504+
if isinstance(obj, list):
505+
return "\n".join(map(str, obj))
506+
else:
507+
return str(obj)
506508

507-
if isinstance(s, list):
508-
s = "\n".join(s)
509-
if isinstance(other, list):
510-
other = "\n".join(other)
509+
s, other = normalize(s), normalize(other)
511510

512511
# find the index of first difference
513512
limit = min(len(s), len(other))
@@ -532,13 +531,15 @@ def _truncate(s, other, max_len=10):
532531

533532

534533
def _raw(s):
535-
"""Get raw representation of s, truncating if too long."""
534+
"""Get raw representation of s."""
536535

537536
if isinstance(s, list):
538537
s = "\n".join(_raw(item) for item in s)
539538

540539
if s == EOF:
541540
return "EOF"
541+
elif s == TIMEOUT:
542+
return "TIMEOUT"
542543

543544
s = f'"{repr(str(s))[1:-1]}"'
544545
return s

0 commit comments

Comments
 (0)