Skip to content

Commit

Permalink
Get hy2py to cope with e.g. (setv def 1)
Browse files Browse the repository at this point in the history
The new loop through the AST doesn't seem to cause a perceptible slowdown. On my system, the test suite took 14.33 s with this change and 14.38 s without it.
  • Loading branch information
Kodiologist committed Apr 6, 2022
1 parent 9ab0ab6 commit af64cbe
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
24 changes: 23 additions & 1 deletion hy/_compat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ast
import platform
import sys

Expand All @@ -10,11 +11,32 @@

if not PY3_9:
# Shim `ast.unparse`.
import ast
import astor.code_gen
ast.unparse = astor.code_gen.to_source


if 'def' in ast.unparse(ast.parse('𝕕𝕖𝕗 = 1')):
# Overwrite `ast.unparse` to backport https://github.com/python/cpython/pull/31012
import copy
import keyword
true_unparse = ast.unparse
def rewriting_unparse(ast_obj):
ast_obj = copy.deepcopy(ast_obj)
for node in ast.walk(ast_obj):
if type(node) in (ast.Constant, ast.Str):
# Don't touch string literals.
continue
for field in node._fields:
v = getattr(node, field, None)
if (type(v) is str and
keyword.iskeyword(v) and
v not in ('True', 'False', 'None')):
setattr(node, field,
chr(ord(v[0]) - ord('a') + ord('𝐚')) + v[1:])
return true_unparse(ast_obj)
ast.unparse = rewriting_unparse


if not PY3_8:
# Shim `re.Pattern`.
import re
Expand Down
7 changes: 5 additions & 2 deletions tests/resources/pydemo.hy
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Call me Ishmael. Some years ago—never mind how long precisely—having little
(setv identifier-that-has☝️💯☝️-to-be-mangled "ponies")
(setv 𝔫𝔬𝔯𝔪𝔞𝔩𝔦𝔷𝔢-𝔱𝔥𝔦𝔰 "ok")
(setv def "variable")
(setv 𝕚𝕗 "if")
(setv mynumber (+ 1 2))
(setv myhex 0x123)
Expand Down Expand Up @@ -117,11 +119,12 @@ Call me Ishmael. Some years ago—never mind how long precisely—having little
(else
(setv ran-try-else True)))
(defn fun [a b [c 9] [d 10] #* args #** kwargs]
(defn fun [a b [c 9] [from 10] #* args #** kwargs]
"function docstring"
[a b c d args (sorted (.items kwargs))])
[a b c from args (sorted (.items kwargs))])
(setv funcall1 (fun 1 2 3 4 "a" "b" "c" :k1 "v1" :k2 "v2"))
(setv funcall2 (fun 7 8 #* [9 10 11] #** {"x1" "y1" "x2" "y2"}))
(setv funcall3 (fun "x" "y" :from "spain"))
(defn returner []
(return 1)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_hy2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def assert_stuff(m):

assert getattr(m, mangle("identifier-that-has☝️💯☝️-to-be-mangled")) == "ponies"
assert m.normalize_this == "ok"
assert getattr(m, "def") == "variable"
assert m.𝕕𝕖𝕗 == "variable"
assert getattr(m, "if") == "if"

assert m.mynumber == 3
assert m.myhex == 0x123
Expand Down Expand Up @@ -114,6 +117,7 @@ def assert_stuff(m):
assert m.fun.__doc__ == "function docstring"
assert m.funcall1 == [1, 2, 3, 4, ("a", "b", "c"), [("k1", "v1"), ("k2", "v2")]]
assert m.funcall2 == [7, 8, 9, 10, (11,), [("x1", "y1"), ("x2", "y2")]]
assert m.funcall3 == ["x", "y", 9, "spain", (), []]

assert m.myret == 1
assert m.myyield == ["a", "b", "c"]
Expand Down

0 comments on commit af64cbe

Please sign in to comment.