Skip to content

Commit 00ce858

Browse files
author
Michal Medvecky
committed
[GR-62729] lambda vs <lambda> repr/str fix
PullRequest: graalpython/3864
2 parents f53b0ce + 5344afc commit 00ce858

File tree

5 files changed

+42
-44
lines changed

5 files changed

+42
-44
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags_bytecode_dsl/test_funcattrs.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ test.test_funcattrs.FunctionPropertiesTest.test___closure__ @ linux-x86_64
1414
test.test_funcattrs.FunctionPropertiesTest.test___code__ @ linux-x86_64
1515
test.test_funcattrs.FunctionPropertiesTest.test___globals__ @ linux-x86_64
1616
test.test_funcattrs.FunctionPropertiesTest.test___name__ @ linux-x86_64
17+
test.test_funcattrs.FunctionPropertiesTest.test___qualname__ @ linux-x86_64
1718
test.test_funcattrs.FunctionPropertiesTest.test_blank_func_defaults @ linux-x86_64
1819
test.test_funcattrs.FunctionPropertiesTest.test_cell_new @ linux-x86_64
1920
test.test_funcattrs.FunctionPropertiesTest.test_copying___code__ @ linux-x86_64

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags_bytecode_dsl/test_inspect.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,19 @@ test.test_inspect.test_inspect.TestGetattrStatic.test_no_dict_no_slots @ linux-x
9292
test.test_inspect.test_inspect.TestGetattrStatic.test_no_dict_no_slots_instance_member @ linux-x86_64
9393
test.test_inspect.test_inspect.TestGetattrStatic.test_property @ linux-x86_64
9494
test.test_inspect.test_inspect.TestGetattrStatic.test_slots @ linux-x86_64
95+
test.test_inspect.test_inspect.TestGetcallargsFunctions.test_keyword_only @ linux-x86_64
9596
test.test_inspect.test_inspect.TestGetcallargsFunctions.test_multiple_features @ linux-x86_64
9697
test.test_inspect.test_inspect.TestGetcallargsFunctions.test_plain @ linux-x86_64
9798
test.test_inspect.test_inspect.TestGetcallargsFunctions.test_varargs @ linux-x86_64
9899
test.test_inspect.test_inspect.TestGetcallargsFunctions.test_varkw @ linux-x86_64
99100
test.test_inspect.test_inspect.TestGetcallargsFunctions.test_varkw_only @ linux-x86_64
101+
test.test_inspect.test_inspect.TestGetcallargsMethods.test_keyword_only @ linux-x86_64
100102
test.test_inspect.test_inspect.TestGetcallargsMethods.test_multiple_features @ linux-x86_64
101103
test.test_inspect.test_inspect.TestGetcallargsMethods.test_plain @ linux-x86_64
102104
test.test_inspect.test_inspect.TestGetcallargsMethods.test_varargs @ linux-x86_64
103105
test.test_inspect.test_inspect.TestGetcallargsMethods.test_varkw @ linux-x86_64
104106
test.test_inspect.test_inspect.TestGetcallargsMethods.test_varkw_only @ linux-x86_64
107+
test.test_inspect.test_inspect.TestGetcallargsUnboundMethods.test_keyword_only @ linux-x86_64
105108
test.test_inspect.test_inspect.TestGetcallargsUnboundMethods.test_multiple_features @ linux-x86_64
106109
test.test_inspect.test_inspect.TestGetcallargsUnboundMethods.test_plain @ linux-x86_64
107110
test.test_inspect.test_inspect.TestGetcallargsUnboundMethods.test_varargs @ linux-x86_64

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags_bytecode_dsl/test_reprlib.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ test.test_reprlib.ReprTests.test_container @ linux-x86_64
99
test.test_reprlib.ReprTests.test_descriptors @ linux-x86_64
1010
test.test_reprlib.ReprTests.test_frozenset @ linux-x86_64
1111
test.test_reprlib.ReprTests.test_instance @ linux-x86_64
12+
test.test_reprlib.ReprTests.test_lambda @ linux-x86_64
1213
test.test_reprlib.ReprTests.test_nesting @ linux-x86_64
1314
test.test_reprlib.ReprTests.test_numbers @ linux-x86_64
1415
test.test_reprlib.ReprTests.test_range @ linux-x86_64

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/bytecode_dsl/BytecodeDSLCompiler.java

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
package com.oracle.graal.python.compiler.bytecode_dsl;
4242

4343
import java.util.EnumSet;
44-
import java.util.HashMap;
45-
import java.util.Map;
4644

4745
import com.oracle.graal.python.PythonLanguage;
4846
import com.oracle.graal.python.compiler.Compiler;
@@ -98,7 +96,6 @@ public static class BytecodeDSLCompilerContext {
9896
public final int futureLineNumber;
9997
public final ParserCallbacksImpl errorCallback;
10098
public final ScopeEnvironment scopeEnvironment;
101-
public final Map<Scope, String> qualifiedNames;
10299

103100
public BytecodeDSLCompilerContext(PythonLanguage language, PythonContext context, ModTy mod, Source source, int optimizationLevel,
104101
EnumSet<FutureFeature> futureFeatures, int futureLineNumber, ParserCallbacksImpl errorCallback, ScopeEnvironment scopeEnvironment) {
@@ -111,7 +108,6 @@ public BytecodeDSLCompilerContext(PythonLanguage language, PythonContext context
111108
this.futureLineNumber = futureLineNumber;
112109
this.errorCallback = errorCallback;
113110
this.scopeEnvironment = scopeEnvironment;
114-
this.qualifiedNames = new HashMap<>();
115111
}
116112

117113
public String maybeMangle(String privateName, Scope scope, String name) {
@@ -132,40 +128,5 @@ String getClassName(Scope s) {
132128
}
133129
return null;
134130
}
135-
136-
String getQualifiedName(Scope scope) {
137-
if (qualifiedNames.containsKey(scope)) {
138-
return qualifiedNames.get(scope);
139-
} else {
140-
String qualifiedName = computeQualifiedName(scope);
141-
qualifiedNames.put(scope, qualifiedName);
142-
return qualifiedName;
143-
}
144-
}
145-
146-
private String computeQualifiedName(Scope scope) {
147-
String qualifiedName = scope.getName();
148-
Scope parentScope = scopeEnvironment.lookupParent(scope);
149-
if (parentScope != null && parentScope != scopeEnvironment.getTopScope()) {
150-
if (parentScope.isTypeParam()) {
151-
parentScope = scopeEnvironment.lookupParent(parentScope);
152-
if (parentScope == null || scopeEnvironment.lookupParent(parentScope) == null) {
153-
return qualifiedName;
154-
}
155-
}
156-
if (!((scope.isFunction() || scope.isClass()) && parentScope.getUseOfName(mangle(scope, scope.getName())).contains(Scope.DefUse.GlobalExplicit))) {
157-
// Qualify the name, unless it's a function/class and the parent declared the
158-
// name as a global (in which case the function/class doesn't belong to the
159-
// parent).
160-
if (parentScope.isFunction()) {
161-
qualifiedName = getQualifiedName(parentScope) + ".<locals>." + scope.getName();
162-
} else {
163-
qualifiedName = getQualifiedName(parentScope) + "." + scope.getName();
164-
}
165-
}
166-
}
167-
168-
return qualifiedName;
169-
}
170131
}
171132
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/bytecode_dsl/RootNodeCompiler.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
*/
4141
package com.oracle.graal.python.compiler.bytecode_dsl;
4242

43+
import static com.oracle.graal.python.compiler.CompilationScope.AsyncFunction;
4344
import static com.oracle.graal.python.compiler.CompilationScope.Class;
45+
import static com.oracle.graal.python.compiler.CompilationScope.TypeParams;
4446
import static com.oracle.graal.python.compiler.SSTUtils.checkCaller;
4547
import static com.oracle.graal.python.compiler.SSTUtils.checkCompare;
4648
import static com.oracle.graal.python.compiler.SSTUtils.checkForbiddenArgs;
@@ -99,6 +101,7 @@
99101
import com.oracle.graal.python.pegparser.ParserCallbacks.WarningType;
100102
import com.oracle.graal.python.pegparser.scope.Scope;
101103
import com.oracle.graal.python.pegparser.scope.Scope.DefUse;
104+
import com.oracle.graal.python.pegparser.scope.ScopeEnvironment;
102105
import com.oracle.graal.python.pegparser.sst.AliasTy;
103106
import com.oracle.graal.python.pegparser.sst.ArgTy;
104107
import com.oracle.graal.python.pegparser.sst.ArgumentsTy;
@@ -187,6 +190,8 @@ public final class RootNodeCompiler implements BaseBytecodeDSLVisitor<BytecodeDS
187190
* {@link com.oracle.graal.python.pegparser.scope.ScopeEnvironment#maybeMangle(String, Scope, String)}.
188191
*/
189192
private final String privateName;
193+
private final RootNodeCompiler parent;
194+
private String qualName;
190195

191196
// Immutable after construction
192197
private final HashMap<String, Integer> varnames;
@@ -215,6 +220,7 @@ public RootNodeCompiler(BytecodeDSLCompilerContext ctx, RootNodeCompiler parent,
215220
this.startNode = rootNode;
216221
this.scope = ctx.scopeEnvironment.lookupScope(scopeKey);
217222
this.scopeType = getScopeType(scope, scopeKey);
223+
this.parent = parent;
218224
if (privateName != null) {
219225
this.privateName = privateName;
220226
} else if (scopeType == Class) {
@@ -319,7 +325,33 @@ private static TruffleString[] orderedTruffleStringArray(HashMap<String, Integer
319325
return orderedKeys(map, new TruffleString[0], PythonUtils::toTruffleStringUncached);
320326
}
321327

328+
private String getNewScopeQualName(String name, CompilationScope scopeType) {
329+
RootNodeCompiler parent = this.parent;
330+
if (parent != null && parent.parent != null) {
331+
if (parent.scopeType == TypeParams && parent.parent != null && parent.parent.parent != null) {
332+
parent = parent.parent;
333+
if (parent.parent != null && parent.parent.parent == null) {
334+
// if there are exactly two parents/ancestros, then return the name
335+
return name;
336+
}
337+
}
338+
if (!(EnumSet.of(CompilationScope.Function, AsyncFunction, Class).contains(scopeType) &&
339+
parent.scope.getUseOfName(ScopeEnvironment.mangle(parent.privateName, name)).contains(Scope.DefUse.GlobalExplicit))) {
340+
String base;
341+
if (EnumSet.of(CompilationScope.Function, AsyncFunction, CompilationScope.Lambda).contains(parent.scopeType)) {
342+
base = parent.qualName + ".<locals>";
343+
} else {
344+
base = parent.qualName;
345+
}
346+
return base + "." + name;
347+
}
348+
}
349+
return name;
350+
}
351+
322352
private BytecodeDSLCompilerResult compileRootNode(String name, ArgumentInfo argumentInfo, SourceRange sourceRange, BytecodeParser<Builder> parser) {
353+
qualName = getNewScopeQualName(name, scopeType);
354+
323355
BytecodeRootNodes<PBytecodeDSLRootNode> nodes = PBytecodeDSLRootNodeGen.create(ctx.language, BytecodeConfig.WITH_SOURCE, parser);
324356
List<PBytecodeDSLRootNode> nodeList = nodes.getNodes();
325357
assert nodeList.size() == 1;
@@ -358,7 +390,7 @@ private BytecodeDSLCompilerResult compileRootNode(String name, ArgumentInfo argu
358390
}
359391
}
360392

361-
BytecodeDSLCodeUnit codeUnit = new BytecodeDSLCodeUnit(toTruffleStringUncached(name), toTruffleStringUncached(ctx.getQualifiedName(scope)),
393+
BytecodeDSLCodeUnit codeUnit = new BytecodeDSLCodeUnit(toTruffleStringUncached(name), toTruffleStringUncached(qualName),
362394
argumentInfo.argCount, argumentInfo.kwOnlyArgCount, argumentInfo.positionalOnlyArgCount,
363395
flags, orderedTruffleStringArray(names),
364396
orderedTruffleStringArray(varnames),
@@ -893,7 +925,7 @@ public BytecodeDSLCompilerResult compileClassDefBody(StmtTy.ClassDef node) {
893925
endStoreLocal("__module__", b);
894926

895927
beginStoreLocal("__qualname__", b);
896-
emitPythonConstant(toTruffleStringUncached(ctx.getQualifiedName(scope)), b);
928+
emitPythonConstant(toTruffleStringUncached(this.qualName), b);
897929
endStoreLocal("__qualname__", b);
898930

899931
if (node.isGeneric()) {
@@ -1108,7 +1140,7 @@ public BytecodeDSLCompilerResult visit(ExprTy.SetComp node) {
11081140

11091141
@Override
11101142
public BytecodeDSLCompilerResult visit(ExprTy.GeneratorExp node) {
1111-
return buildComprehensionCodeUnit(node, node.generators, "<generator>",
1143+
return buildComprehensionCodeUnit(node, node.generators, "<genexpr>",
11121144
null,
11131145
(statementCompiler, collection) -> emitYield((statementCompiler_) -> node.element.accept(statementCompiler_), statementCompiler));
11141146
}
@@ -2101,7 +2133,7 @@ public Void visit(ExprTy.GeneratorExp node) {
21012133
boolean newStatement = beginSourceSection(node, b);
21022134

21032135
b.beginCallUnaryMethod();
2104-
emitMakeFunction(node, "<generator>", COMPREHENSION_ARGS);
2136+
emitMakeFunction(node, "<genexpr>", COMPREHENSION_ARGS);
21052137
node.generators[0].iter.accept(this);
21062138
b.endCallUnaryMethod();
21072139

@@ -3672,7 +3704,7 @@ private void emitMakeFunction(BytecodeDSLCodeUnit codeUnit, Object scopeKey, Str
36723704
ArgumentsTy argsForDefaults, List<ParamAnnotation> annotations) {
36733705
TruffleString functionName = toTruffleStringUncached(name);
36743706
Scope targetScope = ctx.scopeEnvironment.lookupScope(scopeKey);
3675-
TruffleString qualifiedName = toTruffleStringUncached(ctx.getQualifiedName(targetScope));
3707+
TruffleString qualifiedName = codeUnit.qualname;
36763708

36773709
// Register these in the Python constants list.
36783710
addConstant(qualifiedName);

0 commit comments

Comments
 (0)