Skip to content

Commit 1e3bfda

Browse files
timfelcosminbasca
authored andcommitted
fix function globals
1 parent 6f49b8f commit 1e3bfda

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/AbstractFunctionBuiltins.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
import com.oracle.truffle.api.frame.VirtualFrame;
7171
import com.oracle.truffle.api.interop.UnsupportedMessageException;
7272
import com.oracle.truffle.api.library.CachedLibrary;
73+
import com.oracle.truffle.api.profiles.ConditionProfile;
7374

7475
@CoreFunctions(extendClasses = {PythonBuiltinClassType.PFunction, PythonBuiltinClassType.PBuiltinFunction})
7576
public class AbstractFunctionBuiltins extends PythonBuiltins {
@@ -166,9 +167,27 @@ Object getClosure(Object self) {
166167
@GenerateNodeFactory
167168
public abstract static class GetGlobalsNode extends PythonBuiltinNode {
168169
@Specialization(guards = "!isBuiltinFunction(self)")
169-
Object getGlobals(PFunction self) {
170+
Object getGlobals(PFunction self,
171+
@CachedLibrary(limit = "1") PythonObjectLibrary lib,
172+
@Cached("createBinaryProfile()") ConditionProfile moduleGlobals,
173+
@Cached("createBinaryProfile()") ConditionProfile moduleHasNoDict) {
170174
// see the make_globals_function from lib-graalpython/functions.py
171-
return self.getGlobals();
175+
PythonObject globals = self.getGlobals();
176+
if (moduleGlobals.profile(globals instanceof PythonModule)) {
177+
PHashingCollection dict = lib.getDict(globals);
178+
if (moduleHasNoDict.profile(dict == null)) {
179+
dict = factory().createDictFixedStorage(globals);
180+
try {
181+
lib.setDict(globals, dict);
182+
} catch (UnsupportedMessageException e) {
183+
CompilerDirectives.transferToInterpreter();
184+
throw new IllegalStateException(e);
185+
}
186+
}
187+
return dict;
188+
} else {
189+
return globals;
190+
}
172191
}
173192

174193
@SuppressWarnings("unused")

0 commit comments

Comments
 (0)