Skip to content

Commit 528e9c6

Browse files
committed
Sync locals back after a tracing call
1 parent a50effd commit 528e9c6

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
import com.oracle.graal.python.nodes.expression.UnaryOpNode;
175175
import com.oracle.graal.python.nodes.frame.DeleteGlobalNode;
176176
import com.oracle.graal.python.nodes.frame.DeleteGlobalNodeGen;
177+
import com.oracle.graal.python.nodes.frame.GetFrameLocalsNode;
177178
import com.oracle.graal.python.nodes.frame.MaterializeFrameNode;
178179
import com.oracle.graal.python.nodes.frame.ReadFromLocalsNode;
179180
import com.oracle.graal.python.nodes.frame.ReadFromLocalsNodeGen;
@@ -2962,7 +2963,10 @@ private void invokeTraceFunction(VirtualFrame virtualFrame, Object arg, PythonCo
29622963
if (line != -1) {
29632964
pyFrame.setLineLock(line);
29642965
}
2966+
// Force locals dict sync, so that we can sync them back later
2967+
GetFrameLocalsNode.getUncached().execute(pyFrame);
29652968
Object result = CallTernaryMethodNode.getUncached().execute(null, traceFn, pyFrame, event.pythonName, nonNullArg);
2969+
syncLocalsBack(virtualFrame, pyFrame);
29662970
Object realResult = result == PNone.NONE ? null : result;
29672971
pyFrame.setLocalTraceFun(realResult);
29682972
} catch (Throwable e) {
@@ -2976,6 +2980,36 @@ private void invokeTraceFunction(VirtualFrame virtualFrame, Object arg, PythonCo
29762980
}
29772981
}
29782982

2983+
// PyFrame_LocalsToFast
2984+
private void syncLocalsBack(VirtualFrame virtualFrame, PFrame pyFrame) {
2985+
Frame localFrame = virtualFrame;
2986+
if (co.isGeneratorOrCoroutine()) {
2987+
localFrame = PArguments.getGeneratorFrame(virtualFrame);
2988+
}
2989+
Object localsDict = pyFrame.getLocalsDict();
2990+
copyLocalsArray(localFrame, localsDict, varnames, 0, false);
2991+
copyLocalsArray(localFrame, localsDict, cellvars, celloffset, true);
2992+
copyLocalsArray(localFrame, localsDict, freevars, freeoffset, true);
2993+
}
2994+
2995+
private static void copyLocalsArray(Frame localFrame, Object localsDict, TruffleString[] namesArray, int offset, boolean deref) {
2996+
for (int i = 0; i < namesArray.length; i++) {
2997+
TruffleString varname = namesArray[i];
2998+
Object value = null;
2999+
try {
3000+
value = PyObjectGetItem.getUncached().execute(null, localsDict, varname);
3001+
} catch (AbstractTruffleException e) {
3002+
// CPython ignores all exceptions
3003+
}
3004+
if (deref) {
3005+
PCell cell = (PCell) localFrame.getObject(offset + i);
3006+
cell.setRef(value);
3007+
} else {
3008+
localFrame.setObject(offset + i, value);
3009+
}
3010+
}
3011+
}
3012+
29793013
private void profileCEvent(VirtualFrame virtualFrame, Object callable, PythonContext.ProfileEvent event, MutableLoopData mutableData, boolean profilingEnabled) {
29803014
if (profilingEnabled) {
29813015
profileCEvent(virtualFrame, callable, event, mutableData);
@@ -3012,7 +3046,10 @@ private void invokeProfileFunction(VirtualFrame virtualFrame, Object arg, Python
30123046
}
30133047

30143048
try {
3049+
// Force locals dict sync, so that we can sync them back later
3050+
GetFrameLocalsNode.getUncached().execute(pyFrame);
30153051
Object result = CallTernaryMethodNode.getUncached().execute(null, profileFun, pyFrame, event.name, arg == null ? PNone.NONE : arg);
3052+
syncLocalsBack(virtualFrame, pyFrame);
30163053
Object realResult = result == PNone.NONE ? null : result;
30173054
pyFrame.setLocalTraceFun(realResult);
30183055
} catch (Throwable e) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/GetFrameLocalsNode.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,8 @@ protected static FrameInfo getInfo(FrameDescriptor fd) {
140140
public static GetFrameLocalsNode create() {
141141
return GetFrameLocalsNodeGen.create();
142142
}
143+
144+
public static GetFrameLocalsNode getUncached() {
145+
return GetFrameLocalsNodeGen.getUncached();
146+
}
143147
}

0 commit comments

Comments
 (0)