Skip to content

Commit

Permalink
Fix imports, exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
evhub committed Jul 26, 2023
1 parent aa21691 commit 847629a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
15 changes: 10 additions & 5 deletions coconut/compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def set_to_tuple(tokens):

def import_stmt(imp_from, imp, imp_as, raw=False):
"""Generate an import statement."""
if not raw:
if not raw and imp != "*":
module_path = (imp if imp_from is None else imp_from).split(".", 1)
existing_imp = import_existing.get(module_path[0])
if existing_imp is not None:
Expand Down Expand Up @@ -565,6 +565,7 @@ def reset(self, keep_state=False, filename=None):
IMPORTANT: When adding anything here, consider whether it should also be added to inner_environment.
"""
self.filename = filename
self.outer_ln = None
self.indchar = None
self.comments = defaultdict(set)
self.wrapped_type_ignore = None
Expand All @@ -590,8 +591,9 @@ def reset(self, keep_state=False, filename=None):
self.add_code_before_ignore_names = {}

@contextmanager
def inner_environment(self):
def inner_environment(self, ln=None):
"""Set up compiler to evaluate inner expressions."""
outer_ln, self.outer_ln = self.outer_ln, ln
line_numbers, self.line_numbers = self.line_numbers, False
keep_lines, self.keep_lines = self.keep_lines, False
comments, self.comments = self.comments, defaultdict(dictset)
Expand All @@ -604,6 +606,7 @@ def inner_environment(self):
try:
yield
finally:
self.outer_ln = outer_ln
self.line_numbers = line_numbers
self.keep_lines = keep_lines
self.comments = comments
Expand Down Expand Up @@ -1109,7 +1112,7 @@ def make_err(self, errtype, message, original, loc=0, ln=None, extra=None, refor

# get line number
if ln is None:
ln = self.adjust(lineno(loc, original))
ln = self.outer_ln or self.adjust(lineno(loc, original))

# get line indices for the error locs
original_lines = tuple(logical_lines(original, True))
Expand Down Expand Up @@ -1176,6 +1179,8 @@ def internal_assert(self, cond, original, loc, msg=None, item=None):

def inner_parse_eval(
self,
original,
loc,
inputstring,
parser=None,
preargs={"strip": True},
Expand All @@ -1184,7 +1189,7 @@ def inner_parse_eval(
"""Parse eval code in an inner environment."""
if parser is None:
parser = self.eval_parser
with self.inner_environment():
with self.inner_environment(ln=self.adjust(lineno(loc, original))):
self.streamline(parser, inputstring)
pre_procd = self.pre(inputstring, **preargs)
parsed = parse(parser, pre_procd)
Expand Down Expand Up @@ -4064,7 +4069,7 @@ def f_string_handle(self, original, loc, tokens):
compiled_exprs = []
for co_expr in exprs:
try:
py_expr = self.inner_parse_eval(co_expr)
py_expr = self.inner_parse_eval(original, loc, co_expr)
except ParseBaseException:
raise CoconutDeferredSyntaxError("parsing failed for format string expression: " + co_expr, loc)
if not does_parse(self.no_unquoted_newlines, py_expr):
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.2"
VERSION_NAME = None
# False for release, int >= 1 for develop
DEVELOP = 31
DEVELOP = 32
ALPHA = False # for pre releases rather than post releases

assert DEVELOP is False or DEVELOP >= 1, "DEVELOP must be False or an int >= 1"
Expand Down
11 changes: 10 additions & 1 deletion coconut/tests/src/cocotest/agnostic/primary.coco
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ def primary_test() -> bool:
bio = BytesIO(b"herp")
assert bio.read() == b"herp"
if TYPE_CHECKING or sys.version_info >= (3, 5):
from typing import Iterable, Any
from typing import (
Iterable,
Any,
List,
Dict,
cast,
Protocol,
TypeVar,
Generic,
) # NOQA

assert 1 | 2 == 3
assert "\n" == (
Expand Down
10 changes: 1 addition & 9 deletions coconut/tests/src/cocotest/agnostic/util.coco
Original file line number Diff line number Diff line change
Expand Up @@ -1059,15 +1059,7 @@ class unrepresentable:

# Typing
if TYPE_CHECKING or sys.version_info >= (3, 5):
from typing import (
List,
Dict,
Any,
cast,
Protocol,
TypeVar,
Generic,
)
from typing import *

T = TypeVar("T", covariant=True)
U = TypeVar("U", contravariant=True)
Expand Down
9 changes: 9 additions & 0 deletions coconut/tests/src/extras.coco
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ mismatched open '[' and close ')' (line 1)
])
^
""".strip())
assert_raises(-> parse("""
a = 1
b = f"{1+}"
c = 3
""".strip()), CoconutSyntaxError, err_has="""
parsing failed for format string expression: 1+ (line 2)
b = f"{1+}"
^
""".strip())

assert_raises(-> parse("(|*?>)"), CoconutSyntaxError, err_has="'|?*>'")
assert_raises(-> parse("(|**?>)"), CoconutSyntaxError, err_has="'|?**>'")
Expand Down

0 comments on commit 847629a

Please sign in to comment.