diff --git a/src/embed_tests/pyinitialize.cs b/src/embed_tests/pyinitialize.cs index 1622f46d3..df791d664 100644 --- a/src/embed_tests/pyinitialize.cs +++ b/src/embed_tests/pyinitialize.cs @@ -46,6 +46,22 @@ public static void LoadSpecificArgs() } } + // regression test for https://github.com/pythonnet/pythonnet/issues/1561 + [Test] + public void ImportClassShutdownRefcount() + { + PythonEngine.Initialize(); + + PyObject ns = Py.Import(typeof(ImportClassShutdownRefcountClass).Namespace); + PyObject cls = ns.GetAttr(nameof(ImportClassShutdownRefcountClass)); + ns.Dispose(); + + Assert.Less(cls.Refcount, 256); + + PythonEngine.Shutdown(); + Assert.Greater(cls.Refcount, 0); + } + /// /// Failing test demonstrating current issue with OverflowException (#376) /// and ArgumentException issue after that one is fixed. @@ -182,4 +198,6 @@ public static void TestRunExitFuncs() Assert.True(called); } } + + public class ImportClassShutdownRefcountClass { } } diff --git a/src/runtime/classmanager.cs b/src/runtime/classmanager.cs index 18b9f6911..55c330af7 100644 --- a/src/runtime/classmanager.cs +++ b/src/runtime/classmanager.cs @@ -67,7 +67,6 @@ internal static void DisposePythonWrappersForClrTypes() // since others may still referencing it. cls.CallTypeTraverse(TraverseTypeClear, visitedPtr); cls.CallTypeClear(); - cls.DecrRefCount(); } } finally