From b777972a0e9eb9f5077cc739e6f0bfcdf83311a7 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Fri, 14 Dec 2018 15:54:23 -0500 Subject: [PATCH] Fix mangling of characters below 0xFF --- NEWS.rst | 1 + hy/lex/__init__.py | 5 ++++- tests/native_tests/mangling.hy | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/NEWS.rst b/NEWS.rst index 036843a..518ca7a 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -34,6 +34,7 @@ Bug Fixes * 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 +* Fixed incorrect mangling of some characters at low code points 0.15.0 ============================== diff --git a/hy/lex/__init__.py b/hy/lex/__init__.py index 2103275..d1bfb5e 100644 --- a/hy/lex/__init__.py +++ b/hy/lex/__init__.py @@ -61,7 +61,10 @@ def mangle(s): according to Hy's mangling rules.""" def unicode_char_to_hex(uchr): # Covert a unicode char to hex string, without prefix - return uchr.encode('unicode-escape').decode('utf-8').lstrip('\\U').lstrip('\\u').lstrip('0') + if len(uchr) == 1 and ord(uchr) < 128: + return format(ord(uchr), 'x') + return (uchr.encode('unicode-escape').decode('utf-8') + .lstrip('\\U').lstrip('\\u').lstrip('\\x').lstrip('0')) assert s diff --git a/tests/native_tests/mangling.hy b/tests/native_tests/mangling.hy index bfb4d0a..d3f6eae 100644 --- a/tests/native_tests/mangling.hy +++ b/tests/native_tests/mangling.hy @@ -158,6 +158,27 @@ (assert (= (mangle a) b)) (assert (= (unmangle b) a)))) + +(defn test-nongraphic [] + ; https://github.com/hylang/hy/issues/1694 + + (assert (= (mangle " ") "hyx_XspaceX")) + (assert (= (mangle "\a") "hyx_XU7X")) + (assert (= (mangle "\t") "hyx_XU9X")) + (assert (= (mangle "\n") "hyx_XUaX")) + (assert (= (mangle "\r") "hyx_XUdX")) + (assert (= (mangle "\r") "hyx_XUdX")) + + (setv c (try unichr (except [NameError] chr))) + (assert (= (mangle (c 127)) "hyx_XU7fX")) + (assert (= (mangle (c 128)) "hyx_XU80X")) + (assert (= (mangle (c 0xa0)) "hyx_XnoHbreak_spaceX")) + (assert (= (mangle (c 0x378)) "hyx_XU378X")) + (assert (= (mangle (c 0x200a) "hyx_Xhair_spaceX"))) + (assert (= (mangle (c 0x2065)) "hyx_XU2065X")) + (assert (= (mangle (c 0x1000c)) "hyx_XU1000cX"))) + + (defn test-mangle-bad-indent [] ; Shouldn't crash with IndentationError (mangle " 0\n 0"))