8000 Add NoopFormatter and fall back to it if BinaryFormatter is not avail… · pythonnet/pythonnet@a058900 · GitHub
[go: up one dir, main page]

Skip to content

Commit a058900

Browse files
committed
Add NoopFormatter and fall back to it if BinaryFormatter is not available
1 parent 02158f3 commit a058900

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.IO;
3+
using System.Runtime.Serialization;
4+
5+
namespace Python.Runtime;
6+
7+
public class NoopFormatter : IFormatter {
8+
public object Deserialize(Stream s) => throw new NotImplementedException();
9+
public void Serialize(Stream s, object o) {}
10+
11+
public SerializationBinder? Binder { get; set; }
12+
public StreamingContext Context { get; set; }
13+
public ISurrogateSelector? SurrogateSelector { get; set; }
14+
}

src/runtime/StateSerialization/RuntimeData.cs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
4-
using System.Collections.ObjectModel;
53
using System.Diagnostics;
64
using System.IO;
75
using System.Linq;
@@ -18,21 +16,28 @@ namespace Python.Runtime
1816
public static class RuntimeData
1917
{
2018

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 = () =>
2520
{
26-
get
21+
try
2722
{
28-
if (_formatterFactory is null)
29-
{
30-
return DefaultFormatter;
31-
}
32-
return _formatterFactory;
23+
return new BinaryFormatter();
3324
}
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;
3436
set
3537
{
38+
if (value == null)
39+
throw new ArgumentNullExcep 8000 tion(nameof(value));
40+
3641
_formatterFactory = value;
3742
}
3843
}
@@ -302,8 +307,8 @@ public static void FreeSerializationData(string key)
302307
}
303308

304309
/// <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"/>.
307312
/// </summary>
308313
/// <remarks>
309314
/// 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)
320325

321326
try
322327
{
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+
}
327332
catch
328333
{
329334
Marshal.FreeHGlobal(mem);
@@ -333,12 +338,12 @@ public static void StashSerializationData(string key, MemoryStream stream)
333338

334339
static byte[] emptyBuffer = new byte[0];
335340
/// <summary>
336-
/// Retreives the previously stored data on a Python capsule.
341+
/// Retreives the previously stored data on a Python capsule.
337342
/// Throws if the object corresponding to the <paramref name="key"/> parameter
338343
/// on the `sys` module object is not a capsule.
339344
/// </summary>
340345
/// <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.
342347
/// The stream is empty if no name matches the key. </returns>
343348
public static MemoryStream GetSerializationData(string key)
344349
{
@@ -351,7 +356,7 @@ public static MemoryStream GetSerializationData(string key)
351356
var ptr = PyCapsule_GetPointer(capsule, IntPtr.Zero);
352357
if (ptr == IntPtr.Zero)
353358
{
354-
// The PyCapsule API returns NULL on error; NULL cannot be stored
359+
// The PyCapsule API returns NULL on error; NULL cannot be stored
355360
// as a capsule's value
356361
PythonException.ThrowIfIsNull(null);
357362
}

0 commit comments

Comments
 (0)
0