8000 fixed bad GetManagedObject calls in CLR-based descriptor implementations · losttech/pythonnet@004b365 · GitHub
[go: up one dir, main page]

Skip to content

Commit 004b365

Browse files
committed
fixed bad GetManagedObject calls in CLR-based descriptor implementations
1 parent 0dc9fa5 commit 004b365

File tree

11 files changed

+58
-35
lines changed
Filter options

11 files changed

+58
-35
lines changed

src/embed_tests/Inspect.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void InstancePropertiesVisibleOnClass() {
3131
var uri = new Uri("http://example.org").ToPython();
3232
var uriClass = uri.GetPythonType();
3333
var property = uriClass.GetAttr(nameof(Uri.AbsoluteUri));
34-
var pyProp = ManagedType.GetManagedObject<PropertyObject>(property.Reference);
34+
var pyProp = ExtensionType.GetManagedObject<PropertyObject>(property.Reference);
3535
Assert.AreEqual(nameof(Uri.AbsoluteUri), pyProp.info.Name);
3636
}
3737

src/runtime/constructorbinding.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public ConstructorBinding(Type type, BorrowedReference pyTypeHndl, ConstructorBi
4040
/// of a .Overloads[pyTypeOrType...] syntax to allow explicit ctor overload
4141
/// selection.
4242
/// </summary>
43-
/// <param name="op"> PyObject* to a Constructors wrapper </param>
43+
/// <param name="opRaw"> PyObject* to a Constructors wrapper </param>
4444
/// <param name="instance">
4545
/// the instance that the attribute was accessed through,
4646
/// or None when the attribute is accessed through the owner
@@ -59,9 +59,10 @@ public ConstructorBinding(Type type, BorrowedReference pyTypeHndl, ConstructorBi
5959
/// the attribute was accessed through, or None when the attribute is accessed through the owner.
6060
/// This method should return the (computed) attribute value or raise an AttributeError exception.
6161
/// </remarks>
62-
public static IntPtr tp_descr_get(IntPtr op, IntPtr instance, IntPtr owner)
62+
public static IntPtr tp_descr_get(IntPtr opRaw, IntPtr instance, IntPtr owner)
6363
{
64-
var self = (ConstructorBinding)GetManagedObject(op);
64+
var op = new BorrowedReference(opRaw);
65+
var self = GetManagedObject<ConstructorBinding>(op);
6566
if (self == null)
6667
{
6768
return IntPtr.Zero;
@@ -86,9 +87,10 @@ public static IntPtr tp_descr_get(IntPtr op, IntPtr instance, IntPtr owner)
8687
/// Return element of o corresponding to the object key or NULL on failure.
8788
/// This is the equivalent of the Python expression o[key].
8889
/// </remarks>
89-
public static IntPtr mp_subscript(IntPtr op, IntPtr key)
90+
public static IntPtr mp_subscript(IntPtr opRaw, IntPtr key)
9091
{
91-
var self = (ConstructorBinding)GetManagedObject(op);
92+
var op = new BorrowedReference(opRaw);
93+
var self = GetManagedObject<ConstructorBinding>(op);
9294

9395
Type[] types = Runtime.PythonArgsToTypeArray(key);
9496
if (types == null)
@@ -110,9 +112,10 @@ public static IntPtr mp_subscript(IntPtr op, IntPtr key)
110112
/// <summary>
111113
/// ConstructorBinding __repr__ implementation [borrowed from MethodObject].
112114
/// </summary>
113-
public static IntPtr tp_repr(IntPtr ob)
115+
public static IntPtr tp_repr(IntPtr obRaw)
114116
{
115-
var self = (ConstructorBinding)GetManagedObject(ob);
117+
var ob = new BorrowedReference(obRaw);
118+
var self = GetManagedObject<ConstructorBinding>(ob);
116119
if (self.repr != IntPtr.Zero)
117120
{
118121
Runtime.XIncref(self.repr);
@@ -139,9 +142,10 @@ public static IntPtr tp_repr(IntPtr ob)
139142
/// <summary>
140143
/// ConstructorBinding dealloc implementation.
141144
/// </summary>
142-
public new static void tp_dealloc(IntPtr ob)
145+
public new static void tp_dealloc(IntPtr obRaw)
143146
{
144-
var self = (ConstructorBinding)GetManagedObject(ob);
147+
var ob = new BorrowedReference(obRaw);
148+
var self = GetManagedObject<ConstructorBinding>(ob);
145149
Runtime.XDecref(self.repr);
146150
Runtime.XDecref(self.pyTypeHndl);
147151
ExtensionType.FinalizeObject(self);

src/runtime/eventbinding.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ internal class EventBinding : ExtensionType
99
{
1010
private EventObject e;
1111
private IntPtr target;
12+
internal BorrowedReference Target => new BorrowedReference(target);
1213

1314
public EventBinding(EventObject e, IntPtr target)
1415
{
@@ -31,7 +32,7 @@ public static IntPtr nb_inplace_add(IntPtr ob, IntPtr arg)
3132
return IntPtr.Zero;
3233
}
3334

34-
if (!self.e.AddEventHandler(self.target, arg))
35+
if (!self.e.AddEventHandler(self.Target, arg))
3536
{
3637
return IntPtr.Zero;
3738
}
@@ -54,7 +55,7 @@ public static IntPtr nb_inplace_subtract(IntPtr ob, IntPtr arg)
5455
return IntPtr.Zero;
5556
}
5657

57-
if (!self.e.RemoveEventHandler(self.target, arg))
58+
if (!self.e.RemoveEventHandler(self.Target, arg))
5859
{
5960
return IntPtr.Zero;
6061
}

src/runtime/eventobject.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public EventObject(EventInfo info)
2424
/// <summary>
2525
/// Register a new Python object event handler with the event.
2626
/// </summary>
27-
internal bool AddEventHandler(IntPtr target, IntPtr handler)
27+
internal bool AddEventHandler(BorrowedReference target, IntPtr handler)
2828
{
2929
object obj = null;
3030
if (target != IntPtr.Zero)
3131
{
32-
var co = (CLRObject)GetManagedObject(target);
32+
var co = (CLRObject)ManagedType.GetManagedObject(target);
3333
obj = co.inst;
3434
}
3535

@@ -69,12 +69,12 @@ internal bool AddEventHandler(IntPtr target, IntPtr handler)
6969
/// <summary>
7070
/// Remove the given Python object event handler.
7171
/// </summary>
72-
internal bool RemoveEventHandler(IntPtr target, IntPtr handler)
72+
internal bool RemoveEventHandler(BorrowedReference target, IntPtr handler)
7373
{
7474
object obj = null;
7575
if (target != IntPtr.Zero)
7676
{
77-
var co = (CLRObject)GetManagedObject(target);
77+
var co = (CLRObject)ManagedType.GetManagedObject(target);
7878
obj = co.inst;
7979
}
8080

src/runtime/extensiontype.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ public ExtensionType()
4646
}
4747

4848

49+
internal static T GetManagedObject<T>(BorrowedReference ob)
50+
where T : ExtensionType
51+
=> (T)GetManagedObject(ob, ObjectOffset.GetDefaultGCHandleOffset());
52+
[Obsolete]
53+
internal static new ExtensionType GetManagedObject(IntPtr ob)
54+
=> (ExtensionType)GetManagedObject(new BorrowedReference(ob), ObjectOffset.GetDefaultGCHandleOffset());
55+
[Obsolete]
56+
internal static new ExtensionType GetManagedObject(BorrowedReference ob)
57+
=> (ExtensionType)GetManagedObject(ob, ObjectOffset.GetDefaultGCHandleOffset());
58+
59+
internal static bool IsExtensionType(BorrowedReference tp)
60+
{
61+
if (!IsManagedType(tp)) return false;
62+
var metaType = Runtime.PyObject_TYPE(tp);
63+
return metaType == Runtime.PyTypeType;
64+
}
65+
4966
/// <summary>
5067
/// Common finalization code to support custom tp_deallocs.
5168
/// </summary>

src/runtime/fieldobject.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ public FieldObject(FieldInfo info)
2020
/// value of the field on the given object. The returned value
2121
/// is converted to an appropriately typed Python object.
2222
/// </summary>
23-
public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
23+
public static IntPtr tp_descr_get(IntPtr ds, IntPtr obRaw, IntPtr tp)
2424
{
25+
var ob = new BorrowedReference(obRaw);
2526
var self = (FieldObject)GetManagedObject(ds);
2627
object result;
2728

@@ -54,7 +55,7 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
5455

5556
try
5657
{
57-
var co = (CLRObject)GetManagedObject(ob);
58+
var co = (CLRObject)ManagedType.GetManagedObject(ob);
5859
result = info.GetValue(co.inst);
5960
return Converter.ToPython(result);
6061
}
@@ -70,8 +71,9 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
7071
/// a field based on the given Python value. The Python value must be
7172
/// convertible to the type of the field.
7273
/// </summary>
73-
public new static int tp_descr_set(IntPtr ds, IntPtr ob, IntPtr val)
74+
public new static int tp_descr_set(IntPtr ds, IntPtr obRaw, IntPtr val)
7475
{
76+
var ob = new BorrowedReference(obRaw);
7577
var self = (FieldObject)GetManagedObject(ds);
7678
object newval;
7779

@@ -114,7 +116,7 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
114116
{
115117
if (!is_static)
116118
{
117-
var co = (CLRObject)GetManagedObject(ob);
119+
var co = (CLRObject)ManagedType.GetManagedObject(ob);
118120
info.SetValue(co.inst, newval);
119121
}
120122
else

src/runtime/managedtype.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ internal static ManagedType GetManagedObject(BorrowedReference ob)
5151
internal static ManagedType GetManagedObject(IntPtr ob)
5252
=> GetManagedObject(new BorrowedReference(ob));
5353

54-
internal static T GetManagedObject<T>(BorrowedReference ob)
55-
where T : ExtensionType
56-
=> (T)GetManagedObject(ob, ObjectOffset.GetDefaultGCHandleOffset());
57-
5854
internal static ManagedType GetManagedObject(BorrowedReference ob, int gcHandleOffset)
5955
{
6056
if (ob.IsNull) throw new ArgumentNullException(nameof(ob));

src/runtime/metatype.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public static int tp_setattro(IntPtr tp, IntPtr name, IntPtr value)
197197

198198
if (dt == Runtime.PyWrapperDescriptorType
199199
|| dt == Runtime.PyMethodType
200-
|| typeof(ExtensionType).IsInstanceOfType(GetManagedObject(descr))
200+
|| ExtensionType.IsExtensionType(new BorrowedReference(dt))
201201
)
202202
{
203203
IntPtr fp = Marshal.ReadIntPtr(dt, TypeOffset.tp_descr_set);

src/runtime/methodbinding.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ internal class MethodBinding : ExtensionType
1616
internal MethodObject m;
1717
internal IntPtr target;
1818
internal IntPtr targetType;
19-
internal BorrowedReference TargetReference => new BorrowedReference(this.target);
19+
internal BorrowedReference Target => new BorrowedReference(this.target);
20+
internal BorrowedReference TargetType => new BorrowedReference(this.targetType);
2021

2122
public MethodBinding(MethodObject m, IntPtr target, IntPtr targetType)
2223
{
@@ -197,7 +198,7 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
197198
var disposeList = new List<IntPtr>();
198199
try
199200
{
200-
IntPtr target = self.target;
201+
var target = self.target;
201202

202203
if (target == IntPtr.Zero && !self.m.IsStatic())
203204
{
@@ -221,14 +222,14 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
221222
IntPtr superType = IntPtr.Zero;
222223
if (Runtime.PyObject_TYPE(target) != self.targetType)
223224
{
224-
var inst = GetManagedObject(target) as CLRObject;
225+
var inst = ManagedType.GetManagedObject(target) as CLRObject;
225226
if (inst?.inst is IPythonDerivedType)
226227
{
227-
var baseType = GetManagedObject(self.targetType) as ClassBase;
228+
var baseType = ManagedType.GetManagedObject(self.TargetType) as ClassBase;
228229
if (baseType != null)
229230
{
230231
string baseMethodName = "_" + baseType.type.Name + "__" + self.m.name;
231-
using var baseMethod = Runtime.PyObject_GetAttrString(self.TargetReference, baseMethodName);
232+
using var baseMethod = Runtime.PyObject_GetAttrString(self.Target, baseMethodName);
232233
if (!baseMethod.IsNull())
233234
{
234235
BorrowedReference baseMethodType = Runtime.PyObject_TYPE(baseMethod);

src/runtime/methodobject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
178178
// this descriptor was defined on then it will be because the base class method
179179
// is being called via super(Derived, self).method(...).
180180
// In which case create a MethodBinding bound to the base class.
181-
var obj = GetManagedObject(ob) as CLRObject;
181+
var obj = ManagedType.GetManagedObject(ob) as CLRObject;
182182
if (obj != null
183183
&& obj.inst.GetType() != self.type
184184
&& obj.inst is IPythonDerivedType

src/runtime/propertyobject.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ static PropertyObject GetInstance(IntPtr ob)
2828
/// value of the property on the given object. The returned value
2929
/// is converted to an appropriately typed Python object.
3030
/// </summary>
31-
public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
31+
public static IntPtr tp_descr_get(IntPtr ds, IntPtr obRaw, IntPtr tp)
3232
{
33+
var ob = new BorrowedReference(obRaw);
3334
var self = GetInstance(ds);
3435
MethodInfo getter = self.getter;
3536
object result;
@@ -61,7 +62,7 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
6162
}
6263
}
6364

64-
var co = GetManagedObject(ob) as CLRObject;
65+
var co = ManagedType.GetManagedObject(ob) as CLRObject;
6566
if (co == null)
6667
{
6768
return Exceptions.RaiseTypeError("invalid target");
@@ -89,8 +90,9 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
8990
/// a property based on the given Python value. The Python value must
9091
/// be convertible to the type of the property.
9192
/// </summary>
92-
public new static int tp_descr_set(IntPtr ds, IntPtr ob, IntPtr val)
93+
public new static int tp_descr_set(IntPtr ds, IntPtr obRaw, IntPtr val)
9394
{
95+
var ob = new BorrowedReference(obRaw);
9496
var self = GetInstance(ds);
9597
MethodInfo setter = self.setter;
9698
object newval;
@@ -128,7 +130,7 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
128130
{
129131
if (!is_static)
130132
{
131-
var co = GetManagedObject(ob) as CLRObject;
133+
var co = ManagedType.GetManagedObject(ob) as CLRObject;
132134
if (co == null)
133135
{
134136
Exceptions.RaiseTypeError("invalid target");

0 commit comments

Comments
 (0)
0