Skip to content

Commit 6c42df4

Browse files
committed
Add sys.exception()
Fixes #467
1 parent 332c3da commit 6c42df4

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
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

0 commit comments

Comments
 (0)