-
Notifications
You must be signed in to change notification settings - Fork 371
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
Mangling makeover #1517
Mangling makeover #1517
Conversation
hy/_compat.py
Outdated
if x.rstrip() != x: | ||
return False | ||
import tokenize as T | ||
from StringIO import StringIO |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should prefer io.StringIO
/io.BytesIO
over the StringIO
package
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
2c543a8
to
027dc47
Compare
This means that a HySymbol remembers its original name. That is, `a-b` and `a_b` are different symbols although `(setv a-b 1)` and `(setv a_b 1)` set the same variable (namely, `a_b`). Most of the edits in this commit are to switch underscores to hyphens in places where mangling hasn't happened yet. I removed some lexer tests since the lexer no longer does any mangling.
`_`, as a variable, is now the shadow subtraction operator.
027dc47
to
4c5dea0
Compare
This really should have had a link to #1458 where much of the original discussion happened. One-approval merges should say so here in the conversation, for the record. Now that we've got the mangling makeover format settled, it's time to rethink the gensym format as discussed in #1458. I'd have mentioned this in time for the merge had I gotten warning. |
Fair enough.
It isn't hard to figure out if something was validly merged (and, if so, under what rules it was merged), is it?
But the creation of a PR is itself a 14-day warning. In theory, if you wanted a 1-day warning, you could set an alarm for yourself for 13 days hence (we could even write a bot that does this automatically; in Hy, no less), but why wait to do whatever it is you were going to do?
No worries, that can be done in a new issue or PR. This PR is already big, anyway. |
Come to think of it, this could be useful as a reminder to PR authors to check that the PR is in a mergeworthy state. |
Heads up, we'll have to be careful with this at release time. I found I had to flush all my compiled pyc files. |
"Be careful" in the sense of warning users to remove stale bytecode? NEWS could be a good place to put such a warning. |
Yes. |
Shouldn't the installer replace all the stale .pyc's automatically? It's fine to warn users about the issue #1324 since it will come up in their own development. But we shouldn't ask the users to do an extra installation step that we could have automated. |
Well, its not only about Hy, right? Lets say I package Hy for Arch Linux (which there exists a package in AUR), and some other useful Hy library/tool (honestly don't know of any, but for examples sake, suppose there is). Because the best practice for packaging Python like this is to also package the .pyc files, the eventual update to Hy to 0.15 would also mean all other Hy packages have to be rebuilt. Not the worst thing in the world to require, but its a kind of change that really needs to be announced. I'll put something up for adding it to the NEWS file tonight. |
(setv out (eval `(do | ||
(setv ~sym 10) | ||
[foo? is_foo]))) | ||
(assert (= out [10 10]))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kodiologist what's the rational for late mangling?
Just asking because its easier to make #1504 nice if I mangle symbols before comparing:
(defmacro foo [symbol]
(if (= symbol 'foobar?)
...
...))
Means this macro work on either 'foobar?
and is_foobar
. Not sure if there's something I'm missing, so asking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So that you have access to the literal symbol that was entered, in case you want it. If you want to compare things post-mangling, you can always write code like (= (mangle symbol) (mangle 'foobar?))
. By contrast, before this PR, when symbols were mangled at read-time, there was no way to tell whether the user entered foobar?
or is_foobar
, if you wanted to do that. After all, not every symbol ends up as a Python name, so not every symbol eventually needs to be mangled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The specific feature request was #360.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
The new Is there a reasoning for this? Note that If not, the blank string assertion error would be helpful to have dropped. |
I'm not sure what |
Reopened for the new merge policy.
?
#1115 (debatably)This pull request overhauls mangling such that HySymbols remember their original names (it is no longer the case that
(= 'a-b 'a_b)
) and all Python variables have names that are legal Python identifiers. The latter accomplishes most of what we need to ensure thathy2py
produces real Python code.The major changes are:
*foo*
→FOO
andfoo!
→foo_bang
are gone.foo-bar
→foo_bar
andfoo?
→is_foo
remain.hyx_
, and each illegal character is replaced withΔfooΔ
(or for Python 2,XfooX
), wherefoo
is the Unicode character name in lowercase with spaces replaced by underscores and hyphens replaced by "H"es. If the character doesn't have a name, we use "U" followed by its code point in lowercase hexadecimal.hyx_
oris_
is added after any leading underscores (because leading underscores can have magical effects in Python).The below things remain to be done, but I figured I'd get feeback before continuing.
_
is confused with the shadow subtraction operator-
In the unmangle function, undoNot worth it.hyx_
when necessary; otherwise, the distinct variables "hyx_X61X" and "a" will both unmangle to "a"mangle
andunmangle
and add them to core (fixes add mangle function to public api #1336)unmangle
instead ofhyify
in core