FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix shutdown regression with BinaryFormatter by filmor · Pull Request #2750 · pythonnet/pythonnet · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (2) .md  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 2 additions & 0 deletions CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].

### Fixed

- Fix shutdown serialization of CLR namespace modules on .NET 8 while preserving
namespace name tracking across runtime restarts.

## 3.2.0 - 2026-09-29

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
using Python.Runtime;

namespace Python.EmbeddingTest.StateSerialization;

[NonParallelizable]
public class ModuleSerialization
{
[TestCase(false)]
[TestCase(true)]
public void NamespaceModuleRoundtrip(bool loadNames)
{
var formatter = RuntimeData.CreateFormatter();
if (formatter is NoopFormatter)
{
Assert.Inconclusive("NoopFormatter in use, cannot perform serialization test.");
}

using var gil = Py.GIL();
using var pyModule = ModuleObject.Create("System").MoveToPyObject();
var module = (ModuleObject)ManagedType.GetManagedObject(pyModule)!;
using (var int32 = module.GetAttribute("Int32", true))
{
Assert.That(int32.IsNull(), Is.False);
}

string[] names = null;
if (loadNames)
{
using var all = pyModule.GetAttr("__all__");
names = all.As<string[]>();
Assert.That(names, Does.Contain("Int32"));
}

for (int i = 0; i < 2; i++)
{
var context = module.Save(pyModule);
using var buffer = new MemoryStream();
formatter.Serialize(buffer, new object[] { module, context });
buffer.Position = 0;
var restored = (object[])formatter.Deserialize(buffer);
ExtensionType.tp_clear(pyModule);
module = (ModuleObject)restored[0];
module.Load(pyModule, (Dictionary<string, object>)restored[1]);

using (var int32 = module.GetAttribute("Int32", true))
{
Assert.That(int32.IsNull(), Is.False);
}
using (var collections = module.GetAttribute("Collections", true))
{
Assert.That(collections.IsNull(), Is.False);
}

using var all = pyModule.GetAttr("__all__");
var restoredNames = all.As<string[]>();
Assert.That(restoredNames, Does.Contain("Int32"));
Assert.That(restoredNames, Is.Unique);
if (names is not null)
{
Assert.That(restoredNames, Is.EquivalentTo(names));
}
names = restoredNames;
}
}
}
12 changes: 9 additions & 3 deletions src/runtime/Types/ModuleObject.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ namespace Python.Runtime
[Serializable]
internal class ModuleObject : ExtensionType
{
private readonly ConcurrentDictionary<string, PyObject> cache = new();
[NonSerialized]
private ConcurrentDictionary<string, PyObject> cache = new();

internal string moduleName;
internal PyDict dict;
protected string _namespace;
private readonly PyList __all__ = new ();
private readonly ConcurrentDictionary<string, byte> allNames = new();
[NonSerialized]
private ConcurrentDictionary<string, byte> allNames = new();

// Attributes to be set on the module according to PEP302 and 451
// by the import machinery.
Expand Down Expand Up @@ -378,7 +380,8 @@ public static int tp_traverse(BorrowedReference ob, IntPtr visit, IntPtr arg)

protected override Dictionary<string, object?>? OnSave(BorrowedReference ob)
{
var context = base.OnSave(ob);
var context = base.OnSave(ob) ?? new();
context[nameof(allNames)] = allNames.Keys.ToArray();
System.Diagnostics.Debug.Assert(dict == GetObjectDict(ob));
// destroy the cache(s)
foreach (var pair in cache)
Expand All @@ -402,6 +405,9 @@ public static int tp_traverse(BorrowedReference ob, IntPtr visit, IntPtr arg)

protected override void OnLoad(BorrowedReference ob, Dictionary<string, object?>? context)
{
cache = new();
allNames = new(((string[])context![nameof(allNames)]!)
.Select(name => new KeyValuePair<string, byte>(name, 0)));
base.OnLoad(ob, context);
SetObjectDict(ob, new NewReference(dict).Steal());
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL