diff --git a/src/core/IronPython/Compiler/Ast/ClassDefinition.cs b/src/core/IronPython/Compiler/Ast/ClassDefinition.cs index e3f36de40..3d49e00ae 100644 --- a/src/core/IronPython/Compiler/Ast/ClassDefinition.cs +++ b/src/core/IronPython/Compiler/Ast/ClassDefinition.cs @@ -49,6 +49,16 @@ public ClassDefinition(string name, IReadOnlyList? bases, IReadOnlyL public override string Name => _name; + /// + /// The name of the variable the class is bound to. It differs from + /// if the name is private and gets mangled because it is defined in a class. + /// + internal string BindingName { + get => _bindingName ?? Name; + set => _bindingName = value; + } + private string? _bindingName; + public IReadOnlyList Bases => _bases; public IReadOnlyList Keywords => _keywords; diff --git a/src/core/IronPython/Compiler/Ast/FunctionDefinition.cs b/src/core/IronPython/Compiler/Ast/FunctionDefinition.cs index 80453b4fe..b0663d19e 100644 --- a/src/core/IronPython/Compiler/Ast/FunctionDefinition.cs +++ b/src/core/IronPython/Compiler/Ast/FunctionDefinition.cs @@ -114,6 +114,16 @@ internal override int KwOnlyArgCount { public override string Name => _name; + /// + /// The name of the variable the function is bound to. It differs from + /// if the name is private and gets mangled because it is defined in a class. + /// + internal string BindingName { + get => _bindingName ?? Name; + set => _bindingName = value; + } + private string _bindingName; + public IList Decorators { get; internal set; } public Expression ReturnAnnotation { get; internal set; } diff --git a/src/core/IronPython/Compiler/Ast/PythonNameBinder.cs b/src/core/IronPython/Compiler/Ast/PythonNameBinder.cs index 131c65855..77380c36b 100644 --- a/src/core/IronPython/Compiler/Ast/PythonNameBinder.cs +++ b/src/core/IronPython/Compiler/Ast/PythonNameBinder.cs @@ -213,7 +213,7 @@ public override void PostWalk(CallExpression node) { // ClassDefinition public override bool Walk(ClassDefinition node) { - node.PythonVariable = DefineName(node.Name); + node.PythonVariable = DefineName(node.BindingName); // Base references are in the outer context foreach (var b in node.Bases) b.Walk(this); @@ -720,7 +720,7 @@ public override bool Walk(FunctionDefinition node) { // Name is defined in the enclosing context if (!node.IsLambda) { - node.PythonVariable = DefineName(node.Name); + node.PythonVariable = DefineName(node.BindingName); } // process the default arg values in the outer context diff --git a/src/core/IronPython/Compiler/Parser.cs b/src/core/IronPython/Compiler/Parser.cs index a909185b6..8dd1eccba 100644 --- a/src/core/IronPython/Compiler/Parser.cs +++ b/src/core/IronPython/Compiler/Parser.cs @@ -400,9 +400,14 @@ private string FixName(string name) { } private string ReadName() { + string name = ReadUnmangledName(); + return name is null ? null : FixName(name); + } + + private string ReadUnmangledName() { if (PeekToken() is NameToken n) { NextToken(); - return FixName(n.Name); + return n.Name; } ReportSyntaxError(_lookahead); @@ -1047,7 +1052,7 @@ private ClassDefinition ParseClassDef() { Eat(TokenKind.KeywordClass); var start = GetStart(); - string name = ReadName(); + string name = ReadUnmangledName(); if (name == null) { // no name, assume there's no class. return new ClassDefinition(string.Empty, null, null, ErrorStmt()); @@ -1061,10 +1066,13 @@ private ClassDefinition ParseClassDef() { } var mid = GetEnd(); + // the class is bound to its name mangled as in the surrounding class + string bindingName = FixName(name); + // Save private prefix string savedPrefix = SetPrivatePrefix(name); - var ret = new ClassDefinition(name, bases, keywords); + var ret = new ClassDefinition(name, bases, keywords) { BindingName = bindingName }; PushClass(ret); // Parse the class body @@ -1151,7 +1159,8 @@ private FunctionDefinition ParseFuncDef(int? asyncStart = null) { bool isAsync = asyncStart.HasValue; Eat(TokenKind.KeywordDef); var start = asyncStart ?? GetStart(); // like in CPython, an async function starts at its 'async' keyword - string name = ReadName(); + string name = ReadUnmangledName(); + string bindingName = name is null ? null : FixName(name); Eat(TokenKind.LeftParenthesis); @@ -1163,7 +1172,7 @@ private FunctionDefinition ParseFuncDef(int? asyncStart = null) { FunctionDefinition ret; if (parameters == null) { // error in parameters - ret = new FunctionDefinition(name, Array.Empty(), isAsync); + ret = new FunctionDefinition(name, Array.Empty(), isAsync) { BindingName = bindingName }; ret.SetLoc(_globalParent, start, lEnd); return ret; } @@ -1176,7 +1185,7 @@ private FunctionDefinition ParseFuncDef(int? asyncStart = null) { var rStart = GetStart(); var rEnd = GetEnd(); - ret = new FunctionDefinition(name, parameters, isAsync); + ret = new FunctionDefinition(name, parameters, isAsync) { BindingName = bindingName }; PushFunction(ret); Statement body = ParseClassOrFuncBody(); diff --git a/tests/suite/test_class.py b/tests/suite/test_class.py index cf91b55ca..b971e3c31 100644 --- a/tests/suite/test_class.py +++ b/tests/suite/test_class.py @@ -3268,6 +3268,37 @@ class C: self.assertEqual(C.b, 10) self.assertEqual(x, 10) + def test_private_function_and_class_names(self): + # the variable a private function or class is bound to is mangled, but not its __name__ + def decorator(f): return f + + class _C: + def __method(self): return 'method' + @decorator + def __decorated(self): pass + async def __coroutine(self): pass + + class __Nested: + def set(self): + self.__x = 1 + return self + + def run(self): + def __function(): pass + class __Local: + def set(self): + self.__y = 2 + return self + coroutine = self.__coroutine() + coroutine.close() + return (__function.__name__, __Local.__name__, coroutine.__name__, self.__method(), + sorted(vars(__Local().set())), sorted(vars(_C.__Nested().set()))) + + for name in ('method', 'decorated', 'coroutine'): + self.assertEqual(getattr(_C, '_C__' + name).__name__, '__' + name) + self.assertEqual(_C._C__Nested.__name__, '__Nested') + self.assertEqual(_C().run(), ('__function', '__Local', '__coroutine', 'method', ['_Local__y'], ['_Nested__x'])) + def test_private_names_in_fstrings(self): # https://github.com/IronLanguages/ironpython3/issues/1421 class C: