-
Notifications
You must be signed in to change notification settings - Fork 372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add option to hy2py that converts symbols to valid python identifiers #970
Comments
I would argue against that being the default for This is a great idea; I've had it on my mind for a year or so. Do note that it's more complicated than it seems. You can't just mangle every name you see:
Basically, in particular, imports need extra attention. |
I propose that use The second case, import hy.core.shadow
+ = getattr(hy.core.shadow, "+")
# equivalent to `from hy.core.shadow import +`
reduce(+, [1L, 2L])
# we can replace '+' with any valid identifier |
Only problem is that that breaks any usage of (setv + 123)
(print (get (globals) "+")) ; BOOM!! Though I guess we already mangle |
That might be solved as follows: globals().update({'+': 123})
+ = globals().get('+')
# equivalent to `+ = 123` |
Hy would be more useful if it could compile to Python, and not just ast, see #923.
hy2py
can translate ast back into Python source. It's very useful as a debugging tool, but the result is not always executable. The main issue seems to be that Hy identifiers are a superset of valid Python identifiers. The ast doesn't care, but when importing Hy from Python we have a system to resolve this problem using Punycode and a few other rules (-
to_
etc.)I'm proposing we add an option to
hy2py
that converts all invalid symbols using the same system. This way the Python code will be still be executable. I'm not sure if there are other problems with the output, but it's a step in the right direction.This might also be more useful as the default for
hy --spy
.The text was updated successfully, but these errors were encountered: