Skip to content

Commit 42c344a

Browse files
committed
Fix moving lineno of the current frame
1 parent 4723085 commit 42c344a

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,17 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
7474
public abstract static class ReprNode extends PythonUnaryBuiltinNode {
7575
@Specialization
7676
String repr(VirtualFrame frame, PFrame self,
77-
@Cached GetCodeNode getCodeNode) {
77+
@Cached GetCodeNode getCodeNode,
78+
@Cached GetLinenoNode getLinenoNode) {
7879
PCode code = getCodeNode.executeObject(frame, self);
79-
return getFormat(self, code);
80+
int lineno = getLinenoNode.executeInt(frame, self);
81+
return getFormat(self, code, lineno);
8082
}
8183

8284
@TruffleBoundary
83-
private static String getFormat(PFrame self, PCode code) {
85+
private static String getFormat(PFrame self, PCode code, int lineno) {
8486
return String.format("<frame at 0x%x, file %s, line %d, code %s>",
85-
self.hashCode(), code.getFilename(), self.getLine(), code.getName());
87+
self.hashCode(), code.getFilename(), lineno, code.getName());
8688
}
8789
}
8890

@@ -124,10 +126,26 @@ Object get(VirtualFrame frame, @SuppressWarnings("unused") PFrame self) {
124126
@Builtin(name = "f_lineno", minNumOfPositionalArgs = 1, isGetter = true)
125127
@GenerateNodeFactory
126128
public abstract static class GetLinenoNode extends PythonBuiltinNode {
129+
public abstract int executeInt(VirtualFrame frame, PFrame self);
130+
127131
@Specialization
128-
int get(PFrame self) {
132+
int get(VirtualFrame frame, PFrame self,
133+
@Cached ConditionProfile isCurrentFrameProfile,
134+
@Cached MaterializeFrameNode materializeNode) {
135+
// Special case because this builtin can be called without going through an invoke node:
136+
// we need to sync the location of the frame if and only if 'self' represents the
137+
// current frame. If 'self' represents another frame on the stack, the location is
138+
// already set
139+
if (isCurrentFrameProfile.profile(PArguments.getCurrentFrameInfo(frame) == self.getRef())) {
140+
PFrame pyFrame = materializeNode.execute(frame, this, false, false);
141+
assert pyFrame == self;
142+
}
129143
return self.getLine();
130144
}
145+
146+
public static GetLinenoNode create() {
147+
return FrameBuiltinsFactory.GetLinenoNodeFactory.create(new ReadArgumentNode[0]);
148+
}
131149
}
132150

133151
@Builtin(name = "f_lasti", minNumOfPositionalArgs = 1, isGetter = true)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,10 @@ public int getLine() {
226226
} else {
227227
SourceSection sourceSection = location.getEncapsulatingSourceSection();
228228
if (sourceSection == null) {
229-
line = -1;
229+
return -1;
230230
} else {
231-
line = sourceSection.getStartLine();
231+
// The location can change, so we shouldn't cache the value
232+
return sourceSection.getStartLine();
232233
}
233234
}
234235
}

0 commit comments

Comments
 (0)