Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3b79019

Browse files
committed
switched fieldobject.cs and constructorbinding.cs to the new style references
1 parent 2dd3f8f commit 3b79019

File tree

2 files changed

+68
-65
lines changed

2 files changed

+68
-65
lines changed

src/runtime/constructorbinding.cs

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ internal class ConstructorBinding : ExtensionType
2727
private ConstructorBinder ctorBinder;
2828

2929
[NonSerialized]
30-
private IntPtr repr;
30+
private PyObject? repr;
3131

3232
public ConstructorBinding(Type type, PyType typeToCreate, ConstructorBinder ctorBinder)
3333
{
3434
this.type = type;
3535
this.typeToCreate = typeToCreate;
3636
this.ctorBinder = ctorBinder;
37-
repr = IntPtr.Zero;
3837
}
3938

4039
/// <summary>
@@ -62,12 +61,13 @@ public ConstructorBinding(Type type, PyType typeToCreate, ConstructorBinder ctor
6261
/// the attribute was accessed through, or None when the attribute is accessed through the owner.
6362
/// This method should return the (computed) attribute value or raise an AttributeError exception.
6463
/// </remarks>
65-
public static IntPtr tp_descr_get(IntPtr op, IntPtr instance, IntPtr owner)
64+
public static NewReference tp_descr_get(BorrowedReference op, BorrowedReference instance, BorrowedReference owner)
6665
{
67-
var self = (ConstructorBinding)GetManagedObject(op);
66+
var self = (ConstructorBinding?)GetManagedObject(op);
6867
if (self == null)
6968
{
70-
return IntPtr.Zero;
69+
Exceptions.SetError(Exceptions.AssertionError, "attempting to access destroyed object");
70+
return default;
7171
}
7272

7373
// It doesn't seem to matter if it's accessed through an instance (rather than via the type).
@@ -77,8 +77,7 @@ public static IntPtr tp_descr_get(IntPtr op, IntPtr instance, IntPtr owner)
7777
return Exceptions.RaiseTypeError("How in the world could that happen!");
7878
}
7979
}*/
80-
Runtime.XIncref(self.pyHandle);
81-
return self.pyHandle;
80+
return new NewReference(self.pyHandle);
8281
}
8382

8483
/// <summary>
@@ -89,16 +88,16 @@ public static IntPtr tp_descr_get(IntPtr op, IntPtr instance, IntPtr owner)
8988
/// Return element of o corresponding to the object key or NULL on failure.
9089
/// This is the equivalent of the Python expression o[key].
9190
/// </remarks>
92-
public static IntPtr mp_subscript(IntPtr op, IntPtr key)
91+
public static NewReference mp_subscript(BorrowedReference op, BorrowedReference key)
9392
{
94-
var self = (ConstructorBinding)GetManagedObject(op);
93+
var self = (ConstructorBinding)GetManagedObject(op)!;
9594
if (!self.type.Valid)
9695
{
9796
return Exceptions.RaiseTypeError(self.type.DeletedMessage);
9897
}
9998
Type tp = self.type.Value;
10099

101-
Type[] types = Runtime.PythonArgsToTypeArray(key);
100+
Type[]? types = Runtime.PythonArgsToTypeArray(key);
102101
if (types == null)
103102
{
104103
return Exceptions.RaiseTypeError("type(s) expected");
@@ -111,20 +110,18 @@ public static IntPtr mp_subscript(IntPtr op, IntPtr key)
111110
return Exceptions.RaiseTypeError("No match found for constructor signature");
112111
}
113112
var boundCtor = new BoundContructor(tp, self.typeToCreate, self.ctorBinder, ci);
114-
115-
return boundCtor.pyHandle;
113+
return new NewReference(boundCtor.pyHandle);
116114
}
117115

118116
/// <summary>
119117
/// ConstructorBinding __repr__ implementation [borrowed from MethodObject].
120118
/// </summary>
121-
public static IntPtr tp_repr(IntPtr ob)
119+
public static NewReference tp_repr(BorrowedReference ob)
122120
{
123-
var self = (ConstructorBinding)GetManagedObject(ob);
124-
if (self.repr != IntPtr.Zero)
121+
var self = (ConstructorBinding)GetManagedObject(ob)!;
122+
if (self.repr is not null)
125123
{
126-
Runtime.XIncref(self.repr);
127-
return self.repr;
124+
return new NewReference(self.repr);
128125
}
129126
MethodBase[] methods = self.ctorBinder.GetMethods();
130127

@@ -144,9 +141,10 @@ public static IntPtr tp_repr(IntPtr ob)
144141
int idx = str.IndexOf("(");
145142
doc += string.Format("{0}{1}", name, str.Substring(idx));
146143
}
147-
self.repr = Runtime.PyString_FromString(doc);
148-
Runtime.XIncref(self.repr);
149-
return self.repr;
144+
using var docStr = Runtime.PyString_FromString(doc);
145+
if (docStr.IsNull()) return default;
146+
self.repr = docStr.MoveToPyObject();
147+
return new NewReference(self.repr);
150148
}
151149

152150
protected override void Clear()
@@ -155,14 +153,17 @@ protected override void Clear()
155153
base.Clear();
156154
}
157155

158-
public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg)
156+
public static int tp_traverse(BorrowedReference ob, IntPtr visit, IntPtr arg)
159157
{
160-
var self = (ConstructorBinding)GetManagedObject(ob);
161-
int res = PyVisit(self.typeToCreate.Handle, visit, arg);
158+
var self = (ConstructorBinding)GetManagedObject(ob)!;
159+
int res = PyVisit(self.typeToCreate, visit, arg);
162160
if (res != 0) return res;
163161

164-
res = PyVisit(self.repr, visit, arg);
165-
if (res != 0) return res;
162+
if (self.repr is not null)
163+
{
164+
res = PyVisit(self.repr, visit, arg);
165+
if (res != 0) return res;
166+
}
166167
return 0;
167168
}
168169
}
@@ -182,15 +183,14 @@ internal class BoundContructor : ExtensionType
182183
private PyType typeToCreate; // The python type tells GetInstHandle which Type to create.
183184
private ConstructorBinder ctorBinder;
184185
private ConstructorInfo ctorInfo;
185-
private IntPtr repr;
186+
private PyObject? repr;
186187

187188
public BoundContructor(Type type, PyType typeToCreate, ConstructorBinder ctorBinder, ConstructorInfo ci)
188189
{
189190
this.type = type;
190191
this.typeToCreate = typeToCreate;
191192
this.ctorBinder = ctorBinder;
192193
ctorInfo = ci;
193-
repr = IntPtr.Zero;
194194
}
195195

196196
/// <summary>
@@ -200,45 +200,45 @@ public BoundContructor(Type type, PyType typeToCreate, ConstructorBinder ctorBin
200200
/// <param name="args"> PyObject *args </param>
201201
/// <param name="kw"> PyObject *kw </param>
202202
/// <returns> A reference to a new instance of the class by invoking the selected ctor(). </returns>
203-
public static IntPtr tp_call(IntPtr op, IntPtr args, IntPtr kw)
203+
public static NewReference tp_call(BorrowedReference op, BorrowedReference args, BorrowedReference kw)
204204
{
205-
var self = (BoundContructor)GetManagedObject(op);
205+
var self = (BoundContructor)GetManagedObject(op)!;
206206
// Even though a call with null ctorInfo just produces the old behavior
207207
/*if (self.ctorInfo == null) {
208208
string msg = "Usage: Class.Overloads[CLR_or_python_Type, ...]";
209209
return Exceptions.RaiseTypeError(msg);
210210
}*/
211211
// Bind using ConstructorBinder.Bind and invoke the ctor providing a null instancePtr
212212
// which will fire self.ctorInfo using ConstructorInfo.Invoke().
213-
object obj = self.ctorBinder.InvokeRaw(IntPtr.Zero, args, kw, self.ctorInfo);
213+
object? obj = self.ctorBinder.InvokeRaw(null, args, kw, self.ctorInfo);
214214
if (obj == null)
215215
{
216216
// XXX set an error
217-
return IntPtr.Zero;
217+
return default;
218218
}
219219
// Instantiate the python object that wraps the result of the method call
220220
// and return the PyObject* to it.
221-
return CLRObject.GetReference(obj, self.typeToCreate.Reference).DangerousMoveToPointer();
221+
return CLRObject.GetReference(obj, self.typeToCreate);
222222
}
223223

224224
/// <summary>
225225
/// BoundContructor __repr__ implementation [borrowed from MethodObject].
226226
/// </summary>
227-
public static IntPtr tp_repr(IntPtr ob)
227+
public static NewReference tp_repr(BorrowedReference ob)
228228
{
229-
var self = (BoundContructor)GetManagedObject(ob);
230-
if (self.repr != IntPtr.Zero)
229+
var self = (BoundContructor)GetManagedObject(ob)!;
230+
if (self.repr is not null)
231231
{
232-
Runtime.XIncref(self.repr);
233-
return self.repr;
232+
return new NewReference(self.repr);
234233
}
235234
string name = self.type.FullName;
236235
string str = self.ctorInfo.ToString();
237236
int idx = str.IndexOf("(");
238237
str = string.Format("returns a new {0}{1}", name, str.Substring(idx));
239-
self.repr = Runtime.PyString_FromString(str);
240-
Runtime.XIncref(self.repr);
241-
return self.repr;
238+
using var docStr = Runtime.PyString_FromString(str);
239+
if (docStr.IsNull()) return default;
240+
self.repr = docStr.MoveToPyObject();
241+
return new NewReference(self.repr);
242242
}
243243

244244
protected override void Clear()
@@ -247,14 +247,17 @@ protected override void Clear()
247247
base.Clear();
248248
}
249249

250-
public static int tp_traverse(IntPtr ob, IntPtr visit, IntPtr arg)
250+
public static int tp_traverse(BorrowedReference ob, IntPtr visit, IntPtr arg)
251251
{
252-
var self = (BoundContructor)GetManagedObject(ob);
253-
int res = PyVisit(self.typeToCreate.Handle, visit, arg);
252+
var self = (BoundContructor)GetManagedObject(ob)!;
253+
int res = PyVisit(self.typeToCreate, visit, arg);
254254
if (res != 0) return res;
255255

256-
res = PyVisit(self.repr, visit, arg);
257-
if (res != 0) return res;
256+
if (self.repr is not null)
257+
{
258+
res = PyVisit(self.repr, visit, arg);
259+
if (res != 0) return res;
260+
}
258261
return 0;
259262
}
260263
}

src/runtime/fieldobject.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,31 @@ public FieldObject(FieldInfo info)
2222
/// value of the field on the given object. The returned value
2323
/// is converted to an appropriately typed Python object.
2424
/// </summary>
25-
public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
25+
public static NewReference tp_descr_get(BorrowedReference ds, BorrowedReference ob, BorrowedReference tp)
2626
{
27-
var self = (FieldObject)GetManagedObject(ds);
27+
var self = (FieldObject?)GetManagedObject(ds);
2828
object result;
2929

3030
if (self == null)
3131
{
32-
return IntPtr.Zero;
32+
Exceptions.SetError(Exceptions.AssertionError, "attempting to access destroyed object");
33+
return default;
3334
}
3435
else if (!self.info.Valid)
3536
{
3637
Exceptions.SetError(Exceptions.AttributeError, self.info.DeletedMessage);
37-
return IntPtr.Zero;
38+
return default;
3839
}
3940

4041
FieldInfo info = self.info.Value;
4142

42-
if (ob == IntPtr.Zero || ob == Runtime.PyNone)
43+
if (ob == null || ob == Runtime.PyNone)
4344
{
4445
if (!info.IsStatic)
4546
{
4647
Exceptions.SetError(Exceptions.TypeError,
4748
"instance attribute must be accessed through a class instance");
48-
return IntPtr.Zero;
49+
return default;
4950
}
5051
try
5152
{
@@ -55,25 +56,25 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
5556
catch (Exception e)
5657
{
5758
Exceptions.SetError(Exceptions.TypeError, e.Message);
58-
return IntPtr.Zero;
59+
return default;
5960
}
6061
}
6162

6263
try
6364
{
64-
var co = (CLRObject)GetManagedObject(ob);
65+
var co = (CLRObject?)GetManagedObject(ob);
6566
if (co == null)
6667
{
6768
Exceptions.SetError(Exceptions.TypeError, "instance is not a clr object");
68-
return IntPtr.Zero;
69+
return default;
6970
}
7071
result = info.GetValue(co.inst);
7172
return Converter.ToPython(result, info.FieldType);
7273
}
7374
catch (Exception e)
7475
{
7576
Exceptions.SetError(Exceptions.TypeError, e.Message);
76-
return IntPtr.Zero;
77+
return default;
7778
}
7879
}
7980

@@ -82,13 +83,12 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
8283
/// a field based on the given Python value. The Python value must be
8384
/// convertible to the type of the field.
8485
/// </summary>
85-
public new static int tp_descr_set(IntPtr ds, IntPtr ob, IntPtr val)
86+
public static int tp_descr_set(BorrowedReference ds, BorrowedReference ob, BorrowedReference val)
8687
{
87-
var self = (FieldObject)GetManagedObject(ds);
88-
object newval;
89-
88+
var self = (FieldObject?)GetManagedObject(ds);
9089
if (self == null)
9190
{
91+
Exceptions.SetError(Exceptions.AssertionError, "attempting to access destroyed object");
9292
return -1;
9393
}
9494
else if (!self.info.Valid)
@@ -97,7 +97,7 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
9797
return -1;
9898
}
9999

100-
if (val == IntPtr.Zero)
100+
if (val == null)
101101
{
102102
Exceptions.SetError(Exceptions.TypeError, "cannot delete field");
103103
return -1;
@@ -113,7 +113,7 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
113113

114114
bool is_static = info.IsStatic;
115115

116-
if (ob == IntPtr.Zero || ob == Runtime.PyNone)
116+
if (ob == null || ob == Runtime.PyNone)
117117
{
118118
if (!is_static)
119119
{
@@ -122,7 +122,7 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
122122
}
123123
}
124124

125-
if (!Converter.ToManaged(val, info.FieldType, out newval, true))
125+
if (!Converter.ToManaged(val, info.FieldType, out var newval, true))
126126
{
127127
return -1;
128128
}
@@ -131,7 +131,7 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
131131
{
132132
if (!is_static)
133133
{
134-
var co = (CLRObject)GetManagedObject(ob);
134+
var co = (CLRObject?)GetManagedObject(ob);
135135
if (co == null)
136136
{
137137
Exceptions.SetError(Exceptions.TypeError, "instance is not a clr object");
@@ -155,9 +155,9 @@ public static IntPtr tp_descr_get(IntPtr ds, IntPtr ob, IntPtr tp)
155155
/// <summary>
156156
/// Descriptor __repr__ implementation.
157157
/// </summary>
158-
public static IntPtr tp_repr(IntPtr ob)
158+
public static NewReference tp_repr(BorrowedReference ob)
159159
{
160-
var self = (FieldObject)GetManagedObject(ob);
160+
var self = (FieldObject)GetManagedObject(ob)!;
161161
return Runtime.PyString_FromString($"<field '{self.info}'>");
162162
}
163163
}

0 commit comments

Comments
 (0)