X Tutup
Skip to content
This repository was archived by the owner on Jul 22, 2023. It is now read-only.

Commit f23cae6

Browse files
committed
* Manipulate refcnt in Push/Pop objects
* Add several Serializable mark
1 parent 35cbe55 commit f23cae6

File tree

10 files changed

+23
-10
lines changed

10 files changed

+23
-10
lines changed

src/embed_tests/TestFinalizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public void ValidateRefCount()
214214
{
215215
if (!Finalizer.Instance.RefCountValidationEnabled)
216216
{
217-
Assert.Pass("Only run with FINALIZER_CHECK");
217+
Assert.Ignore("Only run with FINALIZER_CHECK");
218218
}
219219
IntPtr ptr = IntPtr.Zero;
220220
bool called = false;

src/runtime/arrayobject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Python.Runtime
88
/// the same as a ClassObject, except that it provides sequence semantics
99
/// to support natural array usage (indexing) from Python.
1010
/// </summary>
11+
[Serializable]
1112
internal class ArrayObject : ClassBase
1213
{
1314
internal ArrayObject(Type tp) : base(tp)

src/runtime/classderived.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public interface IPythonDerivedType
2222
{
2323
}
2424

25+
[Serializable]
2526
internal class ClassDerivedObject : ClassObject
2627
{
2728
private static Dictionary<string, AssemblyBuilder> assemblyBuilders;

src/runtime/classmanager.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,10 @@ internal static void RemoveClasses()
6969
private static int OnVisit(IntPtr ob, IntPtr arg)
7070
{
7171
var visited = (HashSet<IntPtr>)GCHandle.FromIntPtr(arg).Target;
72-
if (visited.Contains(ob))
72+
if (!visited.Add(ob))
7373
{
7474
return 0;
7575
}
76-
visited.Add(ob);
7776
var clrObj = ManagedType.GetManagedObject(ob);
7877
if (clrObj != null)
7978
{

src/runtime/exceptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace Python.Runtime
1515
/// it subclasses System.Object. Instead TypeManager.CreateType() uses
1616
/// Python's exception.Exception class as base class for System.Exception.
1717
/// </remarks>
18+
[Serializable]
1819
internal class ExceptionClassObject : ClassObject
1920
{
2021
internal ExceptionClassObject(Type tp) : base(tp)

src/runtime/generictype.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Python.Runtime
88
/// generic types. Both are essentially factories for creating closed
99
/// types based on the required generic type parameters.
1010
/// </summary>
11+
[Serializable]
1112
internal class GenericType : ClassBase
1213
{
1314
internal GenericType(Type tp) : base(tp)

src/runtime/interfaceobject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Python.Runtime
1010
/// Each of those type objects is associated with an instance of this
1111
/// class, which provides the implementation for the Python type.
1212
/// </summary>
13+
[Serializable]
1314
internal class InterfaceObject : ClassBase
1415
{
1516
internal ConstructorInfo ctor;

src/runtime/managedtype.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,12 @@ protected void TypeClear()
202202

203203
internal void Save()
204204
{
205-
Runtime.XIncref(pyHandle);
206205
OnSave();
207206
}
208207

209208
internal void Load()
210209
{
211210
OnLoad();
212-
Runtime.XDecref(pyHandle);
213211
}
214212

215213
protected virtual void OnSave() { }

src/runtime/pyobject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public interface IPyDisposable : IDisposable
2020
/// PY3: https://docs.python.org/3/c-api/object.html
2121
/// for details.
2222
/// </summary>
23-
//[Serializable]
24-
public class PyObject : DynamicObject, IEnumerable, IPyDisposable
23+
[Serializable]
24+
public partial class PyObject : DynamicObject, IEnumerable, IPyDisposable
2525
{
2626
#if TRACE_ALLOC
2727
/// <summary>

src/runtime/runtime_data.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,17 @@ private static void StashPopImpl()
110110
var formatter = CreateFormatter();
111111
var storage = (RuntimeDataStorage)formatter.Deserialize(ms);
112112

113-
StashPopObjects(storage.GetStorage("objs"));
113+
var objs = StashPopObjects(storage.GetStorage("objs"));
114114
StashPopModules(storage.GetStorage("modules"));
115115
ClassManager.StashPop(storage.GetStorage("classes"));
116116
TypeManager.StashPop(storage.GetStorage("types"));
117117
ImportHook.StashPop(storage.GetStorage("import"));
118118
PyCLRMetaType = MetaType.StashPop(storage.GetStorage("meta"));
119+
120+
foreach (var item in objs)
121+
{
122+
XDecref(item.pyHandle);
123+
}
119124
}
120125

121126
public static bool HasStashData()
@@ -137,6 +142,7 @@ private static void StashPushObjects(RuntimeDataStorage storage)
137142
foreach (var entry in objs)
138143
{
139144
var obj = entry.Key;
145+
XIncref(obj.pyHandle);
140146
switch (entry.Value)
141147
{
142148
case ManagedType.TrackTypes.Extension:
@@ -190,6 +196,7 @@ private static void StashPushObjects(RuntimeDataStorage storage)
190196
}
191197
foreach (var clrObj in wrappers[item.Instance])
192198
{
199+
XIncref(clrObj.pyHandle);
193200
clrObj.Save();
194201
}
195202
}
@@ -198,13 +205,15 @@ private static void StashPushObjects(RuntimeDataStorage storage)
198205
storage.AddValue("wrappers", wrapperStorage);
199206
}
200207

201-
private static void StashPopObjects(RuntimeDataStorage storage)
208+
private static IEnumerable<ManagedType> StashPopObjects(RuntimeDataStorage storage)
202209
{
203210
var extensions = storage.GetValue<List<ManagedType>>("extensions");
204211
var internalStores = storage.GetValue<List<CLRObject>>("internalStores");
212+
var storedObjs = new List<ManagedType>();
205213
foreach (var obj in Enumerable.Union(extensions, internalStores))
206214
{
207215
obj.Load();
216+
storedObjs.Add(obj);
208217
}
209218
if (WrappersStorer != null)
210219
{
@@ -215,10 +224,12 @@ private static void StashPopObjects(RuntimeDataStorage storage)
215224
object obj = item.Instance;
216225
foreach (var handle in item.Handles)
217226
{
218-
CLRObject.Restore(obj, handle);
227+
var co = CLRObject.Restore(obj, handle);
228+
storedObjs.Add(co);
219229
}
220230
}
221231
}
232+
return storedObjs;
222233
}
223234

224235
private static void StashPushModules(RuntimeDataStorage storage)

0 commit comments

Comments
 (0)
X Tutup