@@ -16,21 +16,23 @@ namespace Python.Runtime
16
16
public static class RuntimeData
17
17
{
18
18
19
- public readonly static Func < IFormatter > DefaultFormatterFactory = ( ) =>
19
+ public readonly static Func < IFormatter ? > DefaultFormatterFactory = ( ) =>
20
20
{
21
21
try
22
22
{
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
+ }
28
28
}
29
+ catch { }
30
+ return null ;
29
31
} ;
30
32
31
- private static Func < IFormatter > _formatterFactory { get ; set ; } = DefaultFormatterFactory ;
33
+ private static Func < IFormatter ? > _formatterFactory { get ; set ; } = DefaultFormatterFactory ;
32
34
33
- public static Func < IFormatter > FormatterFactory
35
+ public static Func < IFormatter ? > FormatterFactory
34
36
{
35
37
get => _formatterFactory ;
36
38
set
@@ -82,6 +84,14 @@ static void ClearCLRData ()
82
84
83
85
internal static void Stash ( )
84
86
{
87
+ ClearCLRData ( ) ;
88
+
89
+ IFormatter ? formatter = CreateFormatter ( ) ;
90
+
91
+ if ( formatter == null )
92
+ // No formatter defined, exit early
93
+ return ;
94
+
85
95
var runtimeStorage = new PythonNetState
86
96
{
87
97
Metatype = MetaType . SaveRuntimeData ( ) ,
@@ -91,7 +101,6 @@ internal static void Stash()
91
101
SharedObjects = SaveRuntimeDataObjects ( ) ,
92
102
} ;
93
103
94
- IFormatter formatter = CreateFormatter ( ) ;
95
104
var ms = new MemoryStream ( ) ;
96
105
formatter . Serialize ( ms , runtimeStorage ) ;
97
106
@@ -102,11 +111,10 @@ internal static void Stash()
102
111
Marshal . WriteIntPtr ( mem , ( IntPtr ) ms . Length ) ;
103
112
Marshal . Copy ( data , 0 , mem + IntPtr . Size , ( int ) ms . Length ) ;
104
113
105
- ClearCLRData ( ) ;
106
-
107
114
using NewReference capsule = PyCapsule_New ( mem , IntPtr . Zero , IntPtr . Zero ) ;
108
115
int res = PySys_SetObject ( "clr_data" , capsule . BorrowOrThrow ( ) ) ;
109
116
PythonException . ThrowIfIsNotZero ( res ) ;
117
+
110
118
PostStashHook ? . Invoke ( ) ;
111
119
}
112
120
@@ -124,6 +132,12 @@ internal static void RestoreRuntimeData()
124
132
125
133
private static void RestoreRuntimeDataImpl ( )
126
134
{
135
+ IFormatter ? formatter = CreateFormatter ( ) ;
136
+
137
+ if ( formatter == null )
138
+ // No formatter defined, exit early
139
+ return ;
140
+
127
141
PreRestoreHook ? . Invoke ( ) ;
128
142
BorrowedReference capsule = PySys_GetObject ( "clr_data" ) ;
129
143
if ( capsule . IsNull )
@@ -135,7 +149,6 @@ private static void RestoreRuntimeDataImpl()
135
149
byte [ ] data = new byte [ length ] ;
136
150
Marshal . Copy ( mem + IntPtr . Size , data , 0 , length ) ;
137
151
var ms = new MemoryStream ( data ) ;
138
- var formatter = CreateFormatter ( ) ;
139
152
var storage = ( PythonNetState ) formatter . Deserialize ( ms ) ;
140
153
141
154
PyCLRMetaType = MetaType . RestoreRuntimeData ( storage . Metatype ) ;
@@ -373,9 +386,8 @@ public static MemoryStream GetSerializationData(string key)
373
386
return new MemoryStream ( buffer , writable : false ) ;
374
387
}
375
388
376
- internal static IFormatter CreateFormatter ( )
389
+ internal static IFormatter ? CreateFormatter ( )
377
390
{
378
-
379
391
if ( FormatterType != null )
380
392
{
381
393
return ( IFormatter ) Activator . CreateInstance ( FormatterType ) ;
0 commit comments