Skip to content

Commit 94771ca

Browse files
committed
[GR-40604] Implement limited sys._current_frames
PullRequest: graalpython/2413
2 parents e9369d3 + 0b8a1cd commit 94771ca

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
import com.oracle.graal.python.builtins.modules.io.TextIOWrapperNodes.TextIOWrapperInitNode;
162162
import com.oracle.graal.python.builtins.modules.io.TextIOWrapperNodesFactory.TextIOWrapperInitNodeGen;
163163
import com.oracle.graal.python.builtins.objects.PNone;
164+
import com.oracle.graal.python.builtins.objects.common.HashingStorageLibrary;
164165
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
165166
import com.oracle.graal.python.builtins.objects.dict.PDict;
166167
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
@@ -234,6 +235,7 @@
234235
import com.oracle.truffle.api.dsl.Specialization;
235236
import com.oracle.truffle.api.frame.MaterializedFrame;
236237
import com.oracle.truffle.api.frame.VirtualFrame;
238+
import com.oracle.truffle.api.library.CachedLibrary;
237239
import com.oracle.truffle.api.nodes.Node;
238240
import com.oracle.truffle.api.profiles.ConditionProfile;
239241
import com.oracle.truffle.api.strings.TruffleString;
@@ -789,6 +791,26 @@ private PException raiseCallStackDepth() {
789791
}
790792
}
791793

794+
@Builtin(name = "_current_frames")
795+
@GenerateNodeFactory
796+
abstract static class CurrentFrames extends PythonBuiltinNode {
797+
@Specialization
798+
Object currentFrames(VirtualFrame frame,
799+
@Cached AuditNode auditNode,
800+
@Cached WarningsModuleBuiltins.WarnNode warnNode,
801+
@Cached ReadCallerFrameNode readCallerFrameNode,
802+
@CachedLibrary(limit = "1") HashingStorageLibrary hlib) {
803+
auditNode.audit("sys._current_frames");
804+
if (!getLanguage().singleThreadedAssumption.isValid()) {
805+
warnNode.warn(frame, RuntimeWarning, ErrorMessages.WARN_CURRENT_FRAMES_MULTITHREADED);
806+
}
807+
PFrame currentFrame = readCallerFrameNode.executeWith(frame, 0);
808+
PDict result = factory().createDict();
809+
result.setDictStorage(hlib.setItem(result.getDictStorage(), Thread.currentThread().getId(), currentFrame));
810+
return result;
811+
}
812+
}
813+
792814
@Builtin(name = "getfilesystemencoding", minNumOfPositionalArgs = 0)
793815
@GenerateNodeFactory
794816
public abstract static class GetFileSystemEncodingNode extends PythonBuiltinNode {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,8 @@ public abstract class ErrorMessages {
13421342
public static final TruffleString WARN_IGNORE_UNIMPORTABLE_BREAKPOINT_S = tsLiteral("Ignoring unimportable $PYTHONBREAKPOINT: \"%s\"");
13431343
public static final TruffleString WARN_DEPRECTATED_SYS_CHECKINTERVAL = tsLiteral("sys.getcheckinterval() and sys.setcheckinterval() " +
13441344
"are deprecated. Use sys.getswitchinterval() instead.");
1345+
public static final TruffleString WARN_CURRENT_FRAMES_MULTITHREADED = tsLiteral(
1346+
"GraalPy doesn't support obtaining frames of other threads. That means python debuggers can only see the currently stopped thread");
13451347

13461348
// error messages from parsing
13471349
public static final TruffleString ERROR_MESSAGE_EMPTY_EXPRESSION = tsLiteral("f-string: empty expression not allowed");

0 commit comments

Comments
 (0)