From 28504ba85da698884d7e4bb804c14ad492a2ff3b Mon Sep 17 00:00:00 2001 From: Jakub Wilk Date: Wed, 7 Nov 2018 20:15:43 +0100 Subject: [PATCH] Catch IndentationError in isidentifier() Fixes: >>> from hy._compat import isidentifier >>> isidentifier(u" 0\n 0") Traceback (most recent call last): File "", line 1, in File "hy/_compat.py", line 47, in isidentifier tokens = list(T.generate_tokens(StringIO(x).readline)) File "/usr/lib/python2.7/tokenize.py", line 374, in generate_tokens ("", lnum, pos, line)) File "", line 2 0 ^ IndentationError: unindent does not match any outer indentation level --- NEWS.rst | 1 + hy/_compat.py | 2 +- tests/native_tests/mangling.hy | 4 ++++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/NEWS.rst b/NEWS.rst index bb101a2..44ab3cb 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -30,6 +30,7 @@ Bug Fixes objects. * Fixed errors from `from __future__ import ...` statements and missing Hy module docstrings caused by automatic importing of Hy builtins. +* Fixed crash in `mangle` for some pathological inputs 0.15.0 ============================== diff --git a/hy/_compat.py b/hy/_compat.py index 4416ea5..379ce43 100644 --- a/hy/_compat.py +++ b/hy/_compat.py @@ -45,7 +45,7 @@ def isidentifier(x): from io import StringIO try: tokens = list(T.generate_tokens(StringIO(x).readline)) - except T.TokenError: + except (T.TokenError, IndentationError): return False return len(tokens) == 2 and tokens[0][0] == T.NAME diff --git a/tests/native_tests/mangling.hy b/tests/native_tests/mangling.hy index e009ab6..bfb4d0a 100644 --- a/tests/native_tests/mangling.hy +++ b/tests/native_tests/mangling.hy @@ -157,3 +157,7 @@ ["⚘-⚘" "hyx_XflowerX_XflowerX"]]] (assert (= (mangle a) b)) (assert (= (unmangle b) a)))) + +(defn test-mangle-bad-indent [] + ; Shouldn't crash with IndentationError + (mangle " 0\n 0"))