Skip to content

Improve NameError message when non-mangled name exists in the global scope #119901

@PC-Killer

Description

@PC-Killer

Bug report

Bug description:

Here is the code:

def __a():
    print("A")


class A:
    def b(self):
        __a()


a = A()
a.b()

It was expected to show a short message "A". However, I got a NameError:

python3.12 bug\ represent.py
Traceback (most recent call last):
  File "/home/PC-Killer/python_work/Demos/bug represent.py", line 11, in <module>
    a.b()
  File "/home/PC-Killer/python_work/Demos/bug represent.py", line 7, in b
    __a()
    ^^^
NameError: name '_A__a' is not definedpython3.13 bug\ represent.py
Traceback (most recent call last):
  File "/home/PC-Killer/python_work/Demos/bug represent.py", line 11, in <module>
    a.b()
    ~~~^^
  File "/home/PC-Killer/python_work/Demos/bug represent.py", line 7, in b
    __a()
    ^^^
NameError: name '_A__a' is not defined

It seems that the name had been translated in an unexpected way.

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions