1
1
using System ;
2
- using System . Collections ;
3
2
using System . Collections . Generic ;
4
- using System . Collections . ObjectModel ;
5
3
using System . Diagnostics ;
6
4
using System . IO ;
7
5
using System . Linq ;
@@ -18,21 +16,28 @@ namespace Python.Runtime
18
16
public static class RuntimeData
19
17
{
20
18
21
- private readonly static Func < IFormatter > DefaultFormatter = ( ) => new BinaryFormatter ( ) ;
22
- private static Func < IFormatter > ? _formatterFactory { get ; set ; } = null ;
23
-
24
- public static Func < IFormatter > FormatterFactory
19
+ public readonly static Func < IFormatter > DefaultFormatterFactory = ( ) =>
25
20
{
26
- get
21
+ try
27
22
{
28
- if ( _formatterFactory is null )
29
- {
30
- return DefaultFormatter ;
31
- }
32
- return _formatterFactory ;
23
+ return new BinaryFormatter ( ) ;
33
24
}
25
+ catch
26
+ {
27
+ return new NoopFormatter ( ) ;
28
+ }
29
+ } ;
30
+
31
+ private static Func < IFormatter > _formatterFactory { get ; set ; } = DefaultFormatterFactory ;
32
+
33
+ public static Func < IFormatter > FormatterFactory
34
+ {
35
+ get => _formatterFactory ;
34
36
set
35
37
{
38
+ if ( value == null )
39
+ throw new ArgumentNullExcep
8000
tion ( nameof ( value ) ) ;
40
+
36
41
_formatterFactory = value ;
37
42
}
38
43
}
@@ -302,8 +307,8 @@ public static void FreeSerializationData(string key)
302
307
}
303
308
304
309
/// <summary>
305
- /// Stores the data in the <paramref name="stream"/> argument in a Python capsule and stores
306
- /// the capsule on the `sys` module object with the name <paramref name="key"/>.
310
+ /// Stores the data in the <paramref name="stream"/> argument in a Python capsule and stores
311
+ /// the capsule on the `sys` module object with the name <paramref name="key"/>.
307
312
/// </summary>
308
313
/// <remarks>
309
314
/// No checks on pre-existing names on the `sys` module object are made.
@@ -320,10 +325,10 @@ public static void StashSerializationData(string key, MemoryStream stream)
320
325
321
326
try
322
327
{
323
- using NewReference capsule = PyCapsule_New ( mem , IntPtr . Zero , IntPtr . Zero ) ;
324
- int res = PySys_SetObject ( key , capsule . BorrowOrThrow ( ) ) ;
325
- PythonException . ThrowIfIsNotZero ( res ) ;
326
- }
328
+ using NewReference capsule = PyCapsule_New ( mem , IntPtr . Zero , IntPtr . Zero ) ;
329
+ int res = PySys_SetObject ( key , capsule . BorrowOrThrow ( ) ) ;
330
+ PythonException . ThrowIfIsNotZero ( res ) ;
331
+ }
327
332
catch
328
333
{
329
334
Marshal . FreeHGlobal ( mem ) ;
@@ -333,12 +338,12 @@ public static void StashSerializationData(string key, MemoryStream stream)
333
338
334
339
static byte [ ] emptyBuffer = new byte [ 0 ] ;
335
340
/// <summary>
336
- /// Retreives the previously stored data on a Python capsule.
341
+ /// Retreives the previously stored data on a Python capsule.
337
342
/// Throws if the object corresponding to the <paramref name="key"/> parameter
338
343
/// on the `sys` module object is not a capsule.
339
344
/// </summary>
340
345
/// <param name="key">The name given to the capsule on the `sys` module object</param>
341
- /// <returns>A MemoryStream containing the previously saved serialization data.
346
+ /// <returns>A MemoryStream containing the previously saved serialization data.
342
347
/// The stream is empty if no name matches the key. </returns>
343
348
public static MemoryStream GetSerializationData ( string key )
344
349
{
@@ -351,7 +356,7 @@ public static MemoryStream GetSerializationData(string key)
351
356
var ptr = PyCapsule_GetPointer ( capsule , IntPtr . Zero ) ;
352
357
if ( ptr == IntPtr . Zero )
353
358
{
354
- // The PyCapsule API returns NULL on error; NULL cannot be stored
359
+ // The PyCapsule API returns NULL on error; NULL cannot be stored
355
360
// as a capsule's value
356
361
PythonException . ThrowIfIsNull ( null ) ;
357
362
}
0 commit comments