Skip to content

Commit dc0ad6d

Browse files
committed
Add frame __repr__
1 parent e824f24 commit dc0ad6d

File tree

1 file changed

+29
-1
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/frame

1 file changed

+29
-1
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/frame/FrameBuiltins.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
*/
2626
package com.oracle.graal.python.builtins.objects.frame;
2727

28+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REPR__;
29+
2830
import java.util.List;
2931

3032
import com.oracle.graal.python.builtins.Builtin;
@@ -33,20 +35,23 @@
3335
import com.oracle.graal.python.builtins.PythonBuiltins;
3436
import com.oracle.graal.python.builtins.objects.PNone;
3537
import com.oracle.graal.python.builtins.objects.code.CodeNodes;
38+
import com.oracle.graal.python.builtins.objects.code.PCode;
3639
import com.oracle.graal.python.builtins.objects.frame.PFrame.Reference;
3740
import com.oracle.graal.python.builtins.objects.function.PArguments;
3841
import com.oracle.graal.python.builtins.objects.module.PythonModule;
3942
import com.oracle.graal.python.builtins.objects.object.ObjectBuiltins.DictNode;
4043
import com.oracle.graal.python.builtins.objects.object.ObjectBuiltinsFactory.DictNodeFactory;
4144
import com.oracle.graal.python.builtins.objects.object.PythonObject;
4245
import com.oracle.graal.python.nodes.PRootNode;
46+
import com.oracle.graal.python.nodes.argument.ReadArgumentNode;
4347
import com.oracle.graal.python.nodes.frame.MaterializeFrameNode;
4448
import com.oracle.graal.python.nodes.frame.ReadCallerFrameNode;
4549
import com.oracle.graal.python.nodes.frame.ReadLocalsNode;
4650
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
4751
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
4852
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
4953
import com.oracle.truffle.api.CompilerDirectives;
54+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5055
import com.oracle.truffle.api.RootCallTarget;
5156
import com.oracle.truffle.api.dsl.Cached;
5257
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -64,6 +69,23 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
6469
return FrameBuiltinsFactory.getFactories();
6570
}
6671

72+
@Builtin(name = __REPR__, minNumOfPositionalArgs = 1)
73+
@GenerateNodeFactory
74+
public abstract static class ReprNode extends PythonUnaryBuiltinNode {
75+
@Specialization
76+
String repr(VirtualFrame frame, PFrame self,
77+
@Cached GetCodeNode getCodeNode) {
78+
PCode code = getCodeNode.executeObject(frame, self);
79+
return getFormat(self, code);
80+
}
81+
82+
@TruffleBoundary
83+
private static String getFormat(PFrame self, PCode code) {
84+
return String.format("<frame at 0x%x, file %s, line %d, code %s>",
85+
self.hashCode(), code.getFilename(), self.getLine(), code.getName());
86+
}
87+
}
88+
6789
@Builtin(name = "f_globals", minNumOfPositionalArgs = 1, isGetter = true)
6890
@GenerateNodeFactory
6991
public abstract static class GetGlobalsNode extends PythonBuiltinNode {
@@ -131,8 +153,10 @@ Object get(@SuppressWarnings("unused") PFrame self) {
131153
@Builtin(name = "f_code", minNumOfPositionalArgs = 1, isGetter = true)
132154
@GenerateNodeFactory
133155
public abstract static class GetCodeNode extends PythonBuiltinNode {
156+
public abstract PCode executeObject(VirtualFrame frame, PFrame self);
157+
134158
@Specialization
135-
Object get(VirtualFrame frame, PFrame self,
159+
PCode get(VirtualFrame frame, PFrame self,
136160
@Cached("create()") CodeNodes.CreateCodeNode createCodeNode) {
137161
RootCallTarget ct = self.getTarget();
138162
if (ct != null) {
@@ -144,6 +168,10 @@ Object get(VirtualFrame frame, PFrame self,
144168
"<internal>",
145169
"<internal>", -1, new byte[0]);
146170
}
171+
172+
public static GetCodeNode create() {
173+
return FrameBuiltinsFactory.GetCodeNodeFactory.create(new ReadArgumentNode[0]);
174+
}
147175
}
148176

149177
@Builtin(name = "f_locals", minNumOfPositionalArgs = 1, isGetter = true)

0 commit comments

Comments
 (0)