Skip to content

Commit 84d39c4

Browse files
committed
[GR-60816] Add sys.exception()
PullRequest: graalpython/3629
2 parents f0d4ebe + 6c42df4 commit 84d39c4

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_sys.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
test.test_sys.ActiveExceptionTests.test_exc_info_no_exception @ darwin-arm64,darwin-x86_64,linux-aarch64,linux-x86_64,win32-AMD64
22
test.test_sys.ActiveExceptionTests.test_exc_info_with_exception_instance @ darwin-arm64,darwin-x86_64,linux-aarch64,linux-x86_64,win32-AMD64
33
test.test_sys.ActiveExceptionTests.test_exc_info_with_exception_type @ darwin-arm64,darwin-x86_64,linux-aarch64,linux-x86_64,win32-AMD64
4+
test.test_sys.ActiveExceptionTests.test_sys_exception_no_exception @ linux-x86_64
5+
test.test_sys.ActiveExceptionTests.test_sys_exception_with_exception_instance @ linux-x86_64
6+
test.test_sys.ActiveExceptionTests.test_sys_exception_with_exception_type @ linux-x86_64
47
test.test_sys.DisplayHookTest.test_custom_displayhook @ darwin-arm64,darwin-x86_64,linux-aarch64,linux-x86_64,win32-AMD64
58
test.test_sys.DisplayHookTest.test_lost_displayhook @ darwin-arm64,darwin-x86_64,linux-aarch64,linux-x86_64,win32-AMD64
69
test.test_sys.DisplayHookTest.test_original_displayhook @ darwin-arm64,darwin-x86_64,linux-aarch64,linux-x86_64,win32-AMD64

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -863,6 +863,25 @@ static PTuple run(VirtualFrame frame,
863863
}
864864
}
865865

866+
@Builtin(name = "exception", needsFrame = true)
867+
@GenerateNodeFactory
868+
abstract static class ExceptionNode extends PythonBuiltinNode {
869+
870+
@Specialization
871+
static Object run(VirtualFrame frame,
872+
@Bind("this") Node inliningTarget,
873+
@Cached GetEscapedExceptionNode getEscapedExceptionNode,
874+
@Cached GetCaughtExceptionNode getCaughtExceptionNode) {
875+
AbstractTruffleException currentException = getCaughtExceptionNode.execute(frame);
876+
assert currentException != PException.NO_EXCEPTION;
877+
if (currentException == null) {
878+
return PNone.NONE;
879+
} else {
880+
return getEscapedExceptionNode.execute(inliningTarget, currentException);
881+
}
882+
}
883+
}
884+
866885
// ATTENTION: this is intentionally a PythonBuiltinNode and not PythonUnaryBuiltinNode,
867886
// because we need a guarantee that this builtin will get its own stack frame in order to
868887
// be able to count how many frames down the call stack we need to walk

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/posix/DirEntryBuiltins.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -81,7 +81,6 @@
8181
import com.oracle.graal.python.runtime.PosixSupportLibrary;
8282
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
8383
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
84-
import com.oracle.truffle.api.CompilerDirectives;
8584
import com.oracle.truffle.api.dsl.Bind;
8685
import com.oracle.truffle.api.dsl.Cached;
8786
import com.oracle.truffle.api.dsl.Cached.Shared;
@@ -315,7 +314,6 @@ abstract static class TestModeNode extends Node {
315314

316315
private final long expectedMode;
317316
private final int expectedDirEntryType;
318-
private StatHelperNode statHelperNode;
319317

320318
protected TestModeNode(long expectedMode, int expectedDirEntryType) {
321319
this.expectedMode = expectedMode;
@@ -327,8 +325,9 @@ protected TestModeNode(long expectedMode, int expectedDirEntryType) {
327325
@Specialization(guards = "followSymlinks")
328326
boolean testModeUsingStat(VirtualFrame frame, PDirEntry self, boolean followSymlinks,
329327
@Bind("this") Node inliningTarget,
328+
@Shared @Cached StatHelperNode statHelperNode,
330329
@Shared @Cached SequenceStorageNodes.GetItemScalarNode getItemScalarNode) {
331-
PTuple statResult = getStatHelperNode().execute(frame, self, followSymlinks, true);
330+
PTuple statResult = statHelperNode.execute(frame, self, followSymlinks, true);
332331
if (statResult == null) {
333332
// file not found
334333
return false;
@@ -341,21 +340,14 @@ boolean testModeUsingStat(VirtualFrame frame, PDirEntry self, boolean followSyml
341340
@Specialization(guards = "!followSymlinks")
342341
boolean useTypeIfKnown(VirtualFrame frame, PDirEntry self, @SuppressWarnings("unused") boolean followSymlinks,
343342
@Bind("this") Node inliningTarget,
343+
@Shared @Cached StatHelperNode statHelperNode,
344344
@Shared @Cached SequenceStorageNodes.GetItemScalarNode getItemScalarNode,
345345
@CachedLibrary(limit = "1") PosixSupportLibrary posixLib) {
346346
int entryType = posixLib.dirEntryGetType(PosixSupport.get(this), self.dirEntryData);
347347
if (entryType != DT_UNKNOWN.value) {
348348
return entryType == expectedDirEntryType;
349349
}
350-
return testModeUsingStat(frame, self, false, inliningTarget, getItemScalarNode);
351-
}
352-
353-
private StatHelperNode getStatHelperNode() {
354-
if (statHelperNode == null) {
355-
CompilerDirectives.transferToInterpreterAndInvalidate();
356-
statHelperNode = insert(DirEntryBuiltinsFactory.StatHelperNodeGen.create());
357-
}
358-
return statHelperNode;
350+
return testModeUsingStat(frame, self, false, inliningTarget, statHelperNode, getItemScalarNode);
359351
}
360352

361353
@NeverDefault

0 commit comments

Comments
 (0)