| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ | |||
| 2 | 2 | using System.Collections; | |
| 3 | 3 | using System.Diagnostics; | |
| 4 | 4 | using System.Runtime.InteropServices; | |
| 5 | + using System.Runtime.Serialization; | ||
| 5 | 6 | ||
| 6 | 7 | namespace Python.Runtime | |
| 7 | 8 | { | |
@@ -304,9 +305,25 @@ public static int tp_clear(IntPtr ob) | |||
| 304 | 305 | return 0; | |
| 305 | 306 | } | |
| 306 | 307 | ||
| 307 | - protected override void OnLoad() | ||
| 308 | + protected override void OnSave(PyObjectSerializeContext context) | ||
| 308 | 309 | { | |
| 309 | - base.OnLoad(); | ||
| 310 | + base.OnSave(context); | ||
| 311 | + if (pyHandle != tpHandle) | ||
| 312 | + { | ||
| 313 | + IntPtr dict = GetObjectDict(pyHandle); | ||
| 314 | + Runtime.XIncref(dict); | ||
| 315 | + context.Storage.AddValue("dict", dict); | ||
| 316 | + } | ||
| 317 | + } | ||
| 318 | + | ||
| 319 | + protected override void OnLoad(PyObjectSerializeContext context) | ||
| 320 | + { | ||
| 321 | + base.OnLoad(context); | ||
| 322 | + if (pyHandle != tpHandle) | ||
| 323 | + { | ||
| 324 | + IntPtr dict = context.Storage.GetValue<IntPtr>("dict"); | ||
| 325 | + SetObjectDict(pyHandle, dict); | ||
| 326 | + } | ||
| 310 | 327 | gcHandle = AllocGCHandle(); | |
| 311 | 328 | Marshal.WriteIntPtr(pyHandle, TypeOffset.magic(), (IntPtr)gcHandle); | |
| 312 | 329 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -84,23 +84,31 @@ private static int OnVisit(IntPtr ob, IntPtr arg) | |||
| 84 | 84 | ||
| 85 | 85 | internal static void StashPush(RuntimeDataStorage storage) | |
| 86 | 86 | { | |
| 87 | - storage.PushValue(cache); | ||
| 87 | + var contexts = storage.AddValue("contexts", | ||
| 88 | + new Dictionary<IntPtr, PyObjectSerializeContext>()); | ||
| 89 | + storage.AddValue("cache", cache); | ||
| 88 | 90 | foreach (var cls in cache.Values) | |
| 89 | 91 | { | |
| 90 | 92 | // This incref is for cache to hold the cls, | |
| 91 | 93 | // thus no need for decreasing it at StashPop. | |
| 92 | 94 | Runtime.XIncref(cls.pyHandle); | |
| 93 | - cls.Save(); | ||
| 95 | + var context = contexts[cls.pyHandle] = new PyObjectSerializeContext(); | ||
| 96 | + cls.Save(context); | ||
| 94 | 97 | } | |
| 95 | 98 | } | |
| 96 | 99 | ||
| 97 | - internal static void StashPop(RuntimeDataStorage storage) | ||
| 100 | + internal static Dictionary<ManagedType, PyObjectSerializeContext> StashPop(RuntimeDataStorage storage) | ||
| 98 | 101 | { | |
| 99 | - cache = storage.PopValue<Dictionary<Type, ClassBase>>(); | ||
| 102 | + cache = storage.GetValue<Dictionary<Type, ClassBase>>("cache"); | ||
| 103 | + var contexts = storage.GetValue <Dictionary<IntPtr, PyObjectSerializeContext>>("contexts"); | ||
| 104 | + var loadedObjs = new Dictionary<ManagedType, PyObjectSerializeContext>(); | ||
| 100 | 105 | foreach (var cls in cache.Values) | |
| 101 | 106 | { | |
| 102 | - cls.Load(); | ||
| 107 | + var context = contexts[cls.pyHandle]; | ||
| 108 | + cls.Load(context); | ||
| 109 | + loadedObjs.Add(cls, context); | ||
| 103 | 110 | } | |
| 111 | + return loadedObjs; | ||
| 104 | 112 | } | |
| 105 | 113 | ||
| 106 | 114 | /// <summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -74,30 +74,29 @@ internal static IntPtr GetInstHandle(object ob) | |||
| 74 | 74 | return co.pyHandle; | |
| 75 | 75 | } | |
| 76 | 76 | ||
| 77 | - internal static CLRObject Restore(object ob, IntPtr pyHandle) | ||
| 77 | + internal static CLRObject Restore(object ob, IntPtr pyHandle, PyObjectSerializeContext context) | ||
| 78 | 78 | { | |
| 79 | 79 | CLRObject co = new CLRObject() | |
| 80 | 80 | { | |
| 81 | 81 | inst = ob, | |
| 82 | 82 | pyHandle = pyHandle, | |
| 83 | 83 | tpHandle = Runtime.PyObject_TYPE(pyHandle) | |
| 84 | 84 | }; | |
| 85 | - co.Load(); | ||
| 85 | + co.Load(context); | ||
| 86 | 86 | return co; | |
| 87 | 87 | } | |
| 88 | 88 | ||
| 89 | - protected override void OnSave() | ||
| 89 | + protected override void OnSave(PyObjectSerializeContext context) | ||
| 90 | 90 | { | |
| 91 | - base.OnSave(); | ||
| 91 | + base.OnSave(context); | ||
| 92 | 92 | Runtime.XIncref(pyHandle); | |
| 93 | 93 | } | |
| 94 | 94 | ||
| 95 | - protected override void OnLoad() | ||
| 95 | + protected override void OnLoad(PyObjectSerializeContext context) | ||
| 96 | 96 | { | |
| 97 | - base.OnLoad(); | ||
| 97 | + base.OnLoad(context); | ||
| 98 | 98 | GCHandle gc = AllocGCHandle(TrackTypes.Wrapper); | |
| 99 | 99 | Marshal.WriteIntPtr(pyHandle, ObjectOffset.magic(tpHandle), (IntPtr)gc); | |
| 100 | - Runtime.XDecref(pyHandle); | ||
| 101 | 100 | } | |
| 102 | 101 | } | |
| 103 | 102 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -98,9 +98,9 @@ public static void tp_dealloc(IntPtr ob) | |||
| 98 | 98 | self.Dealloc(); | |
| 99 | 99 | } | |
| 100 | 100 | ||
| 101 | - protected override void OnLoad() | ||
| 101 | + protected override void OnLoad(PyObjectSerializeContext context) | ||
| 102 | 102 | { | |
| 103 | - base.OnLoad(); | ||
| 103 | + base.OnLoad(context); | ||
| 104 | 104 | GCHandle gc = AllocGCHandle(TrackTypes.Extension); | |
| 105 | 105 | Marshal.WriteIntPtr(pyHandle, ObjectOffset.magic(tpHandle), (IntPtr)gc); | |
| 106 | 106 | Runtime.PyObject_GC_UnTrack(pyHandle); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -200,18 +200,18 @@ protected void TypeClear() | |||
| 200 | 200 | ClearObjectDict(pyHandle); | |
| 201 | 201 | } | |
| 202 | 202 | ||
| 203 | - internal void Save() | ||
| 203 | + internal void Save(PyObjectSerializeContext context) | ||
| 204 | 204 | { | |
| 205 | - OnSave(); | ||
| 205 | + OnSave(context); | ||
| 206 | 206 | } | |
| 207 | 207 | ||
| 208 | - internal void Load() | ||
| 208 | + internal void Load(PyObjectSerializeContext context) | ||
| 209 | 209 | { | |
| 210 | - OnLoad(); | ||
| 210 | + OnLoad(context); | ||
| 211 | 211 | } | |
| 212 | 212 | ||
| 213 | - protected virtual void OnSave() { } | ||
| 214 | - protected virtual void OnLoad() { } | ||
| 213 | + protected virtual void OnSave(PyObjectSerializeContext context) { } | ||
| 214 | + protected virtual void OnLoad(PyObjectSerializeContext context) { } | ||
| 215 | 215 | ||
| 216 | 216 | protected static void ClearObjectDict(IntPtr ob) | |
| 217 | 217 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -254,5 +254,12 @@ public static int tp_clear(IntPtr ob) | |||
| 254 | 254 | self.ClearMembers(); | |
| 255 | 255 | return 0; | |
| 256 | 256 | } | |
| 257 | + | ||
| 258 | + protected override void OnSave(PyObjectSerializeContext context) | ||
| 259 | + { | ||
| 260 | + base.OnSave(context); | ||
| 261 | + Runtime.XIncref(target); | ||
| 262 | + Runtime.XIncref(targetType); | ||
| 263 | + } | ||
| 257 | 264 | } | |
| 258 | 265 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,7 +18,7 @@ internal class MethodObject : ExtensionType | |||
| 18 | 18 | internal MethodBinding unbound; | |
| 19 | 19 | internal MethodBinder binder; | |
| 20 | 20 | internal bool is_static = false; | |
| 21 | - [NonSerialized] | ||
| 21 | + | ||
| 22 | 22 | internal IntPtr doc; | |
| 23 | 23 | internal Type type; | |
| 24 | 24 | ||
@@ -221,5 +221,15 @@ public static int tp_clear(IntPtr ob) | |||
| 221 | 221 | ClearObjectDict(ob); | |
| 222 | 222 | return 0; | |
| 223 | 223 | } | |
| 224 | + | ||
| 225 | + protected override void OnSave(PyObjectSerializeContext context) | ||
| 226 | + { | ||
| 227 | + base.OnSave(context); | ||
| 228 | + if (unbound != null) | ||
| 229 | + { | ||
| 230 | + Runtime.XIncref(unbound.pyHandle); | ||
| 231 | + } | ||
| 232 | + Runtime.XIncref(doc); | ||
| 233 | + } | ||
| 224 | 234 | } | |
| 225 | 235 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,13 +14,8 @@ namespace Python.Runtime | |||
| 14 | 14 | [Serializable] | |
| 15 | 15 | internal class ModuleObject : ExtensionType | |
| 16 | 16 | { | |
| 17 | - [NonSerialized] | ||
| 18 | 17 | private Dictionary<string, ManagedType> cache; | |
| 19 | 18 | ||
| 20 | - [NonSerialized] | ||
| 21 | - // FIXME: Used by reload mode, remove it after implement a delay load handler. | ||
| 22 | - private bool _cacheInited; | ||
| 23 | - | ||
| 24 | 19 | internal string moduleName; | |
| 25 | 20 | internal IntPtr dict; | |
| 26 | 21 | protected string _namespace; | |
@@ -33,7 +28,6 @@ public ModuleObject(string name) | |||
| 33 | 28 | } | |
| 34 | 29 | moduleName = name; | |
| 35 | 30 | cache = new Dictionary<string, ManagedType>(); | |
| 36 | - _cacheInited = true; | ||
| 37 | 31 | _namespace = name; | |
| 38 | 32 | ||
| 39 | 33 | // Use the filename from any of the assemblies just so there's something for | |
@@ -63,7 +57,7 @@ public ModuleObject(string name) | |||
| 63 | 57 | Runtime.XDecref(pydocstring); | |
| 64 | 58 | ||
| 65 | 59 | Runtime.XIncref(dict); | |
| 66 | - Marshal.WriteIntPtr(pyHandle, ObjectOffset.DictOffset(pyHandle), dict); | ||
| 60 | + SetObjectDict(pyHandle, dict); | ||
| 67 | 61 | ||
| 68 | 62 | InitializeModuleMembers(); | |
| 69 | 63 | } | |
@@ -77,11 +71,6 @@ public ModuleObject(string name) | |||
| 77 | 71 | /// </summary> | |
| 78 | 72 | public ManagedType GetAttribute(string name, bool guess) | |
| 79 | 73 | { | |
| 80 | - if (!_cacheInited) | ||
| 81 | - { | ||
| 82 | - // XXX: Used by reload mode. | ||
| 83 | - SetupCacheByDict(); | ||
| 84 | - } | ||
| 85 | 74 | ManagedType cached = null; | |
| 86 | 75 | cache.TryGetValue(name, out cached); | |
| 87 | 76 | if (cached != null) | |
@@ -360,46 +349,24 @@ public static int tp_clear(IntPtr ob) | |||
| 360 | 349 | return 0; | |
| 361 | 350 | } | |
| 362 | 351 | ||
| 363 | - protected override void OnSave() | ||
| 352 | + protected override void OnSave(PyObjectSerializeContext context) | ||
| 364 | 353 | { | |
| 365 | - base.OnSave(); | ||
| 354 | + base.OnSave(context); | ||
| 366 | 355 | System.Diagnostics.Debug.Assert(dict == GetObjectDict(pyHandle)); | |
| 356 | + foreach (var attr in cache.Values) | ||
| 357 | + { | ||
| 358 | + Runtime.XIncref(attr.pyHandle); | ||
| 359 | + } | ||
| 367 | 360 | // Decref twice in tp_clear, equilibrate them. | |
| 368 | 361 | Runtime.XIncref(dict); | |
| 369 | 362 | Runtime.XIncref(dict); | |
| 370 | 363 | } | |
| 371 | 364 | ||
| 372 | - protected override void OnLoad() | ||
| 365 | + protected override void OnLoad(PyObjectSerializeContext context) | ||
| 373 | 366 | { | |
| 374 | - base.OnLoad(); | ||
| 375 | - // XXX: Set the cache after all objects loaded. | ||
| 376 | - cache = new Dictionary<string, ManagedType>(); | ||
| 377 | - _cacheInited = false; | ||
| 367 | + base.OnLoad(context); | ||
| 378 | 368 | SetObjectDict(pyHandle, dict); | |
| 379 | 369 | } | |
| 380 | - | ||
| 381 | - private void SetupCacheByDict() | ||
| 382 | - { | ||
| 383 | - System.Diagnostics.Debug.Assert(!_cacheInited); | ||
| 384 | - _cacheInited = true; | ||
| 385 | - IntPtr key, value; | ||
| 386 | - IntPtr pos; | ||
| 387 | - while (Runtime.PyDict_Next(dict, out pos, out key, out value) != 0) | ||
| 388 | - { | ||
| 389 | - ManagedType obj = GetManagedObject(value); | ||
| 390 | - if (obj == null) | ||
| 391 | - { | ||
| 392 | - continue; | ||
| 393 | - } | ||
| 394 | - string name = Runtime.GetManagedString(key); | ||
| 395 | - if (cache.ContainsKey(name)) | ||
| 396 | - { | ||
| 397 | - continue; | ||
| 398 | - } | ||
| 399 | - Runtime.XIncref(value); | ||
| 400 | - cache.Add(name, obj); | ||
| 401 | - } | ||
| 402 | - } | ||
| 403 | 370 | } | |
| 404 | 371 | ||
| 405 | 372 | /// <summary> | |
| Back | FazBrowse Home | New Git URL |
0 commit comments