-
Notifications
You must be signed in to change notification settings - Fork 756
Add soft shutdown #958
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
Add soft shutdown #958
Changes from 1 commit
d1928dc
f000e08
f882400
b7715ee
41ac665
c6dae9e
4e19a4f
657452e
91f64b9
b07b844
7db724e
2a88be4
2940973
d108913
cc9e7e5
1cb8e8c
653a263
b3e889b
04d6dfb
5150e61
65e209e
cad95da
0dee5da
00a0b32
77da6df
2039e69
fe5050d
593fb00
1ce83fc
fba616a
bdc0f72
49d98e8
433d0f6
9b4864b
1b466df
49130c4
76ba510
1ff21ac
0b01378
631bb43
bf3d9f8
da97502
e8b3160
80d4fa0
9874cd1
8da561b
9499c64
431d644
2b84394
5ade069
8dabed7
f4bb77a
670bd74
3cb56f1
3c9a83c
992c469
924b217
df84e29
8b51621
97c8c2a
39f47c8
aa63f0b
5b2f3d4
35cbe55
f23cae6
183f9d8
9d57a82
08fad26
bcfdcc7
66ab719
1428af3
8e3c028
b387e9e
f85999e
cb65af3
498fc8c
cc2219e
8c8d66e
4f00165
39e20e3
da7c150
02b1ada
a8840b2
dec7a74
e877b33
6d738bf
ff5edc3
5ac75ba
65cb22e
73865d4
1a75f51
9b6d140
d9d5562
9b62a61
4f0420e
4ab9f1c
32bcb3a
0077ea8
38ea0b6
06a656e
09f8281
7ec9a6c
802a43a
0fdf969
3a8c72d
b52bc01
bfbf2c3
883c4ce
7e0d56d
82034dc
4ba50a7
1ecdce8
b35f441
ce8ee90
4c4bcb0
f575bd3
d1799aa
d2408b9
fd2b662
2b7bcac
0af3504
ba1df6e
639ba1f
7e5ab52
8075f48
eb8cb8a
308f0f2
1ae0bfe
07aefe6
5387b05
b203674
19d1379
25a4064
1272b89
8c133e3
d9b21a5
c8dee53
598cb77
83e8dd5
6db3181
e38a363
b409a89
f5c24b0
0d6c645
fa89b48
80a7644
12c0206
6a3cfc8
98da1fa
d7d44e8
6aa75c5
2343f89
cc6b8e4
d5fcfa4
fa47957
ff956e4
c7b134c
97e61a5
0b9d2c1
178cbc8
3a17f36
3203457
8d00e4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
reload
shutdown mode
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ namespace Python.Runtime | |
/// <summary> | ||
/// Implements a Python event binding type, similar to a method binding. | ||
/// </summary> | ||
[Serializable] | ||
internal class EventBinding : ExtensionType | ||
{ | ||
private EventObject e; | ||
|
@@ -127,5 +128,11 @@ public static int tp_clear(IntPtr ob) | |
Runtime.Py_CLEAR(ref self.target); | ||
return 0; | ||
} | ||
|
||
protected override void OnSave() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. < F438 /div>Do you need a matching There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, because |
||
{ | ||
base.OnSave(); | ||
Runtime.XIncref(target); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ namespace Python.Runtime | |
/// type object, such as the types that represent CLR methods, fields, | ||
/// etc. Instances implemented by this class do not support sub-typing. | ||
/// </summary> | ||
[Serializable] | ||
internal abstract class ExtensionType : ManagedType | ||
{ | ||
public ExtensionType() | ||
|
@@ -96,5 +97,19 @@ public static void tp_dealloc(IntPtr ob) | |
var self = (ExtensionType)GetManagedObject(ob); | ||
self.Dealloc(); | ||
} | ||
|
||
protected override void OnSave() | ||
{ | ||
base.OnSave(); | ||
Runtime.XIncref(pyHandle); | ||
} | ||
|
||
protected override void OnLoad() | ||
{ | ||
base.OnLoad(); | ||
GCHandle gc = AllocGCHandle(true); | ||
Marshal.WriteIntPtr(pyHandle, ObjectOffset.magic(tpHandle), (IntPtr)gc); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment explaining what this does, and why is it needed? |
||
Runtime.PyObject_GC_UnTrack(pyHandle); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment about why is it safe and necessary for Python GC to stop tracking this object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. huh... it already comment at
Since the mechanism of soft-shutdown, it turns back to GC, after the object reloaded, it just needs the same operation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It won't need a repeat, if you extract a method here :) |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -311,45 +311,45 @@ public static string GetSlotNameByOffset(int offset) | |
internal class TypeFlags | ||
{ | ||
#if PYTHON2 // these flags were removed in Python 3 | ||
public static int HaveGetCharBuffer = (1 << 0); | ||
public static int HaveSequenceIn = (1 << 1); | ||
public static int GC = 0; | ||
public static int HaveInPlaceOps = (1 << 3); | ||
public static int CheckTypes = (1 << 4); | ||
public static int HaveRichCompare = (1 << 5); | ||
public static int HaveWeakRefs = (1 << 6); | ||
public static int HaveIter = (1 << 7); | ||
public static int HaveClass = (1 << 8); | ||
public const int HaveGetCharBuffer = (1 << 0); | ||
public const int HaveSequenceIn = (1 << 1); | ||
public const int GC = 0; | ||
public const int HaveInPlaceOps = (1 << 3); | ||
public const int CheckTypes = (1 << 4); | ||
public const int HaveRichCompare = (1 << 5); | ||
public const int HaveWeakRefs = (1 << 6); | ||
public const int HaveIter = (1 << 7); | ||
public const int HaveClass = (1 << 8); | ||
#endif | ||
public static int HeapType = (1 << 9); | ||
public static int BaseType = (1 << 10); | ||
public static int Ready = (1 << 12); | ||
public static int Readying = (1 << 13); | ||
public static int HaveGC = (1 << 14); | ||
public const int HeapType = (1 << 9); | ||
public const int BaseType = (1 << 10); | ||
public const int Ready = (1 << 12); | ||
public const int Readying = (1 << 13); | ||
public const int HaveGC = (1 << 14); | ||
// 15 and 16 are reserved for stackless | ||
public static int HaveStacklessExtension = 0; | ||
public const int HaveStacklessExtension = 0; | ||
/* XXX Reusing reserved constants */ | ||
public static int Managed = (1 << 15); // PythonNet specific | ||
public static int Subclass = (1 << 16); // PythonNet specific | ||
public static int HaveIndex = (1 << 17); | ||
public const int Managed = (1 << 15); // PythonNet specific | ||
public const int Subclass = (1 << 16); // PythonNet specific | ||
public const int HaveIndex = (1 << 17); | ||
/* Objects support nb_index in PyNumberMethods */ | ||
public static int HaveVersionTag = (1 << 18); | ||
public static int ValidVersionTag = (1 << 19); | ||
public static int IsAbstract = (1 << 20); | ||
public static int HaveNewBuffer = (1 << 21); | ||
public const int HaveVersionTag = (1 << 18); | ||
public const int ValidVersionTag = (1 << 19); | ||
public const int IsAbstract = (1 << 20); | ||
public const int HaveNewBuffer = (1 << 21); | ||
// TODO: Implement FastSubclass functions | ||
public static int IntSubclass = (1 << 23); | ||
public static int LongSubclass = (1 << 24); | ||
public static int ListSubclass = (1 << 25); | ||
public static int TupleSubclass = (1 << 26); | ||
public static int StringSubclass = (1 << 27); | ||
public static int UnicodeSubclass = (1 << 28); | ||
public static int DictSubclass = (1 << 29); | ||
public static int BaseExceptionSubclass = (1 << 30); | ||
public static int TypeSubclass = (1 << 31); | ||
public const int IntSubclass = (1 << 23); | ||
public const int LongSubclass = (1 << 24); | ||
public const int ListSubclass = (1 << 25); | ||
public const int TupleSubclass = (1 << 26); | ||
public const int StringSubclass = (1 << 27); | ||
public const int UnicodeSubclass = (1 << 28); | ||
public const int DictSubclass = (1 << 29); | ||
public const int BaseExceptionSubclass = (1 << 30); | ||
public const int TypeSubclass = (1 << 31); | ||
|
||
#if PYTHON2 // Default flags for Python 2 | ||
public static int Default = ( | ||
public const int Default = ( | ||
HaveGetCharBuffer | | ||
HaveSequenceIn | | ||
HaveInPlaceOps | | ||
|
@@ -361,7 +361,7 @@ internal class TypeFlags | |
HaveIndex | | ||
0); | ||
#elif PYTHON3 // Default flags for Python 3 | ||
public static int Default = ( | ||
public const int Default = ( | ||
HaveStacklessExtension | | ||
HaveVersionTag); | ||
#endif | ||
|
@@ -499,9 +499,16 @@ internal static ThunkInfo GetThunk(MethodInfo method, string funcType = null) | |
{ | ||
return ThunkInfo.Empty; | ||
} | ||
Delegate d = Delegate.CreateDelegate(dt, method); | ||
var info = new ThunkInfo(d); | ||
return info; | ||
try | ||
{ | ||
Delegate d = Delegate.CreateDelegate(dt, method); | ||
var info = new ThunkInfo(d); | ||
return info; | ||
} | ||
catch (Exception) | ||
{ | ||
throw; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change seems unnecessary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
|
||
|
@@ -579,7 +586,7 @@ struct PyGC_Head | |
} | ||
|
||
|
||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] | ||
[StructLayout(LayoutKind.Sequential)] | ||
struct PyMethodDef | ||
{ | ||
public IntPtr ml_name; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,12 @@ namespace Python.Runtime | |
/// code. It defines the common fields that associate CLR and Python | ||
/// objects and common utilities to convert between those identities. | ||
/// </summary> | ||
[Serializable] | ||
internal abstract class ManagedType | ||
{ | ||
[NonSerialized] | ||
internal GCHandle gcHandle; // Native handle | ||
|
||
internal IntPtr pyHandle; // PyObject * | ||
internal IntPtr tpHandle; // PyType * | ||
|
||
|
@@ -190,6 +193,19 @@ protected void TypeClear() | |
ClearObjectDict(pyHandle); | ||
} | ||
|
||
internal void Save() | ||
{ | ||
OnSave(); | ||
} | ||
|
||
internal void Load() | ||
{ | ||
OnLoad(); | ||
} | ||
|
||
protected virtual void OnSave() { } | ||
protected virtual void OnLoad() { } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, add XML docs to these methods explaining what are they supposed to do |
||
|
||
protected static void ClearObjectDict(IntPtr ob) | ||
{ | ||
IntPtr dict = GetObjectDict(ob); | ||
|
@@ -201,12 +217,12 @@ protected static void ClearObjectDict(IntPtr ob) | |
Runtime.XDecref(dict); | ||
} | ||
|
||
private static IntPtr GetObjectDict(IntPtr ob) | ||
protected static IntPtr GetObjectDict(IntPtr ob) | ||
{ | ||
return Marshal.ReadIntPtr(ob, ObjectOffset.DictOffset(ob)); | ||
} | ||
|
||
private static void SetObjectDict(IntPtr ob, IntPtr value) | ||
protected static void SetObjectDict(IntPtr ob, IntPtr value) | ||
{ | ||
Marshal.WriteIntPtr(ob, ObjectOffset.DictOffset(ob), value); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
using System; | ||
using System.Collections; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Python.Runtime | ||
|
@@ -13,6 +15,12 @@ internal class MetaType : ManagedType | |
private static IntPtr PyCLRMetaType; | ||
private static SlotsHolder _metaSlotsHodler; | ||
|
||
internal static readonly string[] CustomMethods = new string[] | ||
{ | ||
"__instancecheck__", | ||
"__subclasscheck__", | ||
}; | ||
|
||
/// <summary> | ||
/// Metatype initialization. This bootstraps the CLR metatype to life. | ||
/// </summary> | ||
|
@@ -32,6 +40,29 @@ public static void Release() | |
_metaSlotsHodler = null; | ||
} | ||
|
||
internal static void StashPush(Stack stack) | ||
{ | ||
Runtime.XIncref(PyCLRMetaType); | ||
stack.Push(PyCLRMetaType); | ||
} | ||
|
||
internal static IntPtr StashPop(Stack stack) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add XML comments to these methods to explain what they do. |
||
{ | ||
PyCLRMetaType = (IntPtr)stack.Pop(); | ||
_metaSlotsHodler = new SlotsHolder(PyCLRMetaType); | ||
TypeManager.InitializeSlots(PyCLRMetaType, typeof(MetaType), _metaSlotsHodler); | ||
|
||
IntPtr mdef = Marshal.ReadIntPtr(PyCLRMetaType, TypeOffset.tp_methods); | ||
foreach (var methodName in CustomMethods) | ||
{ | ||
var mi = typeof(MetaType).GetMethod(methodName); | ||
ThunkInfo thunkInfo = Interop.GetThunk(mi, "BinaryFunc"); | ||
_metaSlotsHodler.KeeapAlive(thunkInfo); | ||
mdef = TypeManager.WriteMethodDef(mdef, methodName, thunkInfo.Address); | ||
} | ||
return PyCLRMetaType; | ||
} | ||
|
||
/// <summary> | ||
/// Metatype __new__ implementation. This is called to create a new | ||
/// class / type when a reflected class is subclassed. | ||
|
Uh oh!
There was an error while loading. Please reload this page.