Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3cd685e

Browse files
committed
Use the BinaryFormatter only on .NET FW and Mono
1 parent dfd746b commit 3cd685e

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

src/embed_tests/StateSerialization/MethodSerialization.cs

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ static T SerializationRoundtrip<T>(T item)
3333
{
3434
using var buf = new MemoryStream();
3535
var formatter = RuntimeData.CreateFormatter();
36+
37+
if (formatter == null)
38+
Assert.Inconclusive("Failed to create formatter for state serialization");
39+
3640
formatter.Serialize(buf, item);
3741
buf.Position = 0;
3842
return (T)formatter.Deserialize(buf);

src/runtime/StateSerialization/NoopFormatter.cs

-14
This file was deleted.

src/runtime/StateSerialization/RuntimeData.cs

+26-14
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,23 @@ namespace Python.Runtime
1616
public static class RuntimeData
1717
{
1818

19-
public readonly static Func<IFormatter> DefaultFormatterFactory = () =>
19+
public readonly static Func<IFormatter?> DefaultFormatterFactory = () =>
2020
{
2121
try
2222
{
23-
return new BinaryFormatter();
24-
}
25-
catch
26-
{
27-
return new NoopFormatter();
23+
var fw = RuntimeInformation.FrameworkDescription;
24+
if (fw.StartsWith(".NET Framework") || fw.StartsWith("Mono"))
25+
{
26+
return new BinaryFormatter();
27+
}
2828
}
29+
catch {}
30+
return null;
2931
};
3032

31-
private static Func<IFormatter> _formatterFactory { get; set; } = DefaultFormatterFactory;
33+
private static Func<IFormatter?> _formatterFactory { get; set; } = DefaultFormatterFactory;
3234

33-
public static Func<IFormatter> FormatterFactory
35+
public static Func<IFormatter?> FormatterFactory
3436
{
3537
get => _formatterFactory;
3638
set
@@ -82,6 +84,14 @@ static void ClearCLRData ()
8284

8385
internal static void Stash()
8486
{
87+
ClearCLRData();
88+
89+
IFormatter? formatter = CreateFormatter();
90+
91+
if (formatter == null)
92+
// No formatter defined, exit early
93+
return;
94+
8595
var runtimeStorage = new PythonNetState
8696
{
8797
Metatype = MetaType.SaveRuntimeData(),
@@ -91,7 +101,6 @@ internal static void Stash()
91101
SharedObjects = SaveRuntimeDataObjects(),
92102
};
93103

94-
IFormatter formatter = CreateFormatter();
95104
var ms = new MemoryStream();
96105
formatter.Serialize(ms, runtimeStorage);
97106

@@ -102,11 +111,10 @@ internal static void Stash()
102111
Marshal.WriteIntPtr(mem, (IntPtr)ms.Length);
103112
Marshal.Copy(data, 0, mem + IntPtr.Size, (int)ms.Length);
104113

105-
ClearCLRData();
106-
107114
using NewReference capsule = PyCapsule_New(mem, IntPtr.Zero, IntPtr.Zero);
108115
int res = PySys_SetObject("clr_data", capsule.BorrowOrThrow());
109116
PythonException.ThrowIfIsNotZero(res);
117+
110118
PostStashHook?.Invoke();
111119
}
112120

@@ -124,6 +132,12 @@ internal static void RestoreRuntimeData()
124132

125133
private static void RestoreRuntimeDataImpl()
126134
{
135+
IFormatter? formatter = CreateFormatter();
136+
137+
if (formatter == null)
138+
// No formatter defined, exit early
139+
return;
140+
127141
PreRestoreHook?.Invoke();
128142
BorrowedReference capsule = PySys_GetObject("clr_data");
129143
if (capsule.IsNull)
@@ -135,7 +149,6 @@ private static void RestoreRuntimeDataImpl()
135149
byte[] data = new byte[length];
136150
Marshal.Copy(mem + IntPtr.Size, data, 0, length);
137151
var ms = new MemoryStream(data);
138-
var formatter = CreateFormatter();
139152
var storage = (PythonNetState)formatter.Deserialize(ms);
140153

141154
PyCLRMetaType = MetaType.RestoreRuntimeData(storage.Metatype);
@@ -373,9 +386,8 @@ public static MemoryStream GetSerializationData(string key)
373386
return new MemoryStream(buffer, writable:false);
374387
}
375388

376-
internal static IFormatter CreateFormatter()
389+
internal static IFormatter? CreateFormatter()
377390
{
378-
379391
if (FormatterType != null)
380392
{
381393
return (IFormatter)Activator.CreateInstance(FormatterType);

0 commit comments

Comments
 (0)