Issue119

Title Name mangling seems to be wrong (and broken for __slots__)
Priority bug Status resolved
Superseder Nosy List JustAMan, kayhayen
Assigned To kayhayen Keywords wrong_execution

Created on 2014-01-13.18:49:26 by JustAMan, last changed by kayhayen.

Files
File name Uploaded Type Edit Remove
test.py JustAMan, 2014-01-13.18:49:26 application/octet-stream
Messages
msg550 (view) Author: kayhayen Date: 2014-01-30.09:59:42
The release 0.5.0.1 contains the fix and was done long ago, forgot to close this.
msg546 (view) Author: kayhayen Date: 2014-01-14.12:57:08
No need the hotfix is already 
running through release tests.
msg545 (view) Author: JustAMan Date: 2014-01-14.12:33:04
I think the algorythm is described in the docs (http://docs.python.org/2/
tutorial/classes.html#private-variables-and-class-local-references), the quote:

Any identifier of the form __spam (at least two leading underscores, at most 
one trailing underscore) is textually replaced with _classname__spam, where 
classname is the current class name *with leading underscore(s) stripped*.

(stress in the quote is mine)

I've fixed that issue locally and can submit a diff if you need one.
msg543 (view) Author: kayhayen Date: 2014-01-13.19:53:32
Some parts of the CPython test suite check the name mangling with values, e.g. 
"test_descr". But none of the test cases involve leading underscores in class 
names. Also, I couldn't find the precise rules.

Now I concluded that the rule should be that leading underscores in any amount 
should be removed from the class name. These will then have name clashes in 
CPython.

The reason this is revealed, is because __slots__ is handled by libpython, and 
applies the correct algorithm.

Thanks for the report. I am going to make a hotfix 0.5.0.1 that includes this 
and your test case if this passes the full test suite.
msg542 (view) Author: JustAMan Date: 2014-01-13.19:04:35
After some more looking it seems that overall mangling is incorrect (at least for 
Python 2.7), but it works for non-slot items even though mangled names in Nuitka-
compiled code differ from CPython-mangled names.
msg541 (view) Author: JustAMan Date: 2014-01-13.18:49:26
Consider the following (at least CPython 2.7-correct) piece of code:
class _Q(object):
    __slots__ = ['__hidden']
    def __init__(self):
        self.__hidden = 5

It compiles fine but when you try to instantiate _Q() AttributeError() is raised:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\4parts\parts-uid\test_pkg\b.py", line 4, in __init__
    self.__hidden = 5
AttributeError: '_Q' object has no attribute '__Q__hidden'

Indeed, _Q() object has _Q__hidden, not __Q_hidden (one leading underscore).

I've checked several combinations of name mangling and __slots__ - they all work except the case when a class which name 
starts with underscore has "private" __slots__ member (try attached test.py).

All checks were done via compiling .pyd module (on Windows) of a package containing empty __init__.py and other small file 
(for example attached one).
History
Date User Action Args
2014-01-30 09:59:42kayhayensetstatus: chatting -> resolved
messages: + msg550
2014-01-14 12:57:08kayhayensetmessages: + msg546
2014-01-14 12:33:04JustAMansetmessages: + msg545
2014-01-13 19:53:32kayhayensetassignedto: kayhayen
messages: + msg543
keyword: + wrong_execution
nosy: + kayhayen
2014-01-13 19:05:00JustAMansettitle: Name mangling for slots seems to be broken -> Name mangling seems to be wrong (and broken for __slots__)
2014-01-13 19:04:35JustAMansetstatus: unread -> chatting
messages: + msg542
2014-01-13 18:49:26JustAMancreate