Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/runtime/StateSerialization/RuntimeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,36 @@ namespace Python.Runtime
{
public static class RuntimeData
{
private static Type? _formatterType;

public readonly static Func<IFormatter> DefaultFormatterFactory = () =>
{
try
{
var res = new BinaryFormatter();
res.Serialize(new MemoryStream(), 1); // test if BinaryFormatter is usable
return res;
}
catch
{
return new NoopFormatter();
}
};

private static Func<IFormatter> _formatterFactory { get; set; } = DefaultFormatterFactory;

public static Func<IFormatter> FormatterFactory
{
get => _formatterFactory;
set
{
if (value == null)
throw new ArgumentNullException(nameof(value));

_formatterFactory = value;
}
}

private static Type? _formatterType = null;
public static Type? FormatterType
{
get => _formatterType;
Expand Down Expand Up @@ -206,7 +235,7 @@ internal static IFormatter CreateFormatter()
{
return FormatterType != null ?
(IFormatter)Activator.CreateInstance(FormatterType)
: new BinaryFormatter();
: FormatterFactory();
}
}
}
Expand Down
Loading