Skip to content

[mypyc] Make namegen not need to use suffixes to disambiguate #7676

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

Merged
merged 2 commits into from
Oct 10, 2019

Conversation

msullivan
Copy link
Collaborator

@msullivan msullivan commented Oct 9, 2019

Accomplish this by using a mapping that doesn't collide on valid
python identifiers. We map . to ___ and the (rare and unlikely,
but possible) ___ to _3___. (This collides ___ with .3,
which is fine.)

This makes naming not reliant on the order of processing things
which is important for separate compilation, since it will turn out
that most of our "internal" names actually do need to be exported
(but we would still like to shorten the module name part).

I also like the output better, even though it is a little more
verbose, since it makes it obvious what is a module separator and what
is an underscore.

Accomplish this by using a mapping that doesn't collide on valid
python identifiers. We map '.' to '___' and the (rare and unlikely,
but possible) '___' to '_3___'. (This collides '___' with '.3',
which is fine.)

This makes naming not reliant on the order of processing things
which is important for separate compilation, since it will turn out
that most of our "internal" names actually do need to be exported
(but we would still like to shorten the module name part).

I also like the output better, even though it is a little more
verbose, since it makes it obvious what is a module seperator and what
is an underscore.
Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it's better to have consistent generated names than the shortest possible one.

@@ -1416,11 +1416,12 @@ def __init__(self,
else:
self.bound_sig = FuncSignature(sig.args[1:], sig.ret_type)

@property
def shortname(self) -> str:
return self.class_name + '.' + self.name if self.class_name else self.name
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work with nested classes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing about mypyc works with nested classes

@@ -7,33 +7,34 @@

class TestNameGen(unittest.TestCase):
def test_candidate_suffixes(self) -> None:
assert candidate_suffixes('foo') == ['', 'foo_']
assert candidate_suffixes('foo.bar') == ['', 'bar_', 'foo_bar_']
assert candidate_suffixes('foo') == ['', 'foo.']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be useful to have an end-to-end test case that has things like triple underscores in names and possible name clashes, such as having names C.foo and C___foo in the same namespace?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add that to the name clash issue test

@msullivan msullivan merged commit 9614518 into master Oct 10, 2019
@gvanrossum gvanrossum deleted the tweak-mangling branch August 26, 2020 21:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants