8000 Expose serialization api by BadSingleton · Pull Request #2336 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Expose serialization api #2336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 10, 2024
Merged
Changes from 1 commit
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
Next Next commit
Expose an API for users to specify their own formatter
Adds post-serialization and pre-deserialization hooks for additional
customization.
  • Loading branch information
BadSingleton committed Feb 6, 2024
commit d7f31bfa430ed725bca554f21d83f5594e6650a2
31 changes: 20 additions & 11 deletions src/runtime/StateSerialization/RuntimeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,29 @@ namespace Python.Runtime
{
public static class RuntimeData
{
private static Type? _formatterType;
public static Type? FormatterType

public delegate IFormatter FormatterFactoryDelegate();
private readonly static FormatterFactoryDelegate DefaultFormatter = () => new BinaryFormatter();
private static FormatterFactoryDelegate? _formatter { get; set; } = null;

public static FormatterFactoryDelegate Formatter
{
get => _formatterType;
set
get
{
if (!typeof(IFormatter).IsAssignableFrom(value))
if (_formatter is null)
{
throw new ArgumentException("Not a type implemented IFormatter");
return DefaultFormatter;
}
_formatterType = value;
return _formatter;
}
set
{
_formatter = value;
}
}

public delegate void SerializationHookDelegate();
public static SerializationHookDelegate? PostStashHook {get; set;} = null;
public static SerializationHookDelegate? PreRestoreHook {get; set;} = null;
public static ICLRObjectStorer? WrappersStorer { get; set; }

/// <summary>
Expand Down Expand Up @@ -74,6 +83,7 @@ internal static void Stash()
using NewReference capsule = PyCapsule_New(mem, IntPtr.Zero, IntPtr.Zero);
int res = PySys_SetObject("clr_data", capsule.BorrowOrThrow());
PythonException.ThrowIfIsNotZero(res);
PostStashHook?.Invoke();
}

internal static void RestoreRuntimeData()
Expand All @@ -90,6 +100,7 @@ internal static void RestoreRuntimeData()

private static void RestoreRuntimeDataImpl()
{
PreRestoreHook?.Invoke();
BorrowedReference capsule = PySys_GetObject("clr_data");
if (capsule.IsNull)
{
Expand Down Expand Up @@ -252,9 +263,7 @@ private static void RestoreRuntimeDataObjects(SharedObjectsState storage)

internal static IFormatter CreateFormatter()
{
return FormatterType != null ?
(IFormatter)Activator.CreateInstance(FormatterType)
: new BinaryFormatter();
return Formatter();
}
}
}
0