Skip to content

Commit 50752e8

Browse files
authored
Non-breaking workaround for BinaryFormatter deprecation, issue #2469
1 parent a21c797 commit 50752e8

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/runtime/StateSerialization/NoopFormatter.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
namespace Python.Runtime;
66

77
public class NoopFormatter : IFormatter {
8-
public object Deserialize(Stream s) => throw new NotImplementedException();
8+
9+
public object Deserialize(Stream s) => throw new NotImplementedException(
10+
"Cannot deserialize using 'NoopFormatter', implement 'Python.Runtime.RuntimeData.FormatterType' if needed"
11+
);
12+
913
public void Serialize(Stream s, object o) {}
1014

1115
public SerializationBinder? Binder { get; set; }

src/runtime/StateSerialization/RuntimeData.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,43 @@ namespace Python.Runtime
1515
{
1616
public static class RuntimeData
1717
{
18+
//TODO: put it in a utility class instead of inside the RuntimeData Class.
19+
private static Version? GetNetVersion(string verToCheck)
20+
{
21+
// Examples:
22+
// ".NET 8.0.0"
23+
// ".NET 9.0.0"
24+
// ".NET Framework 4.8.1"
25+
// ".NET Core 3.1.32"
26+
string desc = RuntimeInformation.FrameworkDescription;
27+
if (desc.StartsWith(verToCheck, StringComparison.OrdinalIgnoreCase))
28+
{
29+
var verStr = desc.Split(' ').Last();
30+
if (Version.TryParse(verStr, out var ver))
31+
return ver;
32+
}
33+
return null;
34+
}
1835

1936
public readonly static Func<IFormatter> DefaultFormatterFactory = () =>
2037
{
21-
try
38+
//The issue here is that just instancing BynaryFormatter doesn't solve the problem.
39+
//you need to use it to generate the exception.
40+
/*try
2241
{
2342
return new BinaryFormatter();
2443
}
2544
catch
2645
{
2746
return new NoopFormatter();
47+
}*/
48+
var ver = GetNetVersion(".NET");
49+
if(ver != null && ver.Major >= 9) {
50+
return new NoopFormatter();
51+
} else {
52+
return new BinaryFormatter();
2853
}
29-
};
54+
}
3055

3156
private static Func<IFormatter> _formatterFactory { get; set; } = DefaultFormatterFactory;
3257

0 commit comments

Comments
 (0)