Skip to content

Commit 7d16017

Browse files
committed
intrinsify globals() and locals()
1 parent b37ca66 commit 7d16017

File tree

3 files changed

+60
-23
lines changed

3 files changed

+60
-23
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
9797
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
9898
import com.oracle.graal.python.builtins.objects.dict.PDict;
99+
import com.oracle.graal.python.builtins.objects.frame.PFrame;
99100
import com.oracle.graal.python.builtins.objects.function.Arity;
100101
import com.oracle.graal.python.builtins.objects.function.PArguments;
101102
import com.oracle.graal.python.builtins.objects.function.PFunction;
@@ -138,6 +139,7 @@
138139
import com.oracle.graal.python.nodes.expression.CastToBooleanNode;
139140
import com.oracle.graal.python.nodes.expression.TernaryArithmetic;
140141
import com.oracle.graal.python.nodes.frame.ReadCallerFrameNode;
142+
import com.oracle.graal.python.nodes.function.ClassBodyRootNode;
141143
import com.oracle.graal.python.nodes.function.FunctionRootNode;
142144
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
143145
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
@@ -1587,4 +1589,46 @@ String inputPrompt(String prompt) {
15871589
return input(null);
15881590
}
15891591
}
1592+
1593+
@Builtin(name = "globals", fixedNumOfPositionalArgs = 0)
1594+
@GenerateNodeFactory
1595+
abstract static class GlobalsNode extends PythonBuiltinNode {
1596+
@Child ReadCallerFrameNode readCallerFrameNode = ReadCallerFrameNode.create();
1597+
private final ConditionProfile condProfile = ConditionProfile.createBinaryProfile();
1598+
1599+
@Specialization
1600+
public Object globals(VirtualFrame frame) {
1601+
Frame callerFrame = readCallerFrameNode.executeWith(frame);
1602+
PythonObject globals = PArguments.getGlobals(callerFrame);
1603+
if (condProfile.profile(globals instanceof PythonModule)) {
1604+
return factory().createDictFixedStorage(globals);
1605+
} else {
1606+
return globals;
1607+
}
1608+
}
1609+
}
1610+
1611+
@Builtin(name = "locals", fixedNumOfPositionalArgs = 0)
1612+
@GenerateNodeFactory
1613+
abstract static class LocalsNode extends PythonBuiltinNode {
1614+
@Child ReadCallerFrameNode readCallerFrameNode = ReadCallerFrameNode.create();
1615+
private final ConditionProfile condProfile = ConditionProfile.createBinaryProfile();
1616+
private final ConditionProfile inClassProfile = ConditionProfile.createBinaryProfile();
1617+
1618+
@Specialization
1619+
public Object globals(VirtualFrame frame) {
1620+
Frame callerFrame = readCallerFrameNode.executeWith(frame);
1621+
PFrame pFrame = PArguments.getPFrame(frame);
1622+
if (condProfile.profile(pFrame != null)) {
1623+
return pFrame.getLocals(factory());
1624+
} else {
1625+
Object specialArgument = PArguments.getSpecialArgument(callerFrame);
1626+
if (inClassProfile.profile(specialArgument instanceof ClassBodyRootNode)) {
1627+
return factory().createDictLocals(frame, true);
1628+
} else {
1629+
return factory().createDictLocals(frame, false);
1630+
}
1631+
}
1632+
}
1633+
}
15901634
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/InvokeNode.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.oracle.graal.python.nodes.PRootNode;
4343
import com.oracle.graal.python.nodes.argument.ApplyKeywordsNode;
4444
import com.oracle.graal.python.nodes.argument.ArityCheckNode;
45+
import com.oracle.graal.python.nodes.function.ClassBodyRootNode;
4546
import com.oracle.graal.python.runtime.PythonOptions;
4647
import com.oracle.truffle.api.CallTarget;
4748
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -60,6 +61,7 @@
6061
abstract class AbstractInvokeNode extends Node {
6162

6263
private final ConditionProfile needsFrameProfile = ConditionProfile.createBinaryProfile();
64+
private final ConditionProfile isClassBodyProfile = ConditionProfile.createBinaryProfile();
6365

6466
protected static boolean shouldInlineGenerators() {
6567
return PythonOptions.getOption(PythonLanguage.getContextRef().get(), PythonOptions.ForceInlineGeneratorCalls);
@@ -101,6 +103,13 @@ protected final MaterializedFrame getCallerFrame(VirtualFrame frame, CallTarget
101103
return null;
102104
}
103105

106+
protected final void optionallySetClassBodySpecial(Object[] arguments, CallTarget callTarget) {
107+
RootNode rootNode = ((RootCallTarget) callTarget).getRootNode();
108+
if (isClassBodyProfile.profile(rootNode instanceof ClassBodyRootNode)) {
109+
PArguments.setSpecialArgument(arguments, rootNode);
110+
}
111+
}
112+
104113
@TruffleBoundary
105114
protected static Arity getArity(PythonCallable callee) {
106115
if (callee instanceof PythonBuiltinClass) {
@@ -128,6 +137,7 @@ protected Object execute(VirtualFrame frame, PythonCallable callee, Object[] arg
128137
RootCallTarget callTarget = getCallTarget(callee);
129138
MaterializedFrame callerFrame = getCallerFrame(frame, callTarget);
130139
PArguments.setCallerFrame(arguments, callerFrame);
140+
optionallySetClassBodySpecial(arguments, callTarget);
131141
Arity arity = getArity(callee);
132142
if (isBuiltin(callee)) {
133143
PArguments.setKeywordArguments(arguments, keywords);
@@ -175,6 +185,7 @@ protected Object doNoKeywords(VirtualFrame frame, PythonObject globals, PCell[]
175185
PArguments.setGlobals(arguments, globals);
176186
PArguments.setClosure(arguments, closure);
177187
PArguments.setCallerFrame(arguments, getCallerFrame(frame, callNode.getCallTarget()));
188+
optionallySetClassBodySpecial(arguments, callNode.getCallTarget());
178189
arityCheck.execute(arity, arguments, keywords);
179190
return callNode.call(arguments);
180191
}
@@ -186,6 +197,7 @@ protected Object doWithKeywords(VirtualFrame frame, PythonObject globals, PCell[
186197
PArguments.setGlobals(combined, globals);
187198
PArguments.setClosure(combined, closure);
188199
PArguments.setCallerFrame(arguments, getCallerFrame(frame, callNode.getCallTarget()));
200+
optionallySetClassBodySpecial(arguments, callNode.getCallTarget());
189201
arityCheck.execute(arity, combined, PArguments.getKeywordArguments(combined));
190202
return callNode.call(combined);
191203
}
@@ -195,6 +207,7 @@ protected Object doBuiltinWithKeywords(VirtualFrame frame, @SuppressWarnings("un
195207
PKeyword[] keywords) {
196208
PArguments.setKeywordArguments(arguments, keywords);
197209
PArguments.setCallerFrame(arguments, getCallerFrame(frame, callNode.getCallTarget()));
210+
optionallySetClassBodySpecial(arguments, callNode.getCallTarget());
198211
arityCheck.execute(arity, arguments, keywords);
199212
return callNode.call(arguments);
200213
}
@@ -236,6 +249,7 @@ protected Object doNoKeywords(VirtualFrame frame, Object[] arguments, PKeyword[]
236249
PArguments.setGlobals(arguments, globals);
237250
PArguments.setClosure(arguments, closure);
238251
PArguments.setCallerFrame(arguments, getCallerFrame(frame, callNode.getCallTarget()));
252+
optionallySetClassBodySpecial(arguments, callNode.getCallTarget());
239253
arityCheck.execute(arity, arguments, keywords);
240254
return callNode.call(arguments);
241255
}
@@ -247,6 +261,7 @@ protected Object doWithKeywords(VirtualFrame frame, Object[] arguments, PKeyword
247261
PArguments.setGlobals(combined, globals);
248262
PArguments.setClosure(combined, closure);
249263
PArguments.setCallerFrame(arguments, getCallerFrame(frame, callNode.getCallTarget()));
264+
optionallySetClassBodySpecial(arguments, callNode.getCallTarget());
250265
arityCheck.execute(arity, combined, PArguments.getKeywordArguments(combined));
251266
return callNode.call(combined);
252267
}
@@ -255,6 +270,7 @@ protected Object doWithKeywords(VirtualFrame frame, Object[] arguments, PKeyword
255270
protected Object doBuiltinWithKeywords(VirtualFrame frame, Object[] arguments, PKeyword[] keywords) {
256271
PArguments.setKeywordArguments(arguments, keywords);
257272
PArguments.setCallerFrame(arguments, getCallerFrame(frame, callNode.getCallTarget()));
273+
optionallySetClassBodySpecial(arguments, callNode.getCallTarget());
258274
arityCheck.execute(arity, arguments, keywords);
259275
return callNode.call(arguments);
260276
}

graalpython/lib-graalpython/functions.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,6 @@ def print_f(*objects, sep=" ", end="\n", file=None, flush=False):
6868
del make_print
6969

7070

71-
# We close over the globals to avoid leaking sys to the builtins scope
72-
def make_globals_function():
73-
import sys
74-
75-
def globals_f():
76-
return sys._getframe(1).f_globals
77-
globals_f.__name__ = "globals"
78-
return globals_f
79-
globals = __builtin__(make_globals_function())
80-
del make_globals_function
81-
82-
83-
def make_locals_function():
84-
import sys
85-
86-
def locals_f():
87-
return sys._getframe(1).f_locals
88-
locals_f.__name__ = "locals"
89-
return locals_f
90-
locals = __builtin__(make_locals_function())
91-
del make_locals_function
92-
93-
9471
@__builtin__
9572
def any(iterable):
9673
for i in iterable:

0 commit comments

Comments
 (0)