Skip to content

Commit d626f7e

Browse files
committed
partially switched classderived.cs to the new reference style
1 parent c05c6ec commit d626f7e

File tree

3 files changed

+41
-46
lines changed

3 files changed

+41
-46
lines changed

src/runtime/classderived.cs

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
#nullable enable
12
using System;
23
using System.Collections.Generic;
34
using System.ComponentModel;
45
using System.Diagnostics;
56
using System.Linq;
67
using System.Reflection;
78
using System.Reflection.Emit;
8-
using System.Resources;
99
using System.Runtime.InteropServices;
1010
using System.Threading.Tasks;
1111

12+
using Python.Runtime.Native;
13+
1214
namespace Python.Runtime
1315
{
1416
/// <summary>
@@ -49,25 +51,25 @@ internal ClassDerivedObject(Type tp) : base(tp)
4951
/// <summary>
5052
/// Implements __new__ for derived classes of reflected classes.
5153
/// </summary>
52-
public new static IntPtr tp_new(IntPtr tp, IntPtr args, IntPtr kw)
54+
public new static NewReference tp_new(BorrowedReference tp, BorrowedReference args, BorrowedReference kw)
5355
{
54-
var cls = GetManagedObject(tp) as ClassDerivedObject;
56+
var cls = (ClassDerivedObject)GetManagedObject(tp)!;
5557

5658
// call the managed constructor
57-
object obj = cls.binder.InvokeRaw(IntPtr.Zero, args, kw);
59+
object obj = cls.binder.InvokeRaw(null, args, kw);
5860
if (obj == null)
5961
{
60-
return IntPtr.Zero;
62+
return default;
6163
}
6264

6365
// return the pointer to the python object
6466
// (this indirectly calls ClassDerivedObject.ToPython)
6567
return Converter.ToPython(obj, cls.GetType());
6668
}
6769

68-
public new static void tp_dealloc(IntPtr ob)
70+
public new static void tp_dealloc(NewReference ob)
6971
{
70-
var self = (CLRObject)GetManagedObject(ob);
72+
var self = (CLRObject)GetManagedObject(ob.Borrow())!;
7173

7274
// don't let the python GC destroy this object
7375
Runtime.PyObject_GC_UnTrack(self.pyHandle);
@@ -94,14 +96,14 @@ internal static NewReference ToPython(IPythonDerivedType obj)
9496
FieldInfo fi = obj.GetType().GetField("__pyobj__");
9597
var self = (CLRObject)fi.GetValue(obj);
9698

97-
Runtime.XIncref(self.pyHandle);
99+
var result = new NewReference(self.ObjectReference);
98100

99101
// when the C# constructor creates the python object it starts as a weak
100102
// reference with a reference count of 0. Now we're passing this object
101103
// to Python the reference count needs to be incremented and the reference
102104
// needs to be replaced with a strong reference to stop the C# object being
103105
// collected while Python still has a reference to it.
104-
if (Runtime.Refcount(self.pyHandle) == 1)
106+
if (Runtime.Refcount(result.Borrow()) == 1)
105107
{
106108
Runtime._Py_NewReference(self.ObjectReference);
107109
GCHandle gc = GCHandle.Alloc(self, GCHandleType.Normal);
@@ -113,7 +115,7 @@ internal static NewReference ToPython(IPythonDerivedType obj)
113115
Runtime.PyObject_GC_Track(self.pyHandle);
114116
}
115117

116-
return self.pyHandle;
118+
return result;
117119
}
118120

119121
/// <summary>
@@ -123,13 +125,12 @@ internal static NewReference ToPython(IPythonDerivedType obj)
123125
/// </summary>
124126
internal static Type CreateDerivedType(string name,
125127
Type baseType,
126-
BorrowedReference dictRef,
128+
BorrowedReference py_dict,
127129
string namespaceStr,
128130
string assemblyName,
129131
string moduleName = "Python.Runtime.Dynamic.dll")
130132
{
131133
// TODO: clean up
132-
IntPtr py_dict = dictRef.DangerousGetAddress();
133134
if (null != namespaceStr)
134135
{
135136
name = namespaceStr + "." + name;
@@ -171,9 +172,9 @@ internal static Type CreateDerivedType(string name,
171172

172173
// Override any properties explicitly overridden in python
173174
var pyProperties = new HashSet<string>();
174-
if (py_dict != IntPtr.Zero && Runtime.PyDict_Check(py_dict))
175+
if (py_dict != null && Runtime.PyDict_Check(py_dict))
175176
{
176-
using var dict = new PyDict(new BorrowedReference(py_dict));
177+
using var dict = new PyDict(py_dict);
177178
using (PyIterable keys = dict.Keys())
178179
{
179180
foreach (PyObject pyKey in keys)
@@ -182,7 +183,7 @@ internal static Type CreateDerivedType(string name,
182183
{
183184
if (value.HasAttr("_clr_property_type_"))
184185
{
185-
string propertyName = pyKey.ToString();
186+
string propertyName = pyKey.ToString()!;
186187
pyProperties.Add(propertyName);
187188

188189
// Add the property to the type
@@ -219,9 +220,9 @@ internal static Type CreateDerivedType(string name,
219220
}
220221

221222
// Add any additional methods and properties explicitly exposed from Python.
222-
if (py_dict != IntPtr.Zero && Runtime.PyDict_Check(py_dict))
223+
if (py_dict != null && Runtime.PyDict_Check(py_dict))
223224
{
224-
using var dict = new PyDict(new BorrowedReference(py_dict));
225+
using var dict = new PyDict(py_dict);
225226
using (PyIterable keys = dict.Keys())
226227
{
227228
foreach (PyObject pyKey in keys)
@@ -230,7 +231,7 @@ internal static Type CreateDerivedType(string name,
230231
{
231232
if (value.HasAttr("_clr_return_type_") && value.HasAttr("_clr_arg_types_"))
232233
{
233-
string methodName = pyKey.ToString();
234+
string methodName = pyKey.ToString()!;
234235

235236
// if this method has already been redirected to the python method skip it
236237
if (virtualMethods.Contains(methodName))
@@ -652,11 +653,10 @@ public static T InvokeMethod<T>(IPythonDerivedType obj, string methodName, strin
652653
if (null != self)
653654
{
654655
var disposeList = new List<PyObject>();
655-
IntPtr gs = Runtime.PyGILState_Ensure();
656+
PyGILState gs = Runtime.PyGILState_Ensure();
656657
try
657658
{
658-
Runtime.XIncref(self.pyHandle);
659-
var pyself = new PyObject(self.pyHandle);
659+
var pyself = new PyObject(self.ObjectReference);
660660
disposeList.Add(pyself);
661661

662662
Runtime.XIncref(Runtime.PyNone);
@@ -665,16 +665,16 @@ public static T InvokeMethod<T>(IPythonDerivedType obj, string methodName, strin
665665

666666
PyObject method = pyself.GetAttr(methodName, pynone);
667667
disposeList.Add(method);
668-
if (method.Handle != Runtime.PyNone)
668+
if (method.Reference != Runtime.PyNone)
669669
{
670670
// if the method hasn't been overridden then it will be a managed object
671-
ManagedType managedMethod = ManagedType.GetManagedObject(method.Handle);
671+
ManagedType? managedMethod = ManagedType.GetManagedObject(method.Reference);
672672
if (null == managedMethod)
673673
{
674674
var pyargs = new PyObject[args.Length];
675675
for (var i = 0; i < args.Length; ++i)
676676
{
677-
pyargs[i] = new PyObject(Converter.ToPythonImplicit(args[i]));
677+
pyargs[i] = new PyObject(Converter.ToPythonImplicit(args[i]).Steal());
678678
disposeList.Add(pyargs[i]);
679679
}
680680

@@ -714,11 +714,10 @@ public static void InvokeMethodVoid(IPythonDerivedType obj, string methodName, s
714714
if (null != self)
715715
{
716716
var disposeList = new List<PyObject>();
717-
IntPtr gs = Runtime.PyGILState_Ensure();
717+
PyGILState gs = Runtime.PyGILState_Ensure();
718718
try
719719
{
720-
Runtime.XIncref(self.pyHandle);
721-
var pyself = new PyObject(self.pyHandle);
720+
var pyself = new PyObject(self.ObjectReference);
722721
disposeList.Add(pyself);
723722

724723
Runtime.XIncref(Runtime.PyNone);
@@ -727,16 +726,16 @@ public static void InvokeMethodVoid(IPythonDerivedType obj, string methodName, s
727726

728727
PyObject method = pyself.GetAttr(methodName, pynone);
729728
disposeList.Add(method);
730-
if (method.Handle != Runtime.PyNone)
729+
if (method.Reference != Runtime.PyNone)
731730
{
732731
// if the method hasn't been overridden then it will be a managed object
733-
ManagedType managedMethod = ManagedType.GetManagedObject(method.Handle);
732+
ManagedType? managedMethod = ManagedType.GetManagedObject(method.Handle);
734733
if (null == managedMethod)
735734
{
736735
var pyargs = new PyObject[args.Length];
737736
for (var i = 0; i < args.Length; ++i)
738737
{
739-
pyargs[i] = new PyObject(Converter.ToPythonImplicit(args[i]));
738+
pyargs[i] = new PyObject(Converter.ToPythonImplicit(args[i]).Steal());
740739
disposeList.Add(pyargs[i]);
741740
}
742741

@@ -778,11 +777,10 @@ public static T InvokeGetProperty<T>(IPythonDerivedType obj, string propertyName
778777
throw new NullReferenceException("Instance must be specified when getting a property");
779778
}
780779

781-
IntPtr gs = Runtime.PyGILState_Ensure();
780+
PyGILState gs = Runtime.PyGILState_Ensure();
782781
try
783782
{
784-
Runtime.XIncref(self.pyHandle);
785-
using (var pyself = new PyObject(self.pyHandle))
783+
using var pyself = new PyObject(self.ObjectReference);
786784
using (PyObject pyvalue = pyself.GetAttr(propertyName))
787785
{
788786
return (T)pyvalue.AsManagedObject(typeof(T));
@@ -804,15 +802,12 @@ public static void InvokeSetProperty<T>(IPythonDerivedType obj, string propertyN
804802
throw new NullReferenceException("Instance must be specified when setting a property");
805803
}
806804

807-
IntPtr gs = Runtime.PyGILState_Ensure();
805+
PyGILState gs = Runtime.PyGILState_Ensure();
808806
try
809807
{
810-
Runtime.XIncref(self.pyHandle);
811-
using (var pyself = new PyObject(self.pyHandle))
812-
using (var pyvalue = new PyObject(Converter.ToPythonImplicit(value)))
813-
{
814-
pyself.SetAttr(propertyName, pyvalue);
815-
}
808+
using var pyself = new PyObject(self.ObjectReference);
809+
using var pyvalue = new PyObject(Converter.ToPythonImplicit(value).Steal());
810+
pyself.SetAttr(propertyName, pyvalue);
816811
}
817812
finally
818813
{
@@ -829,8 +824,8 @@ public static void InvokeCtor(IPythonDerivedType obj, string origCtorName, objec
829824
obj,
830825
args);
831826

832-
CLRObject self = null;
833-
IntPtr gs = Runtime.PyGILState_Ensure();
827+
CLRObject? self = null;
828+
PyGILState gs = Runtime.PyGILState_Ensure();
834829
try
835830
{
836831
// create the python object
@@ -885,7 +880,7 @@ public static void Finalize(IPythonDerivedType obj)
885880
return;
886881
}
887882

888-
IntPtr gs = Runtime.PyGILState_Ensure();
883+
PyGILState gs = Runtime.PyGILState_Ensure();
889884
try
890885
{
891886
// the C# object is being destroyed which must mean there are no more

src/runtime/constructorbinder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal ConstructorBinder(Type containingType)
2929
/// object - the reason is that only the caller knows the correct
3030
/// Python type to use when wrapping the result (may be a subclass).
3131
/// </summary>
32-
internal object InvokeRaw(IntPtr inst, IntPtr args, IntPtr kw)
32+
internal object InvokeRaw(BorrowedReference inst, BorrowedReference args, BorrowedReference kw)
3333
{
3434
return InvokeRaw(inst, args, kw, null);
3535
}
@@ -49,7 +49,7 @@ internal object InvokeRaw(IntPtr inst, IntPtr args, IntPtr kw)
4949
/// Binding binding = this.Bind(inst, args, kw, info);
5050
/// to take advantage of Bind()'s ability to use a single MethodBase (CI or MI).
5151
/// </remarks>
52-
internal object InvokeRaw(IntPtr inst, IntPtr args, IntPtr kw, MethodBase info)
52+
internal object InvokeRaw(BorrowedReference inst, BorrowedReference args, BorrowedReference kw, MethodBase info)
5353
{
5454
if (!_containingType.Valid)
5555
{

src/runtime/converter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ static bool EncodableByUser(Type type, object value)
243243
/// In a few situations, we don't have any advisory type information
244244
/// when we want to convert an object to Python.
245245
/// </summary>
246-
internal static NewReference ToPythonImplicit(object value)
246+
internal static NewReference ToPythonImplicit(object? value)
247247
{
248248
if (value == null)
249249
{

0 commit comments

Comments
 (0)