Skip to content

Keep the cause of a ToString override's error in str() - #175

Merged
quantconnect-kronos[bot] merged 1 commit into
masterfrom
kronos/bug-170-keep-tostring-error-cause
Nov 23, 2026
Merged

quantconnect-kronos[bot] merged 1 commit into
masterfrom
kronos/bug-170-keep-tostring-error-cause

Conversation

@quantconnect-kronos

Copy link
Copy Markdown

Closes #170

What changed and why

ClassBase.tp_str catches what ToString() throws and used to raise its InnerException whenever it had one, to strip reflection wrappers such as the TargetInvocationException thrown when a Python subclass inherits a .NET ToString. A Python ToString override's error arrives as a PythonException, whose InnerException is the Python error's __cause__ (PythonException.FromPyErr). So for raise ValueError("outer") from KeyError("inner"), str() raised the KeyError, and except ValueError didn't catch it.

tp_str now raises a PythonException as it is, so the error keeps its type and its __cause__. Every other exception is unwrapped as before. That covers:

  • a .NET exception raised from a Python override, which arrives as ClrBubbledException wrapping the original and still reaches Python as the original .NET exception
  • a .NET ToString that throws
  • a Python subclass inheriting a .NET ToString, whose exception is wrapped in a TargetInvocationException and still unwrapped

tp_repr is unchanged. It already falls back to the default repr when ToString() throws, and #170 is about str().

Tests

New tests in tests/test_repr.py:

  • test_str_python_tostring_raises: a __namespace__ subclass of System.Object whose ToString override raises:

    • a chained ValueError("outer") from KeyError("inner"): str() raises the ValueError, and its __cause__ is the KeyError
    • a plain ValueError: raised with no cause
    • a .NET InvalidOperationException: raised as that type

    Without the runtime change, the chained case fails with KeyError: 'inner'.

  • test_str_tostring_throws: str() of a .NET type whose ToString throws, called directly and through a __namespace__ subclass that inherits it, raises the InvalidOperationException. It passes both before and after the change, and guards the unwrapping that is kept.

The CI steps, run locally on Python 3.11 / .NET 10 after pip install -v .:

  • dotnet test --runtime any-x64 src/embed_tests/ --filter "FullyQualifiedName!~SetPythonPath": 1049 passed, 8 skipped
  • dotnet test --runtime any-x64 src/embed_tests/ --filter "FullyQualifiedName~SetPythonPath": 1 passed
  • pytest --runtime netcore tests: 488 passed, 23 skipped
  • dotnet test --runtime any-x64 src/python_tests_runner/: 2 passed

Also tested end to end in a Lean Python backtest whose algorithm calls str() on the three kinds of override above and fails unless each result is right:

  • Before the change, it failed: the chained case raised KeyError: 'inner'.
  • With the change, it passed: ValueError: outer with a KeyError cause, ValueError: plain with no cause, and InvalidOperationException: from .NET.

The change is backwards compatible except for the fixed case: str() raises the same exceptions as before for everything except a PythonException that has a cause.

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 <noreply@anthropic.com>
@quantconnect-kronos quantconnect-kronos Bot added kronos:develop:working develop: in progress kronos:review Request review labels Nov 23, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued kronos:review:working review: in progress and removed kronos:review Request review kronos:review:queued review: queued labels Nov 23, 2026
@quantconnect-kronos-reviewer
quantconnect-kronos-reviewer Bot marked this pull request as ready for review November 23, 2026 16:45

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved, risk:low: a one-line guard in tp_str, easy to revert. All other exceptions are unwrapped exactly as before. Covered by tests and green CI, including the Lean regression and unit test runs, and tested end to end.

I checked it locally: tests/test_repr.py passes with the change. Without the ClassBase.cs change, test_str_python_tostring_raises fails with KeyError: 'inner', so the test reproduces #170. Exceptions.SetError restores a PythonException as is, so the original error and its __cause__ reach Python unchanged.

The same unwrap pattern elsewhere: Iterator.tp_iternext calls MoveNext() directly and has the same bug. A Python IEnumerator whose MoveNext raises ValueError(...) from KeyError(...) surfaces the KeyError; I reproduced this on this branch and opened #177 for it. It doesn't block this change. MethodBinder and the PropertyObject getter unwrap one level after a reflection Invoke, so a PythonException there is kept.

@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added risk:low Low risk and removed kronos:review:working review: in progress labels Nov 23, 2026
@quantconnect-kronos quantconnect-kronos Bot added the kronos:review Request review label Nov 23, 2026
@quantconnect-kronos-reviewer quantconnect-kronos-reviewer Bot added kronos:review:queued review: queued and removed kronos:review Request review labels Nov 23, 2026
@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot added kronos:review:working review: in progress and removed kronos:review:queued review: queued labels Nov 23, 2026

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved; no introduced defects found. The guard preserves the original Python error and its cause while leaving non-Python exception unwrapping unchanged.

Independent verification on Python 3.11 / .NET 10:

  • Python suite: 488 passed, 23 skipped.
  • Embedding tests: 1049 passed, 8 skipped, plus 1 passed for the isolated Python-path test.
  • Managed Python runner: 2 passed.
  • Against unchanged master, the new chained-error regression fails with KeyError: 'inner'; it passes with this change.
  • End-to-end exception assertions passed and the algorithm completed. The process subsequently encountered a shutdown error; an unchanged-master control produced the same error, so it is not introduced here.

Separately, a directly thrown .NET ToString exception with an inner exception still loses its outer exception on both master and this head. Filed #178; it does not block this narrowly scoped fix.

After release, Lean needs its pinned pythonnet package references updated.

@quantconnect-kronos-reviewer-2 quantconnect-kronos-reviewer-2 Bot removed the kronos:review:working review: in progress label Nov 23, 2026
@quantconnect-kronos
quantconnect-kronos Bot merged commit 0e25193 into master Nov 23, 2026
9 checks passed
@quantconnect-kronos
quantconnect-kronos Bot deleted the kronos/bug-170-keep-tostring-error-cause branch November 23, 2026 17:04
@quantconnect-kronos quantconnect-kronos Bot removed the kronos:develop:working develop: in progress label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:low Low risk

Projects

None yet

Development

Successfully merging this pull request may close these issues.

str() of a Python subclass of a .NET type raises the cause of its ToString override's error instead of the error

0 participants