Skip to content

Commit 3a6df74

Browse files
msimacektomasstupka
authored andcommitted
Intrinsify vars builtin to fix frame handling issues
1 parent 04d37b8 commit 3a6df74

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import static com.oracle.graal.python.nodes.BuiltinNames.__BUILTINS__;
7070
import static com.oracle.graal.python.nodes.BuiltinNames.__DEBUG__;
7171
import static com.oracle.graal.python.nodes.BuiltinNames.__GRAALPYTHON__;
72+
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DICT__;
7273
import static com.oracle.graal.python.nodes.SpecialMethodNames.__FORMAT__;
7374
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEXT__;
7475
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ROUND__;
@@ -2218,6 +2219,31 @@ static Object getLocalsDict(VirtualFrame frame, Node n, ReadLocalsNode readLocal
22182219
return readLocalsNode.execute(frame, materializeNode.execute(frame, n, false, false, generatorFrame));
22192220
}
22202221
}
2222+
2223+
public static LocalsNode create() {
2224+
return BuiltinFunctionsFactory.LocalsNodeFactory.create(null);
2225+
}
2226+
}
2227+
2228+
@Builtin(name = "vars", maxNumOfPositionalArgs = 1)
2229+
@GenerateNodeFactory
2230+
abstract static class VarsNode extends PythonUnaryBuiltinNode {
2231+
2232+
@Specialization(guards = "isNoValue(none)")
2233+
Object vars(VirtualFrame frame, @SuppressWarnings("unused") PNone none,
2234+
@Cached LocalsNode localsNode) {
2235+
return localsNode.execute(frame);
2236+
}
2237+
2238+
@Specialization(guards = "!isNoValue(obj)")
2239+
Object vars(VirtualFrame frame, Object obj,
2240+
@Cached PyObjectLookupAttr lookupAttr) {
2241+
Object dict = lookupAttr.execute(frame, obj, __DICT__);
2242+
if (dict == NO_VALUE) {
2243+
throw raise(TypeError, ErrorMessages.VARS_ARGUMENT_MUST_HAVE_DICT);
2244+
}
2245+
return dict;
2246+
}
22212247
}
22222248

22232249
@Builtin(name = "open", minNumOfPositionalArgs = 1, parameterNames = {"file", "mode", "buffering", "encoding", "errors", "newline", "closefd", "opener"})

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ public abstract class ErrorMessages {
709709
public static final String UNSUPPORTED_USE_OF_SYS_EXECUTABLE = "internal error: unsupported use of sys.executable";
710710
public static final String UTIME_CANNOT_USE_DIR_FD_AND_FOLLOW_SYMLINKS = "utime: cannot use dir_fd and follow_symlinks together on this platform";
711711
public static final String VALUE_TOO_LARGE_TO_FIT_INTO_INDEX = "value too large to fit into index-sized integer";
712+
public static final String VARS_ARGUMENT_MUST_HAVE_DICT = "vars() argument must have __dict__ attribute";
712713
public static final String WAS_NOT_POSSIBLE_TO_MARSHAL = "Was not possible to marshal";
713714
public static final String WAS_NOT_POSSIBLE_TO_MARSHAL_P = "Was not possible to marshal %p";
714715
public static final String WEAK_OBJ_GONE_AWAY = "weak object has gone away";

graalpython/lib-graalpython/builtins.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,6 @@ def _f(): pass
5252
descriptor = type(FunctionType.__code__)
5353

5454

55-
from sys import _getframe as __getframe__
56-
57-
58-
@__graalpython__.builtin
59-
def vars(module, *obj):
60-
"""Return a dictionary of all the attributes currently bound in obj. If
61-
called with no argument, return the variables bound in local scope."""
62-
if len(obj) == 0:
63-
# TODO inlining _caller_locals().items() in the dict comprehension does not work for now, investigate!
64-
return __getframe__(0).f_locals
65-
elif len(obj) != 1:
66-
raise TypeError("vars() takes at most 1 argument.")
67-
try:
68-
return obj[0].__dict__
69-
except AttributeError:
70-
raise TypeError("vars() argument must have __dict__ attribute")
71-
72-
7355
@__graalpython__.builtin
7456
def input(module, prompt=None):
7557
import sys

0 commit comments

Comments
 (0)