Skip publishing sys.clr_data when BinaryFormatter is unavailable (#2282) by AtwoodTM · Pull Request #2749 · pythonnet/pythonnet
Repository navigation
Skip publishing sys.clr_data when BinaryFormatter is unavailable (#2282) - #2749
Closed
AtwoodTM wants to merge 2 commits into pythonnet:master from AtwoodTM:fix/develop/2282-noop-stash
Closed
Skip publishing sys.clr_data when BinaryFormatter is unavailable (#2282) #2749 AtwoodTM wants to merge 2 commits into pythonnet:master from AtwoodTM:fix/develop/2282-noop-stash
Conversation
AtwoodTM commented Oct 6, 2026
Copy link Copy Markdown
What does this implement/fix? Explain your changes.
When BinaryFormatter cannot run, RuntimeData.Stash falls back to NoopFormatter and still published a zero-length sys.clr_data capsule. NoopFormatter.Serialize writes nothing. The next PythonEngine.Initialize saw that capsule and called Deserialize, which throws.
Stash now returns before building or publishing the capsule when the formatter is a NoopFormatter. An existing capsule is cleared so a later Initialize builds a fresh runtime instead of restoring an empty one. Runtimes where BinaryFormatter still works, including .NET Framework, keep the real stash.
Does this close any currently open issues?
Any other comments?
#2639 already catches the BinaryFormatter failure and returns NoopFormatter, so Shutdown itself no longer throws NotSupportedException. This change stops that fallback from leaving a capsule the next Initialize cannot read.
The new embed test calls Py_Finalize after the assertion, then Initialize. A second Shutdown in the same process deallocates types from the previous runtime and hits MetaType.tp_dealloc in a debug build. GlobalTestsSetup would do that second shutdown.
Local run, with PYTHONNET_PYDLL set:
dotnet test src/embed_tests/Python.EmbeddingTest.csproj -f net10.0
242 passed, 5 skipped, 0 failed. net472 was not run here. This machine has no mono.
Checklist
- Make sure to include one or more tests for your change
- If an enhancement PR, please create docs and at best an example
- Ensure you have signed the .NET Foundation CLA
- Add yourself to
AUTHORS - Updated the
CHANGELOG
Skip publishing sys.clr_data when BinaryFormatter is unavailable (pyt...
0958ef5
...honnet#2282) NoopFormatter serializes nothing. An empty capsule makes the next Initialize call Deserialize and throw. Leave sys.clr_data unset.
Leave AUTHORS.md unchanged
06f9c1d
A single fix does not need a new credits entry.
filmor commented Oct 6, 2026
Copy link Copy Markdown
Member
|
As it stands, we can't safely re- |
AtwoodTM commented Oct 6, 2026
Copy link Copy Markdown
Author
|
You're right, thanks for taking a look. I went back and tested master on net8.0, net9.0 and net10.0. Side question: is there anything left on #2282 , or can that one be closed too? It still shows up as open when people search for the BinaryFormatter error. |