From 2057b4ca08370bfeace5ff167ba0ea37fed89a92 Mon Sep 17 00:00:00 2001 From: "quantconnect-kronos[bot]" <346399058+quantconnect-kronos[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 16:23:48 +0000 Subject: [PATCH] Keep the cause of a ToString override's error in str() ClassBase.tp_str replaced the exception thrown by ToString() with its InnerException, to unwrap reflection wrappers. A Python override's error arrives as a PythonException, whose InnerException is the Python error's __cause__, so str() raised the cause instead of the error. A PythonException is now raised as it is; other exceptions are unwrapped as before. Closes #170 Co-Authored-By: Claude Opus 5.5 --- src/runtime/Types/ClassBase.cs | 3 ++- tests/test_repr.py | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/runtime/Types/ClassBase.cs b/src/runtime/Types/ClassBase.cs index 19d8abf32..525be8a35 100644 --- a/src/runtime/Types/ClassBase.cs +++ b/src/runtime/Types/ClassBase.cs @@ -348,7 +348,8 @@ public static NewReference tp_str(BorrowedReference ob) } catch (Exception e) { - if (e.InnerException != null) + // a PythonException's InnerException is the Python error's __cause__, not a wrapper + if (e is not PythonException && e.InnerException != null) { e = e.InnerException; } diff --git a/tests/test_repr.py b/tests/test_repr.py index 1fce44228..26694a034 100644 --- a/tests/test_repr.py +++ b/tests/test_repr.py @@ -77,6 +77,48 @@ def test_tostring_throws(): ob = ReprTest.ThrowingToString() assert "