33using System . Linq ;
44using System . Runtime . CompilerServices ;
55using System . Runtime . ExceptionServices ;
6+ using System . Runtime . Serialization ;
7+ using System . Security . Permissions ;
68using System . Text ;
79
810using 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