Skip to content

Implement variable mangling for private fields #2296

Closed
@HyeockJinKim

Description

@HyeockJinKim

Feature

In cpython

>>> class A:
...  def __init__(self):
...   self.__a = 1
... 
>>> a = A()
>>> a.__a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute '__a'
>>> a._A__a
1

In Rustpython

>>>>> class A:
.....  def __init__(self):
.....   self.__a = 1
..... 
>>>>> a = A()
>>>>> a.__a
1

In python, the field of the class starting with __ is a private field,
and mangling is performed to prevent external access to the same variable.

Currently, this has not been done in Rustpython.

Python Documentation

https://github.com/python/cpython/blob/3185267400be853404f22a1e06bb9fe1210735c7/Python/compile.c#L254-L256
In cpython, it is probably mangled using the above function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-compatA discrepancy between RustPython and CPython

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions