@@ -16,7 +16,7 @@ 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
{
@@ -27,12 +27,12 @@ public static class RuntimeData
27
27
}
28
28
}
29
29
catch { }
30
- return new NoopFormatter ( ) ;
30
+ return null ;
31
31
} ;
32
32
33
- private static Func < IFormatter > _formatterFactory { get ; set ; } = DefaultFormatterFactory ;
33
+ private static Func < IFormatter ? > _formatterFactory { get ; set ; } = DefaultFormatterFactory ;
34
34
35
- public static Func < IFormatter > FormatterFactory
35
+ public static Func < IFormatter ? > FormatterFactory
36
36
{
37
37
get => _formatterFactory ;
38
38
set
@@ -84,32 +84,37 @@ static void ClearCLRData ()
84
84
85
85
internal static void Stash ( )
86
86
{
87
- var runtimeStorage = new PythonNetState
88
- {
89
- Metatype = MetaType . SaveRuntimeData ( ) ,
90
- ImportHookState = ImportHook . SaveRuntimeData ( ) ,
8000
code>
91
- Types = TypeManager . SaveRuntimeData ( ) ,
92
- Classes = ClassManager . SaveRuntimeData ( ) ,
93
- SharedObjects = SaveRuntimeDataObjects ( ) ,
94
- } ;
95
-
96
- IFormatter formatter = CreateFormatter ( ) ;
97
- var ms = new MemoryStream ( ) ;
98
- formatter . Serialize ( ms , runtimeStorage ) ;
99
-
100
- Debug . Assert ( ms . Length <= int . MaxValue ) ;
101
- byte [ ] data = ms . GetBuffer ( ) ;
102
- // TODO: use buffer api instead
103
- IntPtr mem = PyMem_Malloc ( ms . Length + IntPtr . Size ) ;
104
- Marshal . WriteIntPtr ( mem , ( IntPtr ) ms . Length ) ;
105
- Marshal . Copy ( data , 0 , mem + IntPtr . Size , ( int ) ms . Length ) ;
106
-
107
87
ClearCLRData ( ) ;
108
88
109
- using NewReference capsule = PyCapsule_New ( mem , IntPtr . Zero , IntPtr . Zero ) ;
110
- int res = PySys_SetObject ( "clr_data" , capsule . BorrowOrThrow ( ) ) ;
111
- PythonException . ThrowIfIsNotZero ( res ) ;
112
- PostStashHook ? . Invoke ( ) ;
89
+ IFormatter ? formatter = CreateFormatter ( ) ;
90
+
91
+ if ( formatter != null )
92
+ {
93
+ var runtimeStorage = new PythonNetState
94
+ {
95
+ Metatype = MetaType . SaveRuntimeData ( ) ,
96
+ ImportHookState = ImportHook . SaveRuntimeData ( ) ,
97
+ Types = TypeManager . SaveRuntimeData ( ) ,
98
+ Classes = ClassManager . SaveRuntimeData ( ) ,
99
+ SharedObjects = SaveRuntimeDataObjects ( ) ,
100
+ } ;
101
+
102
+ var ms = new MemoryStream ( ) ;
103
+ formatter . Serialize ( ms , runtimeStorage ) ;
104
+
105
+ Debug . Assert ( ms . Length <= int . MaxValue ) ;
106
+ byte [ ] data = ms . GetBuffer ( ) ;
107
+ // TODO: use buffer api instead
108
+ IntPtr mem = PyMem_Malloc ( ms . Length + IntPtr . Size ) ;
109
+ Marshal . WriteIntPtr ( mem , ( IntPtr ) ms . Length ) ;
110
+ Marshal . Copy ( data , 0 , mem + IntPtr . Size , ( int ) ms . Length ) ;
111
+
112
+ using NewReference capsule = PyCapsule_New ( mem , IntPtr . Zero , IntPtr . Zero ) ;
113
+ int res = PySys_SetObject ( "clr_data" , capsule . BorrowOrThrow ( ) ) ;
114
+ PythonException . ThrowIfIsNotZero ( res ) ;
115
+
116
+ PostStashHook ? . Invoke ( ) ;
117
+ }
113
118
}
114
119
115
120
internal static void RestoreRuntimeData ( )
@@ -126,28 +131,32 @@ internal static void RestoreRuntimeData()
126
131
127
132
private static void RestoreRuntimeDataImpl ( )
128
133
{
129
- PreRestoreHook ? . Invoke ( ) ;
130
- BorrowedReference capsule = PySys_GetObject ( "clr_data" ) ;
131
- if ( capsule . IsNull )
134
+ IFormatter ? formatter = CreateFormatter ( ) ;
135
+
136
+ if ( formatter != null )
132
137
{
133
- return ;
134
- }
135
- IntPtr mem = PyCapsule_GetPointer ( capsule , IntPtr . Zero ) ;
136
- int length = ( int ) Marshal . ReadIntPtr ( mem ) ;
137
- byte [ ] data = new byte [ length ] ;
138
- Marshal . Copy ( mem + IntPtr . Size , data , 0 , length ) ;
139
- var ms = new MemoryStream ( data ) ;
140
- var formatter = CreateFormatter ( ) ;
141
- var storage = ( PythonNetState ) formatter . Deserialize ( ms ) ;
138
+ PreRestoreHook ? . Invoke ( ) ;
139
+ BorrowedReference capsule = PySys_GetObject ( "clr_data" ) ;
140
+ if ( capsule . IsNull )
141
+ {
142
+ return ;
143
+ }
144
+ IntPtr mem = PyCapsule_GetPointer ( capsule , IntPtr . Zero ) ;
145
+ int length = ( int ) Marshal . ReadIntPtr ( mem ) ;
146
+ byte [ ] data = new byte [ length ] ;
147
+ Marshal . Copy ( mem + IntPtr . Size , data , 0 , length ) ;
148
+ var ms = new MemoryStream ( data ) ;
149
+ var storage = ( PythonNetState ) formatter . Deserialize ( ms ) ;
142
150
143
- PyCLRMetaType = MetaType . RestoreRuntimeData ( storage . Metatype ) ;
151
+ PyCLRMetaType = MetaType . RestoreRuntimeData ( storage . Metatype ) ;
144
152
145
- TypeManager . RestoreRuntimeData ( storage . Types ) ;
146
- ClassManager . RestoreRuntimeData ( storage . Classes ) ;
153
+ TypeManager . RestoreRuntimeData ( storage . Types ) ;
154
+ ClassManager . RestoreRuntimeData ( storage . Classes ) ;
147
155
148
- RestoreRuntimeDataObjects ( storage . SharedObjects ) ;
156
+ RestoreRuntimeDataObjects ( storage . SharedObjects ) ;
149
157
150
- ImportHook . RestoreRuntimeData ( storage . ImportHookState ) ;
158
+ ImportHook . RestoreRuntimeData ( storage . ImportHookState ) ;
159
+ }
151
160
}
152
161
153
162
public static bool HasStashData ( )
@@ -375,9 +384,8 @@ public static MemoryStream GetSerializationData(string key)
375
384
return new MemoryStream ( buffer , writable : false ) ;
376
385
}
377
386
378
- internal static IFormatter CreateFormatter ( )
387
+ internal static IFormatter ? CreateFormatter ( )
379
388
{
380
-
381
389
if ( FormatterType != null )
382
390
{
383
391
return ( IFormatter ) Activator . CreateInstance ( FormatterType ) ;
0 commit comments