Skip to content

Commit 7f1aee7

Browse files
committed
Move raise to inside HyASTCompiler
Then I can't forget to raise the error.
1 parent 1f842bc commit 7f1aee7

4 files changed

Lines changed: 26 additions & 20 deletions

File tree

NEWS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Bug Fixes
2323
* Variables set by `(except …)` are now properly scoped.
2424
* Fixed a crash when using `require` in `hy.eval`.
2525
* Fixed some compilation failures for asynchronous comprehension forms.
26+
* Fixed the error message for using `except*` on Pythons < 3.11.
2627
* More model types (`String`, `Bytes`, `Symbol`, `Integer`, `Float`,
2728
`Complex`) now work properly with `match`.
2829
* Fixed a bug that could cause `py_compile.compile` to read Hy as Python.

hy/compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ def compile(self, tree):
412412
raise HyCompileError(exc_msg)
413413

414414
def _syntax_error(self, expr, message):
415-
return HySyntaxError(message, expr, self.filename, self.source)
415+
raise HySyntaxError(message, expr, self.filename, self.source)
416416

417417
def _compile_collect(self, exprs, with_kwargs=False, dict_display=False):
418418
"""Collect the expression contexts from a list of compiled expression.

hy/core/result_macros.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def compile_inline_python(compiler, expr, root, code):
154154
"exec" if exec_mode else "eval",
155155
).body
156156
except (SyntaxError, ValueError) as e:
157-
raise compiler._syntax_error(
157+
compiler._syntax_error(
158158
expr, "Python parse error in '{}': {}".format(root, e)
159159
)
160160

@@ -168,17 +168,17 @@ def compile_pragma(compiler, expr, root, kwargs):
168168
if kw == Keyword("hy"):
169169
min_version = compiler.eval(value)
170170
if not isinstance(min_version, str):
171-
raise compiler._syntax_error(value, "The version given to the pragma `:hy` must be a string")
171+
compiler._syntax_error(value, "The version given to the pragma `:hy` must be a string")
172172
parts = min_version.split('.')
173173
if not all(p.isdigit() for p in parts):
174-
raise compiler._syntax_error(value, "The string given to the pragma `:hy` must be a dot-separated sequence of integers")
174+
compiler._syntax_error(value, "The string given to the pragma `:hy` must be a dot-separated sequence of integers")
175175
for have, need in zip_longest(
176176
map(int, last_version.split('.')),
177177
map(int, parts)):
178178
if need is None:
179179
break
180180
if have is None or have < need:
181-
raise compiler._syntax_error(kw, f"Hy version {min_version} or later required")
181+
compiler._syntax_error(kw, f"Hy version {min_version} or later required")
182182
if have > need:
183183
break
184184

@@ -192,7 +192,7 @@ def compile_pragma(compiler, expr, root, kwargs):
192192
reader.bracketed_templates = bool(compiler.eval(value))
193193

194194
else:
195-
raise compiler._syntax_error(kw, f"Unknown pragma `{kw}`. Perhaps it's implemented by a newer version of Hy.")
195+
compiler._syntax_error(kw, f"Unknown pragma `{kw}`. Perhaps it's implemented by a newer version of Hy.")
196196

197197
return Result()
198198

@@ -247,7 +247,7 @@ def render_quoted_form(compiler, form, level):
247247
f_contents, splice = render_quoted_form(compiler, x, level)
248248
if splice:
249249
if is_unpack("iterable", f_contents):
250-
raise compiler._syntax_error(f_contents, "`unpack-iterable` is not allowed here")
250+
compiler._syntax_error(f_contents, "`unpack-iterable` is not allowed here")
251251
f_contents = Expression(
252252
[
253253
Symbol("unpack-iterable"),
@@ -385,7 +385,7 @@ def enbool(expr):
385385
def get_c_op(compiler, sym):
386386
k = mangle(sym)
387387
if k not in c_ops:
388-
raise compiler._syntax_error(sym, "Illegal comparison operator: " + str(sym))
388+
compiler._syntax_error(sym, "Illegal comparison operator: " + str(sym))
389389
return c_ops[k]()
390390

391391

@@ -608,7 +608,7 @@ def compile_global_or_nonlocal(compiler, expr, root, syms):
608608
try:
609609
compiler.scope.define_nonlocal(ret, root)
610610
except SyntaxError as e:
611-
raise compiler._syntax_error(expr, e.msg)
611+
compiler._syntax_error(expr, e.msg)
612612

613613
return ret
614614

@@ -842,7 +842,7 @@ def compile_comprehension(compiler, expr, root, parts, final):
842842
if node_class is asty.DictComp:
843843
dict_unpack = bool(final)
844844
if not (dict_unpack or (parts and parts[-1].tag == "for")):
845-
raise compiler._syntax_error(
845+
compiler._syntax_error(
846846
parts[-1] if parts else parts,
847847
"`dfor` must end with key and value forms, or `#** FORM`",
848848
)
@@ -1281,7 +1281,7 @@ def compile_match_expression(compiler, expr, root, subject, clauses):
12811281
match_cases = []
12821282
for *pattern, guard, body in clauses:
12831283
if guard and body == Keyword("as"):
1284-
raise compiler._syntax_error(body, ":as clause cannot come after :if guard")
1284+
compiler._syntax_error(body, ":as clause cannot come after :if guard")
12851285

12861286
body = compiler._compile_branch([body])
12871287
body += asty.Assign(pattern[0], targets=[return_var], value=body.force_expr)
@@ -1421,7 +1421,7 @@ def compile_pattern(compiler, pattern):
14211421
kwd_patterns=[],
14221422
)
14231423
else:
1424-
raise compiler._syntax_error(value, "unsupported")
1424+
compiler._syntax_error(value, "unsupported")
14251425

14261426

14271427
# ------------------------------------------------
@@ -1494,7 +1494,7 @@ def compile_try_expression(compiler, expr, root, body, catchers, orelse, finalbo
14941494
compiler._syntax_error(except_sym, "`{}` requires Python 3.11 or later")
14951495
except_syms_seen.add(str(except_sym))
14961496
if len(except_syms_seen) > 1:
1497-
raise compiler._syntax_error(
1497+
compiler._syntax_error(
14981498
except_sym, "cannot have both `except` and `except*` on the same `try`"
14991499
)
15001500

@@ -1703,7 +1703,7 @@ def compile_lambda_list(compiler, params):
17031703
posonly_parms, args_parms, rest_parms, kwonly_parms, kwargs_parms = params
17041704

17051705
if not (posonly_parms or posonly_parms is None):
1706-
raise compiler._syntax_error(
1706+
compiler._syntax_error(
17071707
params, "at least one argument must precede /"
17081708
)
17091709

@@ -1719,7 +1719,7 @@ def compile_lambda_list(compiler, params):
17191719
None,
17201720
)
17211721
if invalid_non_default:
1722-
raise compiler._syntax_error(
1722+
compiler._syntax_error(
17231723
invalid_non_default[0], "non-default argument follows default argument"
17241724
)
17251725

@@ -1734,7 +1734,7 @@ def compile_lambda_list(compiler, params):
17341734

17351735
if rest_parms == Symbol("*"): # rest is a positional only marker
17361736
if not kwonly_parms:
1737-
raise compiler._syntax_error(
1737+
compiler._syntax_error(
17381738
rest_parms, "named arguments must follow bare *"
17391739
)
17401740
rest_ast = None
@@ -1814,7 +1814,7 @@ def compile_yield_expression(compiler, expr, root, args):
18141814
if len(args) == 2:
18151815
from_kw, x = args
18161816
if from_kw != Keyword("from"):
1817-
raise compiler._syntax_error(from_kw, "two-argument `yield` requires `:from`")
1817+
compiler._syntax_error(from_kw, "two-argument `yield` requires `:from`")
18181818
yield_from = True
18191819
args = [x]
18201820
ret = Result()
@@ -1942,7 +1942,7 @@ def compile_require(compiler, expr, root, entries):
19421942
for flag in (True, False)
19431943
)
19441944
if len(rest) > 1 or len(readers) > 1:
1945-
raise compiler._syntax_error(
1945+
compiler._syntax_error(
19461946
entry,
19471947
f"redefinition of ':{'macros' if len(rest) > 1 else 'readers'}' brackets.",
19481948
)

tests/native_tests/import.hy

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@
5050
(assert (= resources.in-init "chippy")))
5151

5252

53-
(defn [(pytest.mark.skipif (not hy.compat.PY3_15) :reason "Lazy imports require Python 3.15")]
54-
test-lazy []
53+
(defn test-lazy []
54+
55+
(when (not hy.compat.PY3_15)
56+
(with [e (pytest.raises hy.errors.HySyntaxError)]
57+
(hy.eval '(import :lazy math)))
58+
(assert (= e.value.msg "Lazy imports require Python 3.15 or later"))
59+
(return))
5560

5661
; The code is wrapped in `hy.eval` because otherwise, pytest may
5762
; resolve lazy imports too early.

0 commit comments

Comments
 (0)