Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 03f32cb

Browse files
committed
PythonException is serializable
1 parent a610aa3 commit 03f32cb

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

src/runtime/pythonexception.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Linq;
44
using System.Runtime.CompilerServices;
55
using System.Runtime.ExceptionServices;
6+
using System.Runtime.Serialization;
7+
using System.Security.Permissions;
68
using System.Text;
79

810
using Python.Runtime.Native;
@@ -13,6 +15,7 @@ namespace Python.Runtime
1315
/// Provides a managed interface to exceptions thrown by the Python
1416
/// runtime.
1517
/// </summary>
18+
[Serializable]
1619
public class PythonException : System.Exception
1720
{
1821
public PythonException(PyType type, PyObject? value, PyObject? traceback,
@@ -395,6 +398,32 @@ public PythonException Clone()
395398
=> new PythonException(type: Type, value: Value, traceback: Traceback,
396399
Message, InnerException);
397400

401+
#region Serializable
402+
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
403+
protected PythonException(SerializationInfo info, StreamingContext context)
404+
: base(info, context)
405+
{
406+
Type = (PyType)info.GetValue(nameof(Type), typeof(PyType));
407+
Value = (PyObject)info.GetValue(nameof(Value), typeof(PyObject));
408+
Traceback = (PyObject)info.GetValue(nameof(Traceback), typeof(PyObject));
409+
}
410+
411+
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
412+
public override void GetObjectData(SerializationInfo info, StreamingContext context)
413+
{
414+
if (info == null)
415+
{
416+
throw new ArgumentNullException(nameof(info));
417+
}
418+
419+
base.GetObjectData(info, context);
420+
421+
info.AddValue(nameof(Type), Type);
422+
info.AddValue(nameof(Value), Value);
423+
info.AddValue(nameof(Traceback), Traceback);
424+
}
425+
#endregion
426+
398427
internal bool Is(BorrowedReference type)
399428
{
400429
return Runtime.PyErr_GivenExceptionMatches(

0 commit comments

Comments
 (0)