From 089e5a8a34ad6c8a03d93a9c4911b00231ec1f41 Mon Sep 17 00:00:00 2001 From: "quantconnect-kronos[bot]" <346399058+quantconnect-kronos[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 18:22:29 +0000 Subject: [PATCH 1/5] Keep the Python object of a __namespace__ subclass of a .NET exception A Python subclass of System.Exception, or of any exception type, that declares __namespace__ gets a generated .NET type, but that type was reflected as a plain exception rather than as a Python-derived type. Its Python wrapper never stored itself on the .NET object, so the generated constructor created a temporary wrapper, kept its pointer and released it: later calls through the .NET object, such as an overridden virtual method or the object coming back from .NET, used a freed object and could crash the process. An __init__ that didn't call the base __init__ also left the base constructor unrun, so field initializers stayed null. Generated exception types now get the derived-type handling of ordinary __namespace__ subclasses (the wrapper set on the object, the base constructor run before __init__, the object's lifetime tied to the .NET object) together with the exception repr, str and args. Co-Authored-By: Claude Opus 5.5 --- src/runtime/ClassManager.cs | 16 ++--- src/runtime/Types/ExceptionClassObject.cs | 27 ++++++++ tests/test_exceptions.py | 75 ++++++++++++++++++++++- 3 files changed, 110 insertions(+), 8 deletions(-) diff --git a/src/runtime/ClassManager.cs b/src/runtime/ClassManager.cs index b4aefacc2..a796424e4 100644 --- a/src/runtime/ClassManager.cs +++ b/src/runtime/ClassManager.cs @@ -190,17 +190,19 @@ internal static ClassBase CreateClass(Type type) impl = new InterfaceObject(type); } - else if (type == typeof(Exception) || - type.IsSubclassOf(typeof(Exception))) - { - impl = new ExceptionClassObject(type); - } - #pragma warning disable CS0618 // Type or member is obsolete. OK for internal use. else if (null != PythonDerivedType.GetPyObjField(type)) #pragma warning restore CS0618 // Type or member is obsolete { - impl = new ClassDerivedObject(type); + impl = type.IsSubclassOf(typeof(Exception)) + ? new ExceptionClassDerivedObject(type) + : new ClassDerivedObject(type); + } + + else if (type == typeof(Exception) || + type.IsSubclassOf(typeof(Exception))) + { + impl = new ExceptionClassObject(type); } else if (typeof(IDynamicMetaObjectProvider).IsAssignableFrom(type)) diff --git a/src/runtime/Types/ExceptionClassObject.cs b/src/runtime/Types/ExceptionClassObject.cs index 762c9255a..037b53df5 100644 --- a/src/runtime/Types/ExceptionClassObject.cs +++ b/src/runtime/Types/ExceptionClassObject.cs @@ -76,3 +76,30 @@ public override bool Init(BorrowedReference obj, BorrowedReference args, Borrowe return Exceptions.SetArgsAndCause(obj, (Exception)e.inst); } } + +/// +/// Implementation for the managed types generated for Python subclasses of exceptions +/// that declare __namespace__: the generated object keeps its Python object as derived +/// types do, while it still reads as an exception from Python. +/// +[Serializable] +internal class ExceptionClassDerivedObject : ClassDerivedObject +{ + internal ExceptionClassDerivedObject(Type tp) : base(tp) + { + } + + // Type slots are looked up on the implementing type's hierarchy, which here is ClassDerivedObject's + public new static NewReference tp_repr(BorrowedReference ob) => ExceptionClassObject.tp_repr(ob); + + public new static NewReference tp_str(BorrowedReference ob) => ExceptionClassObject.tp_str(ob); + + public override bool Init(BorrowedReference obj, BorrowedReference args, BorrowedReference kw) + { + if (!base.Init(obj, args, kw)) return false; + + var e = (CLRObject)GetManagedObject(obj)!; + + return Exceptions.SetArgsAndCause(obj, (Exception)e.inst); + } +} diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 5334c06a7..1a050c459 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -2,6 +2,8 @@ """Test exception support.""" +import os +import subprocess import sys import System @@ -447,4 +449,75 @@ def test_dont_leak_exceptions_inner(): except TypeError: print('type error, as expected') except System.ArgumentException: - print('type error, also expected') \ No newline at end of file + print('type error, also expected') + + +_NAMESPACED_EXCEPTION_SCRIPT = """ +import os, sys +bin_path = sys.argv[1] +sys.path.append(bin_path) +from clr_loader import get_coreclr +from pythonnet import set_runtime +set_runtime(get_coreclr(runtime_config=os.path.join(bin_path, "Python.Test.runtimeconfig.json"))) +import clr +clr.AddReference("Python.Test") +import gc +from Python.Test import ExtendedException +from System import GC, Object +from System.Collections.Generic import List + +class WithoutInit(ExtendedException): + __namespace__ = "Python.Test.NamespacedExceptions" + +class InitWithoutSuper(ExtendedException): + __namespace__ = "Python.Test.NamespacedExceptions" + def __init__(self): + pass + def ToString(self): + return "overridden" + +class InitWithSuper(ExtendedException): + __namespace__ = "Python.Test.NamespacedExceptions" + def __init__(self): + super().__init__("message") + +def check(cls): + error = cls() + held = List[Object]() + held.Add(error) + try: + raise error + except ExtendedException as caught: + raised = caught is error + print(cls.__name__, error.extra, held[0] is error, raised, str(error), error.args) + +check(WithoutInit) +check(InitWithoutSuper) +check(InitWithSuper) +gc.collect() +for _ in range(2): + GC.Collect() + GC.WaitForPendingFinalizers() +print("collected") +""" + + +@pytest.mark.skipif(os.environ.get("PYTHONNET_RUNTIME") != "netcore", reason="loads the netcore runtime") +def test_namespaced_exception_subclass_construction(): + """A Python subclass of a .NET exception declaring __namespace__ runs its base + constructor, keeps its Python object for .NET and its overrides, and still + reads as an exception; it runs in a subprocess so a native crash fails the test""" + from Python.Test import ExtendedException + + bin_path = os.path.dirname(ExtendedException().GetType().Assembly.Location) + result = subprocess.run([sys.executable, "-c", _NAMESPACED_EXCEPTION_SCRIPT, bin_path], + capture_output=True, text=True, timeout=300) + + assert result.returncode == 0, result.stderr + overflow = "Arithmetic operation resulted in an overflow." + assert result.stdout.splitlines()[-4:] == [ + f"WithoutInit extra True True {overflow} ('{overflow}',)", + f"InitWithoutSuper extra True True overridden ('{overflow}',)", + "InitWithSuper extra True True message ('message',)", + "collected", + ] From e491af154161b090d9c31b788b31b70ca02fc8d2 Mon Sep 17 00:00:00 2001 From: "quantconnect-kronos[bot]" <346399058+quantconnect-kronos[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 19:34:21 +0000 Subject: [PATCH 2/5] Release __namespace__ exception subclasses safely - A __namespace__ subclass of another one has a generated Finalize at each level, all calling PythonDerivedType.PyFinalize on the same object, which queued the same Python object once per level for release: freed twice, the process crashed. PyFinalize now clears the object's __pyobj__ field once it queued it, and skips an object it already queued. This also fixes chains of __namespace__ subclasses of System.Object. - The derived types' tp_clear is a no-op, so an exception referencing itself through args, __cause__, __context__ or __traceback__ was never collected. A __namespace__ exception subclass now clears those fields, keeping its wrapper for the .NET object. - A Python ToString override that raised escaped the exception str() slot and aborted the process; its error is now raised to str()'s caller. Co-Authored-By: Claude Opus 5.5 --- src/runtime/Types/ClassDerived.cs | 7 ++ src/runtime/Types/ExceptionClassObject.cs | 16 +++- tests/test_exceptions.py | 102 ++++++++++++++++++++-- 3 files changed, 115 insertions(+), 10 deletions(-) diff --git a/src/runtime/Types/ClassDerived.cs b/src/runtime/Types/ClassDerived.cs index 6c2c81b13..32326451e 100644 --- a/src/runtime/Types/ClassDerived.cs +++ b/src/runtime/Types/ClassDerived.cs @@ -928,7 +928,14 @@ public static void PyFinalize(IPythonDerivedType obj) // the C# object is being destroyed which must mean there are no more // references to the Python object as well var self = GetPyObj(obj); + // every generated type in a chain of Python subclasses has its own Finalize calling this on the + // same object: only the first one queues its Python object + if (self.Ref == null) + { + return; + } Finalizer.Instance.AddDerivedFinalizedObject(ref self.RawObj, self.Run); + SetPyObj(obj, BorrowedReference.Null); } internal static void Finalize(IntPtr derived) diff --git a/src/runtime/Types/ExceptionClassObject.cs b/src/runtime/Types/ExceptionClassObject.cs index 037b53df5..77808c119 100644 --- a/src/runtime/Types/ExceptionClassObject.cs +++ b/src/runtime/Types/ExceptionClassObject.cs @@ -53,7 +53,16 @@ internal ExceptionClassObject(Type tp) : base(tp) return Exceptions.RaiseTypeError("invalid object"); } - string message = e.ToString(); + string message; + try + { + message = e.ToString(); + } + catch (Exception error) + { + Exceptions.SetError(error.InnerException ?? error); + return default; + } string fullTypeName = e.GetType().FullName; string prefix = fullTypeName + ": "; if (message.StartsWith(prefix)) @@ -94,6 +103,11 @@ internal ExceptionClassDerivedObject(Type tp) : base(tp) public new static NewReference tp_str(BorrowedReference ob) => ExceptionClassObject.tp_str(ob); + // ClassDerivedObject's tp_clear leaves the object to the .NET finalizer, but the exception's own fields + // (args, __cause__, __context__, __traceback__, __dict__) can form cycles that only clearing them breaks. + // The wrapper and its handle stay, as the .NET object may still use them. + public new static int tp_clear(BorrowedReference ob) => BaseUnmanagedClear(ob); + public override bool Init(BorrowedReference obj, BorrowedReference args, BorrowedReference kw) { if (!base.Init(obj, args, kw)) return false; diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 1a050c459..bb17c381c 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -452,7 +452,7 @@ def test_dont_leak_exceptions_inner(): print('type error, also expected') -_NAMESPACED_EXCEPTION_SCRIPT = """ +_NAMESPACED_EXCEPTION_SCRIPT_SETUP = """ import os, sys bin_path = sys.argv[1] sys.path.append(bin_path) @@ -464,6 +464,22 @@ def test_dont_leak_exceptions_inner(): import gc from Python.Test import ExtendedException from System import GC, Object +""" + + +def _run_namespaced_exception_script(script): + """Runs the script in a subprocess, so a native crash fails the test instead of the test run""" + from Python.Test import ExtendedException + + bin_path = os.path.dirname(ExtendedException().GetType().Assembly.Location) + result = subprocess.run([sys.executable, "-c", _NAMESPACED_EXCEPTION_SCRIPT_SETUP + script, bin_path], + capture_output=True, text=True, timeout=300) + + assert result.returncode == 0, result.stderr + return result.stdout.splitlines() + + +_NAMESPACED_EXCEPTION_SCRIPT = """ from System.Collections.Generic import List class WithoutInit(ExtendedException): @@ -506,18 +522,86 @@ def check(cls): def test_namespaced_exception_subclass_construction(): """A Python subclass of a .NET exception declaring __namespace__ runs its base constructor, keeps its Python object for .NET and its overrides, and still - reads as an exception; it runs in a subprocess so a native crash fails the test""" - from Python.Test import ExtendedException - - bin_path = os.path.dirname(ExtendedException().GetType().Assembly.Location) - result = subprocess.run([sys.executable, "-c", _NAMESPACED_EXCEPTION_SCRIPT, bin_path], - capture_output=True, text=True, timeout=300) + reads as an exception""" + output = _run_namespaced_exception_script(_NAMESPACED_EXCEPTION_SCRIPT) - assert result.returncode == 0, result.stderr overflow = "Arithmetic operation resulted in an overflow." - assert result.stdout.splitlines()[-4:] == [ + assert output[-4:] == [ f"WithoutInit extra True True {overflow} ('{overflow}',)", f"InitWithoutSuper extra True True overridden ('{overflow}',)", "InitWithSuper extra True True message ('message',)", "collected", ] + + +_NAMESPACED_EXCEPTION_RELEASE_SCRIPT = """ +class Probe(Object): + __namespace__ = "Python.Test.ReleasedExceptions" + def ToString(self): + return "probe" + +class ParentError(ExtendedException): + __namespace__ = "Python.Test.ReleasedExceptions" + def __init__(self): + pass + +class ChildError(ParentError): + __namespace__ = "Python.Test.ReleasedExceptions" + def __init__(self): + pass + +class CycleError(ExtendedException): + __namespace__ = "Python.Test.ReleasedExceptions" + def __init__(self): + pass + +class FailingToString(ExtendedException): + __namespace__ = "Python.Test.ReleasedExceptions" + def __init__(self): + pass + def ToString(self): + raise ValueError("ToString failed") + +def release(): + for _ in range(3): + gc.collect() + GC.Collect() + GC.WaitForPendingFinalizers() + # each call from .NET into the Python override creates Python objects, which + # periodically release the queued Python objects of finalized .NET objects + for _ in range(300): + str(probe) + +probe = Probe() + +for _ in range(10): + ChildError() +release() +print("released") + +for _ in range(10): + error = CycleError() + error.__cause__ = error + error = CycleError() + error.__context__ = error + error = CycleError() + error.args = (error,) +del error +release() +print(sum(1 for o in gc.get_objects() if type(o) is CycleError)) + +try: + str(FailingToString()) +except ValueError as error: + print(error) +""" + + +@pytest.mark.skipif(os.environ.get("PYTHONNET_RUNTIME") != "netcore", reason="loads the netcore runtime") +def test_namespaced_exception_subclass_release(): + """A __namespace__ exception subclass of another one is released once, exceptions + referencing themselves through args, __cause__ or __context__ are collected, and + an error raised by a Python ToString override reaches str()'s caller""" + output = _run_namespaced_exception_script(_NAMESPACED_EXCEPTION_RELEASE_SCRIPT) + + assert output[-3:] == ["released", "0", "ToString failed"] From d282ec15c668feb3b480da6a55a3b37da9fea6f3 Mon Sep 17 00:00:00 2001 From: "quantconnect-kronos[bot]" <346399058+quantconnect-kronos[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:07:24 +0000 Subject: [PATCH 3/5] Raise errors from Message overrides in exception repr() and __init__ A __namespace__ exception subclass's Python overrides are reached from .NET now, so a Message override that raised escaped tp_repr, and __init__ through Exceptions.SetArgsAndCause, aborting the process. Both now raise the error to the caller, as tp_str does for ToString. The exception Init of reflected and generated types shares the guarded SetArgsAndCause call. Co-Authored-By: Claude Opus 5.5 --- src/runtime/Types/ExceptionClassObject.cs | 42 ++++++++++++++++------- tests/test_exceptions.py | 23 +++++++++++-- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/runtime/Types/ExceptionClassObject.cs b/src/runtime/Types/ExceptionClassObject.cs index 77808c119..1597d5c6a 100644 --- a/src/runtime/Types/ExceptionClassObject.cs +++ b/src/runtime/Types/ExceptionClassObject.cs @@ -30,10 +30,20 @@ internal ExceptionClassObject(Type tp) : base(tp) return Exceptions.RaiseTypeError("invalid object"); } string name = e.GetType().Name; + string exceptionMessage; + try + { + exceptionMessage = e.Message; + } + catch (Exception error) + { + Exceptions.SetError(error.InnerException ?? error); + return default; + } string message; - if (e.Message != String.Empty) + if (exceptionMessage != String.Empty) { - message = String.Format("{0}('{1}')", name, e.Message); + message = String.Format("{0}('{1}')", name, exceptionMessage); } else { @@ -77,12 +87,24 @@ internal ExceptionClassObject(Type tp) : base(tp) } public override bool Init(BorrowedReference obj, BorrowedReference args, BorrowedReference kw) - { - if (!base.Init(obj, args, kw)) return false; + => base.Init(obj, args, kw) && SetArgsAndCause(obj); + /// + /// Sets the args and __cause__ of the constructed exception, raising to Python an error + /// its members throw, such as a Python override of Message + /// + internal static bool SetArgsAndCause(BorrowedReference obj) + { var e = (CLRObject)GetManagedObject(obj)!; - - return Exceptions.SetArgsAndCause(obj, (Exception)e.inst); + try + { + return Exceptions.SetArgsAndCause(obj, (Exception)e.inst); + } + catch (Exception error) + { + Exceptions.SetError(error.InnerException ?? error); + return false; + } } } @@ -109,11 +131,5 @@ internal ExceptionClassDerivedObject(Type tp) : base(tp) public new static int tp_clear(BorrowedReference ob) => BaseUnmanagedClear(ob); public override bool Init(BorrowedReference obj, BorrowedReference args, BorrowedReference kw) - { - if (!base.Init(obj, args, kw)) return false; - - var e = (CLRObject)GetManagedObject(obj)!; - - return Exceptions.SetArgsAndCause(obj, (Exception)e.inst); - } + => base.Init(obj, args, kw) && ExceptionClassObject.SetArgsAndCause(obj); } diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index bb17c381c..b68c5d168 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -594,6 +594,24 @@ def release(): str(FailingToString()) except ValueError as error: print(error) + +class FailingMessage(ExtendedException): + __namespace__ = "Python.Test.ReleasedExceptions" + def __init__(self, call_base): + if call_base: + super().__init__() + def get_Message(self): + raise ValueError("Message failed") + +try: + repr(FailingMessage(False)) +except ValueError as error: + print("repr", error) + +try: + FailingMessage(True) +except ValueError as error: + print("init", error) """ @@ -601,7 +619,8 @@ def release(): def test_namespaced_exception_subclass_release(): """A __namespace__ exception subclass of another one is released once, exceptions referencing themselves through args, __cause__ or __context__ are collected, and - an error raised by a Python ToString override reaches str()'s caller""" + an error raised by a Python ToString or Message override reaches the caller of + str(), repr() or the constructor""" output = _run_namespaced_exception_script(_NAMESPACED_EXCEPTION_RELEASE_SCRIPT) - assert output[-3:] == ["released", "0", "ToString failed"] + assert output[-5:] == ["released", "0", "ToString failed", "repr Message failed", "init Message failed"] From ab439ce92dc13d5aebc902d852f831006c936204 Mon Sep 17 00:00:00 2001 From: "quantconnect-kronos[bot]" <346399058+quantconnect-kronos[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:29:57 +0000 Subject: [PATCH 4/5] Keep exception formatting and its override errors inside the slot guards - tp_str and tp_repr of exceptions now run all their work inside the guard: a Python ToString override returning None crashed on the prefix stripping after the guarded call. None now reads as an empty message. - The guards raised the InnerException of the caught error, which for a Python override's error is its __cause__: raise ValueError() from KeyError() reached the caller as KeyError. ToString and Message are called directly here, so the error is raised as it is. - The override error cases move to their own subprocess test, which constructs the exception before its overrides start failing, so repr() is reached, and checks each error's type and cause. Co-Authored-By: Claude Opus 5.5 --- src/runtime/Types/ExceptionClassObject.cs | 54 +++++++-------- tests/test_exceptions.py | 80 ++++++++++++++--------- 2 files changed, 74 insertions(+), 60 deletions(-) diff --git a/src/runtime/Types/ExceptionClassObject.cs b/src/runtime/Types/ExceptionClassObject.cs index 1597d5c6a..422612447 100644 --- a/src/runtime/Types/ExceptionClassObject.cs +++ b/src/runtime/Types/ExceptionClassObject.cs @@ -29,27 +29,22 @@ internal ExceptionClassObject(Type tp) : base(tp) { return Exceptions.RaiseTypeError("invalid object"); } - string name = e.GetType().Name; - string exceptionMessage; + // Message may be a Python override: its error is raised to the caller as it is try { - exceptionMessage = e.Message; + string name = e.GetType().Name; + string message = e.Message; + if (!string.IsNullOrEmpty(message)) + { + return Runtime.PyString_FromString(String.Format("{0}('{1}')", name, message)); + } + return Runtime.PyString_FromString(String.Format("{0}()", name)); } catch (Exception error) { - Exceptions.SetError(error.InnerException ?? error); + Exceptions.SetError(error); return default; } - string message; - if (exceptionMessage != String.Empty) - { - message = String.Format("{0}('{1}')", name, exceptionMessage); - } - else - { - message = String.Format("{0}()", name); - } - return Runtime.PyString_FromString(message); } /// @@ -63,27 +58,28 @@ internal ExceptionClassObject(Type tp) : base(tp) return Exceptions.RaiseTypeError("invalid object"); } - string message; + // ToString may be a Python override: its error is raised to the caller as it is, and a None + // it returns reads as an empty message try { - message = e.ToString(); + string message = e.ToString() ?? string.Empty; + string fullTypeName = e.GetType().FullName; + string prefix = fullTypeName + ": "; + if (message.StartsWith(prefix)) + { + message = message.Substring(prefix.Length); + } + else if (message.StartsWith(fullTypeName)) + { + message = message.Substring(fullTypeName.Length); + } + return Runtime.PyString_FromString(message); } catch (Exception error) { - Exceptions.SetError(error.InnerException ?? error); + Exceptions.SetError(error); return default; } - string fullTypeName = e.GetType().FullName; - string prefix = fullTypeName + ": "; - if (message.StartsWith(prefix)) - { - message = message.Substring(prefix.Length); - } - else if (message.StartsWith(fullTypeName)) - { - message = message.Substring(fullTypeName.Length); - } - return Runtime.PyString_FromString(message); } public override bool Init(BorrowedReference obj, BorrowedReference args, BorrowedReference kw) @@ -102,7 +98,7 @@ internal static bool SetArgsAndCause(BorrowedReference obj) } catch (Exception error) { - Exceptions.SetError(error.InnerException ?? error); + Exceptions.SetError(error); return false; } } diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index b68c5d168..7dd777492 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -555,13 +555,6 @@ class CycleError(ExtendedException): def __init__(self): pass -class FailingToString(ExtendedException): - __namespace__ = "Python.Test.ReleasedExceptions" - def __init__(self): - pass - def ToString(self): - raise ValueError("ToString failed") - def release(): for _ in range(3): gc.collect() @@ -590,37 +583,62 @@ def release(): release() print(sum(1 for o in gc.get_objects() if type(o) is CycleError)) -try: - str(FailingToString()) -except ValueError as error: - print(error) +""" -class FailingMessage(ExtendedException): - __namespace__ = "Python.Test.ReleasedExceptions" - def __init__(self, call_base): - if call_base: + +@pytest.mark.skipif(os.environ.get("PYTHONNET_RUNTIME") != "netcore", reason="loads the netcore runtime") +def test_namespaced_exception_subclass_release(): + """A __namespace__ exception subclass of another one is released once, and exceptions + referencing themselves through args, __cause__ or __context__ are collected""" + output = _run_namespaced_exception_script(_NAMESPACED_EXCEPTION_RELEASE_SCRIPT) + + assert output[-2:] == ["released", "0"] + + +_NAMESPACED_EXCEPTION_OVERRIDES_SCRIPT = """ +class OverridingError(ExtendedException): + __namespace__ = "Python.Test.OverridingExceptions" + def __init__(self, failing=False): + self.failing = failing + if failing: super().__init__() + def fail(self): + raise ValueError("outer") from KeyError("inner") def get_Message(self): - raise ValueError("Message failed") + if self.failing: + self.fail() + return "message" + def ToString(self): + if self.failing: + self.fail() + return None + +error = OverridingError() +print(repr(str(error)), repr(error)) +error.failing = True -try: - repr(FailingMessage(False)) -except ValueError as error: - print("repr", error) +def check(name, call): + try: + call() + except ValueError as raised: + print(name, raised, type(raised.__cause__).__name__, raised.__cause__) -try: - FailingMessage(True) -except ValueError as error: - print("init", error) +check("str", lambda: str(error)) +check("repr", lambda: repr(error)) +check("init", lambda: OverridingError(failing=True)) """ @pytest.mark.skipif(os.environ.get("PYTHONNET_RUNTIME") != "netcore", reason="loads the netcore runtime") -def test_namespaced_exception_subclass_release(): - """A __namespace__ exception subclass of another one is released once, exceptions - referencing themselves through args, __cause__ or __context__ are collected, and - an error raised by a Python ToString or Message override reaches the caller of - str(), repr() or the constructor""" - output = _run_namespaced_exception_script(_NAMESPACED_EXCEPTION_RELEASE_SCRIPT) +def test_namespaced_exception_subclass_override_errors(): + """str(), repr() and the constructor of a __namespace__ exception subclass read None + from a Python ToString override as an empty message, and raise the error of a Python + ToString or Message override to their caller as it is, with its cause""" + output = _run_namespaced_exception_script(_NAMESPACED_EXCEPTION_OVERRIDES_SCRIPT) - assert output[-5:] == ["released", "0", "ToString failed", "repr Message failed", "init Message failed"] + assert output[-4:] == [ + "'' OverridingError('message')", + "str outer KeyError 'inner'", + "repr outer KeyError 'inner'", + "init outer KeyError 'inner'", + ] From 2c5dc3b252e010c673ab6055751799f3dd885276 Mon Sep 17 00:00:00 2001 From: "quantconnect-kronos[bot]" <346399058+quantconnect-kronos[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:38:54 +0000 Subject: [PATCH 5/5] Release a derived object's Python object after its base finalizers - A generated type's Finalize released the Python object before calling its base class finalizer, and since releasing became once only it clears __pyobj__: a base finalizer calling a virtual member then no longer reached the Python override (the .NET implementation ran, or an abstract one threw and terminated the process). Finalize now runs the base finalizer first: the innermost generated type queues the Python object once the finalizer chain ran, and the outer ones skip it. - An inherited ToString runs through reflection, which wraps the error of a Python Message override it calls in a TargetInvocationException. The exception slots now raise that wrapper's inner error, and still raise a PythonException as it is, with its cause. Co-Authored-By: Claude Opus 5.5 --- src/runtime/Types/ClassDerived.cs | 9 ++-- src/runtime/Types/ExceptionClassObject.cs | 22 +++++--- src/testing/constructortests.cs | 24 +++++++++ tests/test_exceptions.py | 65 +++++++++++++++++++++-- 4 files changed, 105 insertions(+), 15 deletions(-) diff --git a/src/runtime/Types/ClassDerived.cs b/src/runtime/Types/ClassDerived.cs index 32326451e..daeacfcd7 100644 --- a/src/runtime/Types/ClassDerived.cs +++ b/src/runtime/Types/ClassDerived.cs @@ -269,7 +269,8 @@ internal static Type CreateDerivedType(string name, } } - // add the destructor so the python object created in the constructor gets destroyed + // add the destructor so the python object created in the constructor gets destroyed, + // after the base class finalizers, which may still call the Python overrides MethodBuilder methodBuilder = typeBuilder.DefineMethod("Finalize", MethodAttributes.Family | MethodAttributes.Virtual | @@ -279,11 +280,11 @@ internal static Type CreateDerivedType(string name, Type.EmptyTypes); ILGenerator il = methodBuilder.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); + il.Emit(OpCodes.Call, baseClass.GetMethod("Finalize", BindingFlags.NonPublic | BindingFlags.Instance)); + il.Emit(OpCodes.Ldarg_0); #pragma warning disable CS0618 // PythonDerivedType is for internal use only il.Emit(OpCodes.Call, typeof(PythonDerivedType).GetMethod(nameof(PyFinalize))); #pragma warning restore CS0618 // PythonDerivedType is for internal use only - il.Emit(OpCodes.Ldarg_0); - il.Emit(OpCodes.Call, baseClass.GetMethod("Finalize", BindingFlags.NonPublic | BindingFlags.Instance)); il.Emit(OpCodes.Ret); Type type = typeBuilder.CreateType(); @@ -929,7 +930,7 @@ public static void PyFinalize(IPythonDerivedType obj) // references to the Python object as well var self = GetPyObj(obj); // every generated type in a chain of Python subclasses has its own Finalize calling this on the - // same object: only the first one queues its Python object + // same object, once the finalizers of its base classes ran: only the first one queues its Python object if (self.Ref == null) { return; diff --git a/src/runtime/Types/ExceptionClassObject.cs b/src/runtime/Types/ExceptionClassObject.cs index 422612447..672d8c86a 100644 --- a/src/runtime/Types/ExceptionClassObject.cs +++ b/src/runtime/Types/ExceptionClassObject.cs @@ -1,4 +1,5 @@ using System; +using System.Reflection; namespace Python.Runtime; @@ -29,7 +30,6 @@ internal ExceptionClassObject(Type tp) : base(tp) { return Exceptions.RaiseTypeError("invalid object"); } - // Message may be a Python override: its error is raised to the caller as it is try { string name = e.GetType().Name; @@ -42,7 +42,7 @@ internal ExceptionClassObject(Type tp) : base(tp) } catch (Exception error) { - Exceptions.SetError(error); + SetError(error); return default; } } @@ -58,8 +58,7 @@ internal ExceptionClassObject(Type tp) : base(tp) return Exceptions.RaiseTypeError("invalid object"); } - // ToString may be a Python override: its error is raised to the caller as it is, and a None - // it returns reads as an empty message + // a None returned by a Python override of ToString reads as an empty message try { string message = e.ToString() ?? string.Empty; @@ -77,11 +76,22 @@ internal ExceptionClassObject(Type tp) : base(tp) } catch (Exception error) { - Exceptions.SetError(error); + SetError(error); return default; } } + /// + /// Raises to Python the error of an exception member, as it is: a Python override of the member + /// throws its PythonException directly, whose InnerException is its cause, while a member the + /// Python subclass inherits runs through reflection, which wraps what it throws + /// + static void SetError(Exception error) + { + var raised = error is TargetInvocationException { InnerException: { } invoked } ? invoked : error; + Exceptions.SetError(raised); + } + public override bool Init(BorrowedReference obj, BorrowedReference args, BorrowedReference kw) => base.Init(obj, args, kw) && SetArgsAndCause(obj); @@ -98,7 +108,7 @@ internal static bool SetArgsAndCause(BorrowedReference obj) } catch (Exception error) { - Exceptions.SetError(error); + SetError(error); return false; } } diff --git a/src/testing/constructortests.cs b/src/testing/constructortests.cs index 5912853b0..d57bcdaf7 100644 --- a/src/testing/constructortests.cs +++ b/src/testing/constructortests.cs @@ -108,4 +108,28 @@ public FinalizedConstructorTest(Exception toThrow) Interlocked.Increment(ref Finalized); } } + + public abstract class AbstractFinalizerCallbackTest + { + public static int Result; + + public abstract int Callback(); + + ~AbstractFinalizerCallbackTest() + { + Result = Callback(); + } + } + + public class VirtualFinalizerCallbackTest + { + public static int Result; + + public virtual int Callback() => 7; + + ~VirtualFinalizerCallbackTest() + { + Result = Callback(); + } + } } diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 7dd777492..0337e7a28 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -583,16 +583,56 @@ def release(): release() print(sum(1 for o in gc.get_objects() if type(o) is CycleError)) +from Python.Test import AbstractFinalizerCallbackTest, VirtualFinalizerCallbackTest + +class AbstractCallback(AbstractFinalizerCallbackTest): + __namespace__ = "Python.Test.ReleasedExceptions" + def Callback(self): + return 42 + +class AbstractCallbackChild(AbstractCallback): + __namespace__ = "Python.Test.ReleasedExceptions" + def Callback(self): + return 43 + +class VirtualCallback(VirtualFinalizerCallbackTest): + __namespace__ = "Python.Test.ReleasedExceptions" + def Callback(self): + return 42 + +class VirtualCallbackChild(VirtualCallback): + __namespace__ = "Python.Test.ReleasedExceptions" + def Callback(self): + return 43 + +for cls, base in ((AbstractCallback, AbstractFinalizerCallbackTest), + (AbstractCallbackChild, AbstractFinalizerCallbackTest), + (VirtualCallback, VirtualFinalizerCallbackTest), + (VirtualCallbackChild, VirtualFinalizerCallbackTest)): + base.Result = 0 + cls() + release() + print(cls.__name__, base.Result) + """ @pytest.mark.skipif(os.environ.get("PYTHONNET_RUNTIME") != "netcore", reason="loads the netcore runtime") def test_namespaced_exception_subclass_release(): - """A __namespace__ exception subclass of another one is released once, and exceptions - referencing themselves through args, __cause__ or __context__ are collected""" + """A __namespace__ exception subclass of another one is released once, exceptions + referencing themselves through args, __cause__ or __context__ are collected, and + the finalizer of a .NET base class still reaches the Python overrides of its + __namespace__ subclasses, before their Python object is released""" output = _run_namespaced_exception_script(_NAMESPACED_EXCEPTION_RELEASE_SCRIPT) - assert output[-2:] == ["released", "0"] + assert output[-6:] == [ + "released", + "0", + "AbstractCallback 42", + "AbstractCallbackChild 43", + "VirtualCallback 42", + "VirtualCallbackChild 43", + ] _NAMESPACED_EXCEPTION_OVERRIDES_SCRIPT = """ @@ -626,6 +666,19 @@ def check(name, call): check("str", lambda: str(error)) check("repr", lambda: repr(error)) check("init", lambda: OverridingError(failing=True)) + +class InheritedToStringError(ExtendedException): + __namespace__ = "Python.Test.OverridingExceptions" + def __init__(self): + self.failing = False + def get_Message(self): + if self.failing: + raise ValueError("outer") from KeyError("inner") + return "message" + +inherited = InheritedToStringError() +inherited.failing = True +check("inherited str", lambda: str(inherited)) """ @@ -633,12 +686,14 @@ def check(name, call): def test_namespaced_exception_subclass_override_errors(): """str(), repr() and the constructor of a __namespace__ exception subclass read None from a Python ToString override as an empty message, and raise the error of a Python - ToString or Message override to their caller as it is, with its cause""" + ToString or Message override to their caller as it is, with its cause, also when an + inherited ToString calls the Message override""" output = _run_namespaced_exception_script(_NAMESPACED_EXCEPTION_OVERRIDES_SCRIPT) - assert output[-4:] == [ + assert output[-5:] == [ "'' OverridingError('message')", "str outer KeyError 'inner'", "repr outer KeyError 'inner'", "init outer KeyError 'inner'", + "inherited str outer KeyError 'inner'", ]