Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evhub committed Nov 26, 2023
1 parent 1721293 commit 055b1e5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 33 deletions.
2 changes: 1 addition & 1 deletion DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2139,7 +2139,7 @@ Additionally, if the first argument is not callable, and is instead an `int`, `f
Though the first item may be any atom, following arguments are highly restricted, and must be:
- variables/attributes (e.g. `a.b`),
- literal constants (e.g. `True`),
- number literals (e.g. `1.5`), or
- number literals (e.g. `1.5`) (and no binary, hex, or octal), or
- one of the above followed by an exponent (e.g. `a**-5`).

For example, `(f .. g) x 1` will work, but `f x [1]`, `f x (1+2)`, and `f "abc"` will not.
Expand Down
13 changes: 9 additions & 4 deletions coconut/compiler/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,11 +807,15 @@ class Grammar(object):
bin_num = combine(caseless_literal("0b") + Optional(underscore.suppress()) + binint)
oct_num = combine(caseless_literal("0o") + Optional(underscore.suppress()) + octint)
hex_num = combine(caseless_literal("0x") + Optional(underscore.suppress()) + hexint)
non_decimal_num = any_of(
hex_num,
bin_num,
oct_num,
use_adaptive=False,
)
number = (
hex_num
| bin_num
| oct_num
# must come last to avoid matching "0" in "0b"
non_decimal_num
# must come last
| maybe_imag_num
)
# make sure that this gets addspaced not condensed so it doesn't produce a SyntaxError
Expand Down Expand Up @@ -1404,6 +1408,7 @@ class Grammar(object):
impl_call_item = condense(
disallow_keywords(reserved_vars)
+ ~any_string
+ ~non_decimal_num
+ atom_item
+ Optional(power_in_impl_call)
)
Expand Down
3 changes: 2 additions & 1 deletion coconut/compiler/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,8 +912,9 @@ def pickle_cache(original, cache_path, include_incremental=True, protocol=pickle
internal_assert(lambda: match_any == all_parse_elements[identifier](), "failed to look up match_any by identifier", (match_any, all_parse_elements[identifier]()))
if validation_dict is not None:
validation_dict[identifier] = match_any.__class__.__name__
match_any.expr_order.sort(key=lambda i: (-match_any.adaptive_usage[i], i))
all_adaptive_stats[identifier] = (match_any.adaptive_usage, match_any.expr_order)
logger.log("Caching adaptive item:", match_any, "<-", all_adaptive_stats[identifier])
logger.log("Caching adaptive item:", match_any, all_adaptive_stats[identifier])

logger.log("Saving {num_inc} incremental and {num_adapt} adaptive cache items to {cache_path!r}.".format(
num_inc=len(pickleable_cache_items),
Expand Down
2 changes: 1 addition & 1 deletion coconut/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
VERSION = "3.0.4"
VERSION_NAME = None
# False for release, int >= 1 for develop
DEVELOP = 3
DEVELOP = 4
ALPHA = True # for pre releases rather than post releases

assert DEVELOP is False or DEVELOP >= 1, "DEVELOP must be False or an int >= 1"
Expand Down
62 changes: 37 additions & 25 deletions coconut/tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@

jupyter_timeout = 120

base = os.path.dirname(os.path.relpath(__file__))
src = os.path.join(base, "src")
dest = os.path.join(base, "dest")
additional_dest = os.path.join(base, "dest", "additional_dest")
tests_dir = os.path.dirname(os.path.relpath(__file__))
src = os.path.join(tests_dir, "src")
dest = os.path.join(tests_dir, "dest")
additional_dest = os.path.join(tests_dir, "dest", "additional_dest")

src_cache_dir = os.path.join(src, coconut_cache_dir)
cocotest_dir = os.path.join(src, "cocotest")
Expand Down Expand Up @@ -428,29 +428,25 @@ def rm_path(path, allow_keep=False):
assert not base_dir.startswith(path), "refusing to delete Coconut itself: " + repr(path)
if allow_keep and get_bool_env_var("COCONUT_KEEP_TEST_FILES"):
return
if os.path.isdir(path):
try:
try:
if os.path.isdir(path):
shutil.rmtree(path)
except OSError:
logger.print_exc()
elif os.path.isfile(path):
os.remove(path)
elif os.path.isfile(path):
os.remove(path)
except OSError:
logger.print_exc()


@contextmanager
def using_paths(*paths):
"""Removes paths at the beginning and end."""
for path in paths:
if os.path.exists(path):
rm_path(path)
rm_path(path)
try:
yield
finally:
for path in paths:
try:
rm_path(path, allow_keep=True)
except OSError:
logger.print_exc()
rm_path(path, allow_keep=True)


@contextmanager
Expand All @@ -465,10 +461,25 @@ def using_dest(dest=dest, allow_existing=False):
try:
yield
finally:
try:
rm_path(dest, allow_keep=True)
except OSError:
logger.print_exc()
rm_path(dest, allow_keep=True)


def clean_caches():
"""Clean out all __coconut_cache__ dirs."""
for dirpath, dirnames, filenames in os.walk(tests_dir):
for name in dirnames:
if name == coconut_cache_dir:
rm_path(os.path.join(dirpath, name))


@contextmanager
def using_caches():
"""Cleans caches at start and end."""
clean_caches()
try:
yield
finally:
clean_caches()


@contextmanager
Expand Down Expand Up @@ -990,11 +1001,12 @@ def test_no_wrap(self):
if TEST_ALL:
if CPYTHON:
def test_any_of(self):
with using_env_vars({
adaptive_any_of_env_var: "True",
reverse_any_of_env_var: "True",
}):
run()
with using_caches():
with using_env_vars({
adaptive_any_of_env_var: "True",
reverse_any_of_env_var: "True",
}):
run()

def test_keep_lines(self):
run(["--keep-lines"])
Expand Down
5 changes: 4 additions & 1 deletion coconut/tests/src/extras.coco
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ cannot reassign type variable 'T' (use explicit '\T' syntax if intended) (line 1
" \\~~~~~~~~~~~~^",
))
assert_raises(-> parse("a := b"), CoconutParseError, err_has=" \\~^")
assert_raises(-> parse("1 + return"), CoconutParseError, err_has=" \\~~~~^")
assert_raises(-> parse("1 + return"), CoconutParseError, err_has=(
" \\~~~^",
" \\~~~~^",
))
assert_raises(-> parse("""
def f() =
assert 1
Expand Down

0 comments on commit 055b1e5

Please sign in to comment.