Skip to content

Commit 3e0a2bb

Browse files
author
Adam Hrbac
committed
fix f_lineno behavior
1 parent c6a4e79 commit 3e0a2bb

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.oracle.graal.python.builtins.objects.code.PCode;
3939
import com.oracle.graal.python.builtins.objects.frame.PFrame.Reference;
4040
import com.oracle.graal.python.builtins.objects.function.PArguments;
41+
import com.oracle.graal.python.builtins.objects.getsetdescriptor.DescriptorDeleteMarker;
4142
import com.oracle.graal.python.builtins.objects.module.PythonModule;
4243
import com.oracle.graal.python.builtins.objects.object.ObjectBuiltins.DictNode;
4344
import com.oracle.graal.python.builtins.objects.object.ObjectBuiltinsFactory;
@@ -53,6 +54,7 @@
5354
import com.oracle.graal.python.nodes.frame.ReadCallerFrameNode.FrameSelector;
5455
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
5556
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
57+
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
5658
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
5759
import com.oracle.graal.python.nodes.util.CannotCastException;
5860
import com.oracle.graal.python.nodes.util.CastToJavaBooleanNode;
@@ -153,14 +155,7 @@ int get(VirtualFrame frame, PFrame self,
153155
@Bind("this") Node inliningTarget,
154156
@Cached InlinedConditionProfile isCurrentFrameProfile,
155157
@Cached MaterializeFrameNode materializeNode) {
156-
// Special case because this builtin can be called without going through an invoke node:
157-
// we need to sync the location of the frame if and only if 'self' represents the
158-
// current frame. If 'self' represents another frame on the stack, the location is
159-
// already set
160-
if (isCurrentFrameProfile.profile(inliningTarget, frame != null && PArguments.getCurrentFrameInfo(frame) == self.getRef())) {
161-
PFrame pyFrame = materializeNode.execute(frame, this, false, false);
162-
assert pyFrame == self;
163-
}
158+
LinenoNode.syncLocationIfNeeded(frame, self, this, inliningTarget, isCurrentFrameProfile, materializeNode);
164159
return self.getLine();
165160
}
166161

@@ -170,40 +165,45 @@ public static GetLinenoNode create() {
170165
}
171166
}
172167

173-
@Builtin(name = "f_lineno", minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, isGetter = true, isSetter = true)
168+
@Builtin(name = "f_lineno", minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, isGetter = true, isSetter = true, allowsDelete = true)
174169
@GenerateNodeFactory
175-
public abstract static class LinenoNode extends PythonBuiltinNode {
176-
public abstract Object execute(VirtualFrame frame, PFrame self, Object newLineno);
170+
public abstract static class LinenoNode extends PythonBinaryBuiltinNode {
171+
172+
@Specialization
173+
Object delete(VirtualFrame frame, PFrame self, DescriptorDeleteMarker ignored,
174+
@Bind("this") Node inliningTarget,
175+
@Cached @Cached.Exclusive PRaiseNode.Lazy raise) {
176+
raise.get(inliningTarget).raise(PythonBuiltinClassType.AttributeError, ErrorMessages.CANNOT_DELETE);
177+
return PNone.NONE;
178+
}
177179

178180
@Specialization(guards = "isNoValue(newLineno)")
179181
int get(VirtualFrame frame, PFrame self, Object newLineno,
180-
@Bind("this") Node inliningTarget,
181-
@Cached.Shared("isCurrentFrame") @Cached InlinedConditionProfile isCurrentFrameProfile,
182-
@Cached.Shared("materialize") @Cached MaterializeFrameNode materializeNode) {
183-
syncLocationIfNeeded(frame, self, inliningTarget, isCurrentFrameProfile, materializeNode);
184-
return self.getLine();
182+
@Cached GetLinenoNode getLinenoNode) {
183+
return getLinenoNode.executeInt(frame, self);
185184
}
186185

187-
private void syncLocationIfNeeded(VirtualFrame frame, PFrame self, Node inliningTarget, InlinedConditionProfile isCurrentFrameProfile, MaterializeFrameNode materializeNode) {
186+
static void syncLocationIfNeeded(VirtualFrame frame, PFrame self, Node location, Node inliningTarget, InlinedConditionProfile isCurrentFrameProfile, MaterializeFrameNode materializeNode) {
188187
// Special case because this builtin can be called without going through an invoke node:
189188
// we need to sync the location of the frame if and only if 'self' represents the
190189
// current frame. If 'self' represents another frame on the stack, the location is
191190
// already set
192191
if (isCurrentFrameProfile.profile(inliningTarget, frame != null && PArguments.getCurrentFrameInfo(frame) == self.getRef())) {
193-
PFrame pyFrame = materializeNode.execute(frame, this, false, false);
192+
PFrame pyFrame = materializeNode.execute(frame, location, false, false);
194193
assert pyFrame == self;
195194
}
196195
}
197196

198-
@Specialization(guards = "!isNoValue(newLineno)")
197+
@SuppressWarnings("truffle-static-method") // this is used for location here
198+
@Specialization(guards = {"!isNoValue(newLineno)", "!isDeleteMarker(newLineno)"})
199199
PNone set(VirtualFrame frame, PFrame self, Object newLineno,
200200
@Bind("this") Node inliningTarget,
201-
@Cached.Shared("isCurrentFrame") @Cached InlinedConditionProfile isCurrentFrameProfile,
202-
@Cached.Shared("materialize") @Cached MaterializeFrameNode materializeNode,
203-
@Cached PRaiseNode.Lazy raise,
201+
@Cached InlinedConditionProfile isCurrentFrameProfile,
202+
@Cached MaterializeFrameNode materializeNode,
203+
@Cached @Cached.Exclusive PRaiseNode.Lazy raise,
204204
@Cached PyLongCheckExactNode isLong,
205205
@Cached PyLongAsLongAndOverflowNode toLong) {
206-
syncLocationIfNeeded(frame, self, inliningTarget, isCurrentFrameProfile, materializeNode);
206+
syncLocationIfNeeded(frame, self, this, inliningTarget, isCurrentFrameProfile, materializeNode);
207207
if (self.isTraceArgument()) {
208208
if (isLong.execute(inliningTarget, newLineno)) {
209209
try {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/PBytecodeRootNode.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,8 +3120,14 @@ private void invokeTraceFunction(VirtualFrame virtualFrame, Object arg, PythonCo
31203120
Object result = CallTernaryMethodNode.getUncached().execute(null, traceFn, pyFrame, event.pythonName, nonNullArg);
31213121
syncLocalsBackToFrame(virtualFrame, pyFrame);
31223122
// https://github.com/python/cpython/issues/104232
3123-
Object realResult = result == PNone.NONE ? traceFn : result;
3124-
pyFrame.setLocalTraceFun(realResult);
3123+
if (useLocalFn) {
3124+
Object realResult = result == PNone.NONE ? traceFn : result;
3125+
pyFrame.setLocalTraceFun(realResult);
3126+
} else if (result != PNone.NONE) {
3127+
pyFrame.setLocalTraceFun(result);
3128+
} else {
3129+
pyFrame.setLocalTraceFun(null);
3130+
}
31253131
} catch (Throwable e) {
31263132
threadState.setTraceFun(null, PythonLanguage.get(this));
31273133
throw e;

0 commit comments

Comments
 (0)