Skip to content

Commit bf3d9f8

Browse files
committed
Add basic reload shutdown mode
1 parent 631bb43 commit bf3d9f8

26 files changed

+387
-261
lines changed

src/embed_tests/TestDomainReload.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public static void RunPython() {
8585
AppDomain.CurrentDomain.DomainUnload += OnDomainUnload;
8686
string name = AppDomain.CurrentDomain.FriendlyName;
8787
Console.WriteLine(string.Format(""[{0} in .NET] In PythonRunner.RunPython"", name));
88-
PythonEngine.Initialize(softShutdown: true);
88+
PythonEngine.Initialize(mode: ShutdownMode.Reload);
8989
using (Py.GIL()) {
9090
try {
9191
var pyScript = string.Format(""import clr\n""

src/runtime/classbase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace Python.Runtime
1313
/// concrete subclasses provide slot implementations appropriate for
1414
/// each variety of reflected type.
1515
/// </summary>
16+
[Serializable]
1617
internal class ClassBase : ManagedType
1718
{
1819
internal Indexer indexer;

src/runtime/classmanager.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Python.Runtime
1919
internal class ClassManager
2020
{
2121
private static Dictionary<Type, ClassBase> cache;
22-
private static Type dtype;
22+
private static readonly Type dtype;
2323

2424
private ClassManager()
2525
{
@@ -81,6 +81,17 @@ private static int OnVisit(IntPtr ob, IntPtr arg)
8181
return 0;
8282
}
8383

84+
85+
internal static void StashPush(Stack stack)
86+
{
87+
stack.Push(cache);
88+
}
89+
90+
internal static void StashPop(Stack stack)
91+
{
92+
cache = (Dictionary<Type, ClassBase>)stack.Pop();
93+
}
94+
8495
/// <summary>
8596
/// Return the ClassBase-derived instance that implements a particular
8697
/// reflected managed type, creating it if it doesn't yet exist.
@@ -239,7 +250,7 @@ private static void InitClassBase(Type type, ClassBase impl)
239250

240251
private static ClassInfo GetClassInfo(Type type)
241252
{
242-
var ci = new ClassInfo(type);
253+
var ci = new ClassInfo();
243254
var methods = new Hashtable();
244255
ArrayList list;
245256
MethodInfo meth;
@@ -443,7 +454,7 @@ internal class ClassInfo
443454
public Indexer indexer;
444455
public Hashtable members;
445456

446-
internal ClassInfo(Type t)
457+
internal ClassInfo()
447458
{
448459
members = new Hashtable();
449460
indexer = null;

src/runtime/classobject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Python.Runtime
99
/// Python type objects. Each of those type objects is associated with
1010
/// an instance of ClassObject, which provides its implementation.
1111
/// </summary>
12+
[Serializable]
1213
internal class ClassObject : ClassBase
1314
{
1415
internal ConstructorBinder binder;

src/runtime/clrobject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ internal class CLRObject : ManagedType
99

1010
internal CLRObject(object ob, IntPtr tp)
1111
{
12+
System.Diagnostics.Debug.Assert(tp != IntPtr.Zero);
1213
IntPtr py = Runtime.PyType_GenericAlloc(tp, 0);
1314

1415
long flags = Util.ReadCLong(tp, TypeOffset.tp_flags);

src/runtime/constructorbinder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Python.Runtime
1010
/// standard MethodBinder because of a difference in invoking constructors
1111
/// using reflection (which is seems to be a CLR bug).
1212
/// </summary>
13+
[Serializable]
1314
internal class ConstructorBinder : MethodBinder
1415
{
1516
private Type _containingType;

src/runtime/constructorbinding.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ namespace Python.Runtime
1919
/// and creating the BoundContructor object which contains ContructorInfo object.
2020
/// 3) In tp_call, if ctorInfo is not null, ctorBinder.InvokeRaw() is called.
2121
/// </remarks>
22+
[Serializable]
2223
internal class ConstructorBinding : ExtensionType
2324
{
2425
private Type type; // The managed Type being wrapped in a ClassObject
2526
private IntPtr pyTypeHndl; // The python type tells GetInstHandle which Type to create.
2627
private ConstructorBinder ctorBinder;
28+
29+
[NonSerialized]
2730
private IntPtr repr;
2831

2932
public ConstructorBinding(Type type, IntPtr pyTypeHndl, ConstructorBinder ctorBinder)
@@ -173,6 +176,7 @@ public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg)
173176
/// An earlier implementation hung the __call__ on the ContructorBinding class and
174177
/// returned an Incref()ed self.pyHandle from the __get__ function.
175178
/// </remarks>
179+
[Serializable]
176180
internal class BoundContructor : ExtensionType
177181
{
178182
private Type type; // The managed Type being wrapped in a ClassObject

src/runtime/delegateobject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Python.Runtime
88
/// Each of those type objects is associated an instance of this class,
99
/// which provides its implementation.
1010
/// </summary>
11+
[Serializable]
1112
internal class DelegateObject : ClassBase
1213
{
1314
private MethodBinder binder;

src/runtime/eventbinding.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Python.Runtime
55
/// <summary>
66
/// Implements a Python event binding type, similar to a method binding.
77
/// </summary>
8+
[Serializable]
89
internal class EventBinding : ExtensionType
910
{
1011
private EventObject e;
@@ -127,5 +128,11 @@ public static int tp_clear(IntPtr ob)
127128
Runtime.Py_CLEAR(ref self.target);
128129
return 0;
129130
}
131+
132+
protected override void OnSave()
133+
{
134+
base.OnSave();
135+
Runtime.XIncref(target);
136+
}
130137
}
131138
}

src/runtime/eventobject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Python.Runtime
77
/// <summary>
88
/// Implements a Python descriptor type that provides access to CLR events.
99
/// </summary>
10+
[Serializable]
1011
internal class EventObject : ExtensionType
1112
{
1213
internal string name;

0 commit comments

Comments
 (0)