Skip to content

Commit e9c3a3d

Browse files
committed
fixed InterfaceObject and MethodObject not being serializable
1 parent 53836de commit e9c3a3d

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/runtime/interfaceobject.cs

+17-5
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ namespace Python.Runtime
1313
[Serializable]
1414
internal class InterfaceObject : ClassBase
1515
{
16+
[NonSerialized]
1617
internal ConstructorInfo? ctor;
1718

1819
internal InterfaceObject(Type tp) : base(tp)
1920
{
20-
var coclass = (CoClassAttribute)Attribute.GetCustomAttribute(tp, cc_attr);
21-
if (coclass != null)
22-
{
23-
ctor = coclass.CoClass.GetConstructor(Type.EmptyTypes);
24-
}
21+
this.ctor = TryGetCOMConstructor(tp);
22+
}
23+
24+
static ConstructorInfo? TryGetCOMConstructor(Type tp)
25+
{
26+
var comClass = (CoClassAttribute?)Attribute.GetCustomAttribute(tp, cc_attr);
27+
return comClass?.CoClass.GetConstructor(Type.EmptyTypes);
2528
}
2629

2730
private static Type cc_attr;
@@ -113,5 +116,14 @@ public static NewReference tp_getattro(BorrowedReference ob, BorrowedReference k
113116

114117
return Runtime.PyObject_GenericGetAttr(ob, key);
115118
}
119+
120+
protected override void OnDeserialization(object sender)
121+
{
122+
base.OnDeserialization(sender);
123+
if (this.type.Valid)
124+
{
125+
this.ctor = TryGetCOMConstructor(this.type.Value);
126+
}
127+
}
116128
}
117129
}

src/runtime/methodobject.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal class MethodObject : ExtensionType
2525
internal bool is_static = false;
2626

2727
internal PyString? doc;
28-
internal Type type;
28+
internal MaybeType type;
2929

3030
public MethodObject(Type type, string name, MethodInfo[] info, bool allow_threads = MethodBinder.DefaultAllowThreads)
3131
{
@@ -157,6 +157,11 @@ public static NewReference tp_descr_get(BorrowedReference ds, BorrowedReference
157157
{
158158
var self = (MethodObject)GetManagedObject(ds)!;
159159

160+
if (!self.type.Valid)
161+
{
162+
return Exceptions.RaiseTypeError(self.type.DeletedMessage);
163+
}
164+
160165
// If the method is accessed through its type (rather than via
161166
// an instance) we return an 'unbound' MethodBinding that will
162167
// cached for future accesses through the type.
@@ -178,11 +183,11 @@ public static NewReference tp_descr_get(BorrowedReference ds, BorrowedReference
178183
// In which case create a MethodBinding bound to the base class.
179184
var obj = GetManagedObject(ob) as CLRObject;
180185
if (obj != null
181-
&& obj.inst.GetType() != self.type
186+
&& obj.inst.GetType() != self.type.Value
182187
&& obj.inst is IPythonDerivedType
183-
&& self.type.IsInstanceOfType(obj.inst))
188+
&& self.type.Value.IsInstanceOfType(obj.inst))
184189
{
185-
var basecls = ClassManager.GetClass(self.type);
190+
var basecls = ClassManager.GetClass(self.type.Value);
186191
return new MethodBinding(self, new PyObject(ob), basecls).Alloc();
187192
}
188193

0 commit comments

Comments
 (0)