| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,11 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + | ||
| 4 | + namespace Python.Runtime.StateSerialization; | ||
| 5 | + | ||
| 6 | + [Serializable] | ||
| 7 | + internal class ClassManagerState | ||
| 8 | + { | ||
| 9 | + public Dictionary<PyType, InterDomainContext> Contexts { get; set; } | ||
| 10 | + public Dictionary<MaybeType, ClassBase> Cache { get; set; } | ||
| 11 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,12 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + | ||
| 4 | + namespace Python.Runtime.StateSerialization; | ||
| 5 | + | ||
| 6 | + [Serializable] | ||
| 7 | + internal class ImportHookState | ||
| 8 | + { | ||
| 9 | + public PyModule PyCLRModule { get; set; } | ||
| 10 | + public PyObject Root { get; set; } | ||
| 11 | + public Dictionary<PyString, PyObject> Modules { get; set; } | ||
| 12 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,9 @@ | |||
| 1 | + using System; | ||
| 2 | + | ||
| 3 | + namespace Python.Runtime.StateSerialization; | ||
| 4 | + | ||
| 5 | + [Serializable] | ||
| 6 | + internal class MetatypeState | ||
| 7 | + { | ||
| 8 | + public PyType CLRMetaType { get; set; } | ||
| 9 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,13 @@ | |||
| 1 | + using System; | ||
| 2 | + | ||
| 3 | + namespace Python.Runtime.StateSerialization; | ||
| 4 | + | ||
| 5 | + [Serializable] | ||
| 6 | + internal class PythonNetState | ||
| 7 | + { | ||
| 8 | + public MetatypeState Metatype { get; set; } | ||
| 9 | + public SharedObjectsState SharedObjects { get; set; } | ||
| 10 | + public TypeManagerState Types { get; set; } | ||
| 11 | + public ClassManagerState Classes { get; set; } | ||
| 12 | + public ImportHookState ImportHookState { get; set; } | ||
| 13 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,13 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + | ||
| 4 | + namespace Python.Runtime.StateSerialization; | ||
| 5 | + | ||
| 6 | + [Serializable] | ||
| 7 | + internal class SharedObjectsState | ||
| 8 | + { | ||
| 9 | + public List<CLRObject> InternalStores { get; set; } | ||
| 10 | + public List<ManagedType> Extensions { get; set; } | ||
| 11 | + public RuntimeDataStorage Wrappers { get; set; } | ||
| 12 | + public Dictionary<PyObject, InterDomainContext> Contexts { get; set; } | ||
| 13 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,11 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + | ||
| 4 | + namespace Python.Runtime.StateSerialization; | ||
| 5 | + | ||
| 6 | + [Serializable] | ||
| 7 | + internal class TypeManagerState | ||
| 8 | + { | ||
| 9 | + public Dictionary<MaybeType, PyType> Cache { get; set; } | ||
| 10 | + public Dictionary<MaybeType, Type> SlotImplementations { get; set; } | ||
| 11 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -405,8 +405,7 @@ protected override void OnSave(InterDomainContext context) | |||
| 405 | 405 | if (!this.IsClrMetaTypeInstance()) | |
| 406 | 406 | { | |
| 407 | 407 | BorrowedReference dict = GetObjectDict(ObjectReference); | |
| 408 | - Runtime.XIncref(dict); | ||
| 409 | - context.Storage.AddValue("dict", dict); | ||
| 408 | + context.Storage.AddValue("dict", PyObject.FromNullableReference(dict)); | ||
| 410 | 409 | } | |
| 411 | 410 | } | |
| 412 | 411 | ||
@@ -415,8 +414,8 @@ protected override void OnLoad(InterDomainContext context) | |||
| 415 | 414 | base.OnLoad(context); | |
| 416 | 415 | if (!this.IsClrMetaTypeInstance()) | |
| 417 | 416 | { | |
| 418 | - IntPtr dict = context.Storage.GetValue<IntPtr>("dict"); | ||
| 419 | - SetObjectDict(ObjectReference, dict); | ||
| 417 | + var dict = context.Storage.GetValue<PyObject>("dict"); | ||
| 418 | + SetObjectDict(ObjectReference, dict.NewReferenceOrNull().StealNullable()); | ||
| 420 | 419 | } | |
| 421 | 420 | gcHandle = AllocGCHandle(); | |
| 422 | 421 | SetGCHandle(ObjectReference, gcHandle); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,8 @@ | |||
| 7 | 7 | using System.Runtime.InteropServices; | |
| 8 | 8 | using System.Security; | |
| 9 | 9 | ||
| 10 | + using Python.Runtime.StateSerialization; | ||
| 11 | + | ||
| 10 | 12 | namespace Python.Runtime | |
| 11 | 13 | { | |
| 12 | 14 | /// <summary> | |
@@ -93,21 +95,16 @@ private static int TraverseTypeClear(BorrowedReference ob, IntPtr arg) | |||
| 93 | 95 | return 0; | |
| 94 | 96 | } | |
| 95 | 97 | ||
| 96 | - internal static void SaveRuntimeData(RuntimeDataStorage storage) | ||
| 98 | + internal static ClassManagerState SaveRuntimeData() | ||
| 97 | 99 | { | |
| 98 | - var contexts = storage.AddValue("contexts", | ||
| 99 | - new Dictionary<PyType, InterDomainContext>(PythonReferenceComparer.Instance)); | ||
| 100 | - storage.AddValue("cache", cache); | ||
| 100 | + var contexts = new Dictionary<PyType, InterDomainContext>(PythonReferenceComparer.Instance); | ||
| 101 | 101 | foreach (var cls in cache) | |
| 102 | 102 | { | |
| 103 | 103 | if (!cls.Key.Valid) | |
| 104 | 104 | { | |
| 105 | 105 | // Don't serialize an invalid class | |
| 106 | 106 | continue; | |
| 107 | 107 | } | |
| 108 | - // This incref is for cache to hold the cls, | ||
| 109 | - // thus no need for decreasing it at RestoreRuntimeData. | ||
| 110 | - Runtime.XIncref(cls.Value.pyHandle); | ||
| 111 | 108 | var context = contexts[cls.Value.pyHandle] = new InterDomainContext(); | |
| 112 | 109 | cls.Value.Save(context); | |
| 113 | 110 | ||
@@ -137,13 +134,19 @@ internal static void SaveRuntimeData(RuntimeDataStorage storage) | |||
| 137 | 134 | // We modified the Type object, notify it we did. | |
| 138 | 135 | Runtime.PyType_Modified(cls.Value.TypeReference); | |
| 139 | 136 | } | |
| 137 | + | ||
| 138 | + return new() | ||
| 139 | + { | ||
| 140 | + Contexts = contexts, | ||
| 141 | + Cache = cache, | ||
| 142 | + }; | ||
| 140 | 143 | } | |
| 141 | 144 | ||
| 142 | - internal static Dictionary<ManagedType, InterDomainContext> RestoreRuntimeData(RuntimeDataStorage storage) | ||
| 145 | + internal static Dictionary<ManagedType, InterDomainContext> RestoreRuntimeData(ClassManagerState storage) | ||
| 143 | 146 | { | |
| 144 | - cache = storage.GetValue<Dictionary<MaybeType, ClassBase>>("cache"); | ||
| 147 | + cache = storage.Cache; | ||
| 145 | 148 | var invalidClasses = new List<KeyValuePair<MaybeType, ClassBase>>(); | |
| 146 | - var contexts = storage.GetValue <Dictionary<PyType, InterDomainContext>>("contexts"); | ||
| 149 | + var contexts = storage.Contexts; | ||
| 147 | 150 | var loadedObjs = new Dictionary<ManagedType, InterDomainContext>(); | |
| 148 | 151 | foreach (var pair in cache) | |
| 149 | 152 | { | |
@@ -171,7 +174,7 @@ internal static Dictionary<ManagedType, InterDomainContext> RestoreRuntimeData(R | |||
| 171 | 174 | foreach (var pair in invalidClasses) | |
| 172 | 175 | { | |
| 173 | 176 | cache.Remove(pair.Key); | |
| 174 | - Runtime.XDecref(pair.Value.pyHandle); | ||
| 177 | + pair.Value.pyHandle.Dispose(); | ||
| 175 | 178 | } | |
| 176 | 179 | ||
| 177 | 180 | return loadedObjs; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,7 +16,7 @@ internal CLRObject(object ob, PyType tp) | |||
| 16 | 16 | using var py = Runtime.PyType_GenericAlloc(tp, 0); | |
| 17 | 17 | ||
| 18 | 18 | tpHandle = tp; | |
| 19 | - pyHandle = py; | ||
| 19 | + pyHandle = py.MoveToPyObject(); | ||
| 20 | 20 | inst = ob; | |
| 21 | 21 | ||
| 22 | 22 | GCHandle gc = AllocGCHandle(TrackTypes.Wrapper); | |
@@ -45,44 +45,34 @@ static CLRObject GetInstance(object ob) | |||
| 45 | 45 | ||
| 46 | 46 | internal static NewReference GetReference(object ob, BorrowedReference pyType) | |
| 47 | 47 | { | |
| 48 | - CLRObject co = GetInstance(ob, pyType.DangerousGetAddress()); | ||
| 49 | - return NewReference.DangerousFromPointer(co.pyHandle); | ||
| 50 | - } | ||
| 51 | - internal static IntPtr GetInstHandle(object ob, IntPtr pyType) | ||
| 52 | - { | ||
| 53 | - CLRObject co = GetInstance(ob, pyType); | ||
| 54 | - return co.pyHandle; | ||
| 48 | + CLRObject co = GetInstance(ob, new PyType(pyType)); | ||
| 49 | + return new NewReference(co.pyHandle); | ||
| 55 | 50 | } | |
| 56 | 51 | ||
| 57 | - | ||
| 58 | - internal static IntPtr GetInstHandle(object ob, Type type) | ||
| 52 | + internal static NewReference GetReference(object ob, Type type) | ||
| 59 | 53 | { | |
| 60 | 54 | ClassBase cc = ClassManager.GetClass(type); | |
| 61 | 55 | CLRObject co = GetInstance(ob, cc.tpHandle); | |
| 62 | - return co.pyHandle; | ||
| 56 | + return new NewReference(co.pyHandle); | ||
| 63 | 57 | } | |
| 64 | 58 | ||
| 65 | 59 | ||
| 66 | - internal static IntPtr GetInstHandle(object ob) | ||
| 60 | + internal static NewReference GetReference(object ob) | ||
| 67 | 61 | { | |
| 68 | 62 | CLRObject co = GetInstance(ob); | |
| 69 | - return co.pyHandle; | ||
| 63 | + return new NewReference(co.pyHandle); | ||
| 70 | 64 | } | |
| 71 | 65 | ||
| 72 | - internal static NewReference GetReference(object ob) | ||
| 73 | - => NewReference.DangerousFromPointer(GetInstHandle(ob)); | ||
| 74 | - internal static NewReference GetReference(object ob, Type type) | ||
| 75 | - => NewReference.DangerousFromPointer(GetInstHandle(ob, type)); | ||
| 76 | - | ||
| 77 | - internal static CLRObject Restore(object ob, IntPtr pyHandle, InterDomainContext context) | ||
| 66 | + internal static CLRObject Restore(object ob, BorrowedReference pyHandle, InterDomainContext context) | ||
| 78 | 67 | { | |
| 68 | + var pyObj = new PyObject(pyHandle); | ||
| 79 | 69 | CLRObject co = new CLRObject() | |
| 80 | 70 | { | |
| 81 | 71 | inst = ob, | |
| 82 | - pyHandle = pyHandle, | ||
| 83 | - tpHandle = Runtime.PyObject_TYPE(pyHandle) | ||
| 72 | + pyHandle = pyObj, | ||
| 73 | + tpHandle = pyObj.GetPythonType(), | ||
| 84 | 74 | }; | |
| 85 | - Debug.Assert(co.tpHandle != IntPtr.Zero); | ||
| 75 | + Debug.Assert(co.tpHandle != null); | ||
| 86 | 76 | co.Load(context); | |
| 87 | 77 | return co; | |
| 88 | 78 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -30,8 +30,8 @@ public ExtensionType() | |||
| 30 | 30 | NewReference py = Runtime.PyType_GenericAlloc(tp, 0); | |
| 31 | 31 | ||
| 32 | 32 | // Borrowed reference. Valid as long as pyHandle is valid. | |
| 33 | - tpHandle = tp.DangerousGetAddress(); | ||
| 34 | - pyHandle = py.DangerousMoveToPointer(); | ||
| 33 | + tpHandle = new PyType(tp, prevalidated: true); | ||
| 34 | + pyHandle = py.MoveToPyObject(); | ||
| 35 | 35 | ||
| 36 | 36 | #if DEBUG | |
| 37 | 37 | GetGCHandle(ObjectReference, TypeReference, out var existing); | |
@@ -79,26 +79,26 @@ protected virtual void Clear() | |||
| 79 | 79 | public static int tp_setattro(BorrowedReference ob, BorrowedReference key, BorrowedReference val) | |
| 80 | 80 | { | |
| 81 | 81 | var message = "type does not support setting attributes"; | |
| 82 | - if (val == IntPtr.Zero) | ||
| 82 | + if (val == null) | ||
| 83 | 83 | { | |
| 84 | 84 | message = "readonly attribute"; | |
| 85 | 85 | } | |
| 86 | 86 | Exceptions.SetError(Exceptions.AttributeError, message); | |
| 87 | 87 | return -1; | |
| 88 | 88 | } | |
| 89 | 89 | ||
| 90 | - public static void tp_dealloc(IntPtr ob) | ||
| 90 | + public static void tp_dealloc(NewReference ob) | ||
| 91 | 91 | { | |
| 92 | 92 | // Clean up a Python instance of this extension type. This | |
| 93 | 93 | // frees the allocated Python object and decrefs the type. | |
| 94 | - var self = (ExtensionType)GetManagedObject(ob); | ||
| 94 | + var self = (ExtensionType?)GetManagedObject(ob.Borrow()); | ||
| 95 | 95 | self?.Clear(); | |
| 96 | 96 | self?.Dealloc(); | |
| 97 | 97 | } | |
| 98 | 98 | ||
| 99 | - public static int tp_clear(IntPtr ob) | ||
| 99 | + public static int tp_clear(BorrowedReference ob) | ||
| 100 | 100 | { | |
| 101 | - var self = (ExtensionType)GetManagedObject(ob); | ||
| 101 | + var self = (ExtensionType?)GetManagedObject(ob); | ||
| 102 | 102 | self?.Clear(); | |
| 103 | 103 | return 0; | |
| 104 | 104 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments