py-core/docs: CPython: Class Level Name Mangling does not happen
When running some code, I found that MicroPython does not do any name mangling inside of classes. When trying to find information about it, the only item I found was a TODO comment from the initial commit of the compiler, and not mention elsewhere. Being such an edge case issue, I'm not sure that implementing it is truly necessary, as most issues that arise from this not happening can be fixed in the Python code itself. However, it should be included in MicroPython Differences page in the docs making it much easier for anyone that encounters it to find the information they need.
Tested on MicroPython device
MicroPython v1.20.0 on 2023-04-26; Raspberry Pi Pico with RP2040
Example Code:
def __example_print(string):
print(string)
class ExampleClass():
def __init__(self, string) -> None:
self.string = string
def do_print(self):
__example_print(self.string)
# A simple string for testing
string = 'A testing string'
# Should print the string the first time
__example_print(string)
# Create a new object from the class with the same string
new_object = ExampleClass(string)
# Print a second time by accessing the attribute
__example_print(new_object.string)
# Try to run the do_print() method to print it again.
# This should cause an NameError as '__example_print' should be name mangled to '_ExampleClass__example_print', which does not exist
# However, it does not and prints that string we initialized with for the third time
new_object.do_print()
Expected Output:
A testing string
A testing string
Traceback (most recent call last):
File "/tmp/example_code.py", line 26, in <module>
new_object.do_print()
File "/tmp/example_code.py", line 9, in do_print
__example_print(self.string)
^^^^^^^^^^^^^^^
NameError: name '_ExampleClass__example_print' is not defined
Micropython output:
A testing string
A testing string
A testing string
tests/cpydiff/core_class_name_mangling: New tests for name mangling.
Adding new tests/documentation for missing name mangling for private class members.
Adds testing/documentation for #12150.