Skip to content

Commit 86937bb

Browse files
ArvidJBdenfromufa
authored and
denfromufa
committed
Remove printing if Decref is called with NULL. Rename Decref/Incref to XDecref/XIncref (#275)
* Rename Runtime.Incref/Decref to XIncref/XDecref to signal that they check for NULL * Remove NULL check debug print in Decref * Remove added NULL checks for self.repr in constructorbinding.cs * Add missing changes for Python 2
1 parent 1a2499b commit 86937bb

36 files changed

+232
-234
lines changed

src/runtime/classbase.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public virtual IntPtr type_subscript(IntPtr idx)
5757
{
5858
Type t = target.MakeGenericType(types);
5959
ManagedType c = (ManagedType)ClassManager.GetClass(t);
60-
Runtime.Incref(c.pyHandle);
60+
Runtime.XIncref(c.pyHandle);
6161
return c.pyHandle;
6262
}
6363

@@ -71,7 +71,7 @@ public virtual IntPtr type_subscript(IntPtr idx)
7171
public static IntPtr tp_richcompare(IntPtr ob, IntPtr other, int op) {
7272
if (op != Runtime.Py_EQ && op != Runtime.Py_NE)
7373
{
74-
Runtime.Incref(Runtime.PyNotImplemented);
74+
Runtime.XIncref(Runtime.PyNotImplemented);
7575
return Runtime.PyNotImplemented;
7676
}
7777

@@ -86,26 +86,26 @@ public static IntPtr tp_richcompare(IntPtr ob, IntPtr other, int op) {
8686
}
8787

8888
if (ob == other) {
89-
Runtime.Incref(pytrue);
89+
Runtime.XIncref(pytrue);
9090
return pytrue;
9191
}
9292

9393
CLRObject co1 = GetManagedObject(ob) as CLRObject;
9494
CLRObject co2 = GetManagedObject(other) as CLRObject;
9595
if (null == co2) {
96-
Runtime.Incref(pyfalse);
96+
Runtime.XIncref(pyfalse);
9797
return pyfalse;
9898
}
9999

100100
Object o1 = co1.inst;
101101
Object o2 = co2.inst;
102102

103103
if (Object.Equals(o1, o2)) {
104-
Runtime.Incref(pytrue);
104+
Runtime.XIncref(pytrue);
105105
return pytrue;
106106
}
107107

108-
Runtime.Incref(pyfalse);
108+
Runtime.XIncref(pyfalse);
109109
return pyfalse;
110110
}
111111
#else
@@ -237,11 +237,11 @@ public static void tp_dealloc(IntPtr ob)
237237
IntPtr dict = Marshal.ReadIntPtr(ob, ObjectOffset.DictOffset(ob));
238238
if (dict != IntPtr.Zero)
239239
{
240-
Runtime.Decref(dict);
240+
Runtime.XDecref(dict);
241241
}
242242
Runtime.PyObject_GC_UnTrack(self.pyHandle);
243243
Runtime.PyObject_GC_Del(self.pyHandle);
244-
Runtime.Decref(self.tpHandle);
244+
Runtime.XDecref(self.tpHandle);
245245
self.gcHandle.Free();
246246
}
247247
}

src/runtime/classderived.cs

+13-13
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ internal static IntPtr ToPython(IPythonDerivedType obj)
8181
FieldInfo fi = obj.GetType().GetField("__pyobj__");
8282
CLRObject self = (CLRObject)fi.GetValue(obj);
8383

84-
Runtime.Incref(self.pyHandle);
84+
Runtime.XIncref(self.pyHandle);
8585

8686
// when the C# constructor creates the python object it starts as a weak
8787
// reference with a reference count of 0. Now we're passing this object
@@ -153,7 +153,7 @@ internal static Type CreateDerivedType(string name,
153153
HashSet<string> pyProperties = new HashSet<string>();
154154
if (py_dict != IntPtr.Zero && Runtime.PyDict_Check(py_dict))
155155
{
156-
Runtime.Incref(py_dict);
156+
Runtime.XIncref(py_dict);
157157
using (PyDict dict = new PyDict(py_dict))
158158
using (PyObject keys = dict.Keys())
159159
{
@@ -196,7 +196,7 @@ internal static Type CreateDerivedType(string name,
196196
// Add any additional methods and properties explicitly exposed from Python.
197197
if (py_dict != IntPtr.Zero && Runtime.PyDict_Check(py_dict))
198198
{
199-
Runtime.Incref(py_dict);
199+
Runtime.XIncref(py_dict);
200200
using (PyDict dict = new PyDict(py_dict))
201201
using (PyObject keys = dict.Keys())
202202
{
@@ -588,11 +588,11 @@ public static T InvokeMethod<T>(IPythonDerivedType obj, string methodName, strin
588588
IntPtr gs = Runtime.PyGILState_Ensure();
589589
try
590590
{
591-
Runtime.Incref(self.pyHandle);
591+
Runtime.XIncref(self.pyHandle);
592592
PyObject pyself = new PyObject(self.pyHandle);
593593
disposeList.Add(pyself);
594594

595-
Runtime.Incref(Runtime.PyNone);
595+
Runtime.XIncref(Runtime.PyNone);
596596
PyObject pynone = new PyObject(Runtime.PyNone);
597597
disposeList.Add(pynone);
598598

@@ -649,11 +649,11 @@ public static void InvokeMethodVoid(IPythonDerivedType obj, string methodName, s
649649
IntPtr gs = Runtime.PyGILState_Ensure();
650650
try
651651
{
652-
Runtime.Incref(self.pyHandle);
652+
Runtime.XIncref(self.pyHandle);
653653
PyObject pyself = new PyObject(self.pyHandle);
654654
disposeList.Add(pyself);
655655

656-
Runtime.Incref(Runtime.PyNone);
656+
Runtime.XIncref(Runtime.PyNone);
657657
PyObject pynone = new PyObject(Runtime.PyNone);
658658
disposeList.Add(pynone);
659659

@@ -710,7 +710,7 @@ public static T InvokeGetProperty<T>(IPythonDerivedType obj, string propertyName
710710
IntPtr gs = Runtime.PyGILState_Ensure();
711711
try
712712
{
713-
Runtime.Incref(self.pyHandle);
713+
Runtime.XIncref(self.pyHandle);
714714
using (PyObject pyself = new PyObject(self.pyHandle))
715715
using (PyObject pyvalue = pyself.GetAttr(propertyName))
716716
return (T)pyvalue.AsManagedObject(typeof(T));
@@ -732,7 +732,7 @@ public static void InvokeSetProperty<T>(IPythonDerivedType obj, string propertyN
732732
IntPtr gs = Runtime.PyGILState_Ensure();
733733
try
734734
{
735-
Runtime.Incref(self.pyHandle);
735+
Runtime.XIncref(self.pyHandle);
736736
using (PyObject pyself = new PyObject(self.pyHandle))
737737
using (PyObject pyvalue = new PyObject(Converter.ToPythonImplicit(value)))
738738
pyself.SetAttr(propertyName, pyvalue);
@@ -766,11 +766,11 @@ public static void InvokeCtor(IPythonDerivedType obj, string origCtorName, Objec
766766
FieldInfo fi = obj.GetType().GetField("__pyobj__");
767767
fi.SetValue(obj, self);
768768

769-
Runtime.Incref(self.pyHandle);
769+
Runtime.XIncref(self.pyHandle);
770770
PyObject pyself = new PyObject(self.pyHandle);
771771
disposeList.Add(pyself);
772772

773-
Runtime.Incref(Runtime.PyNone);
773+
Runtime.XIncref(Runtime.PyNone);
774774
PyObject pynone = new PyObject(Runtime.PyNone);
775775
disposeList.Add(pynone);
776776

@@ -806,7 +806,7 @@ public static void InvokeCtor(IPythonDerivedType obj, string origCtorName, Objec
806806
// This doesn't actually destroy the object, it just sets the reference to this object
807807
// to be a weak reference and it will be destroyed when the C# object is destroyed.
808808
if (null != self)
809-
Runtime.Decref(self.pyHandle);
809+
Runtime.XDecref(self.pyHandle);
810810

811811
Runtime.PyGILState_Release(gs);
812812
}
@@ -848,7 +848,7 @@ public static void Finalize(IPythonDerivedType obj)
848848
// python object.
849849
IntPtr dict = Marshal.ReadIntPtr(self.pyHandle, ObjectOffset.DictOffset(self.pyHandle));
850850
if (dict != IntPtr.Zero)
851-
Runtime.Decref(dict);
851+
Runtime.XDecref(dict);
852852
Runtime.PyObject_GC_Del(self.pyHandle);
853853
self.gcHandle.Free();
854854
}

src/runtime/classmanager.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private static void InitClassBase(Type type, ClassBase impl)
160160
string docStr = attr.DocString;
161161
doc = Runtime.PyString_FromString(docStr);
162162
Runtime.PyDict_SetItemString(dict, "__doc__", doc);
163-
Runtime.Decref(doc);
163+
Runtime.XDecref(doc);
164164
}
165165

166166
ClassObject co = impl as ClassObject;
@@ -185,7 +185,7 @@ private static void InitClassBase(Type type, ClassBase impl)
185185
{
186186
doc = co.GetDocString();
187187
Runtime.PyDict_SetItemString(dict, "__doc__", doc);
188-
Runtime.Decref(doc);
188+
Runtime.XDecref(doc);
189189
}
190190
}
191191
}

src/runtime/classobject.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public override IntPtr type_subscript(IntPtr idx)
139139
}
140140
Type a = t.MakeArrayType();
141141
ClassBase o = ClassManager.GetClass(a);
142-
Runtime.Incref(o.pyHandle);
142+
Runtime.XIncref(o.pyHandle);
143143
return o.pyHandle;
144144
}
145145

@@ -159,7 +159,7 @@ public override IntPtr type_subscript(IntPtr idx)
159159
{
160160
GenericType g = ClassManager.GetClass(gtype) as GenericType;
161161
return g.type_subscript(idx);
162-
/*Runtime.Incref(g.pyHandle);
162+
/*Runtime.XIncref(g.pyHandle);
163163
return g.pyHandle;*/
164164
}
165165
return Exceptions.RaiseTypeError("unsubscriptable object");
@@ -194,7 +194,7 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
194194
if (!Runtime.PyTuple_Check(idx))
195195
{
196196
args = Runtime.PyTuple_New(1);
197-
Runtime.Incref(idx);
197+
Runtime.XIncref(idx);
198198
Runtime.PyTuple_SetItem(args, 0, idx);
199199
free = true;
200200
}
@@ -209,7 +209,7 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
209209
{
210210
if (free)
211211
{
212-
Runtime.Decref(args);
212+
Runtime.XDecref(args);
213213
}
214214
}
215215
return value;
@@ -243,7 +243,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
243243
if (!Runtime.PyTuple_Check(idx))
244244
{
245245
args = Runtime.PyTuple_New(1);
246-
Runtime.Incref(idx);
246+
Runtime.XIncref(idx);
247247
Runtime.PyTuple_SetItem(args, 0, idx);
248248
free = true;
249249
}
@@ -257,23 +257,23 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
257257
for (int n = 0; n < i; n++)
258258
{
259259
IntPtr item = Runtime.PyTuple_GetItem(args, n);
260-
Runtime.Incref(item);
260+
Runtime.XIncref(item);
261261
Runtime.PyTuple_SetItem(real, n, item);
262262
}
263263

264264
// Add Default Args if needed
265265
for (int n = 0; n < numOfDefaultArgs; n++)
266266
{
267267
IntPtr item = Runtime.PyTuple_GetItem(defaultArgs, n);
268-
Runtime.Incref(item);
268+
Runtime.XIncref(item);
269269
Runtime.PyTuple_SetItem(real, n + i, item);
270270
}
271271
// no longer need defaultArgs
272-
Runtime.Decref(defaultArgs);
272+
Runtime.XDecref(defaultArgs);
273273
i = temp;
274274

275275
// Add value to argument list
276-
Runtime.Incref(v);
276+
Runtime.XIncref(v);
277277
Runtime.PyTuple_SetItem(real, i, v);
278278

279279
try
@@ -282,11 +282,11 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
282282
}
283283
finally
284284
{
285-
Runtime.Decref(real);
285+
Runtime.XDecref(real);
286286

287287
if (free)
288288
{
289-
Runtime.Decref(args);
289+
Runtime.XDecref(args);
290290
}
291291
}
292292

src/runtime/constructorbinder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ internal object InvokeRaw(IntPtr inst, IntPtr args, IntPtr kw,
9292

9393
IntPtr eargs = Runtime.PyTuple_New(0);
9494
binding = this.Bind(inst, eargs, kw);
95-
Runtime.Decref(eargs);
95+
Runtime.XDecref(eargs);
9696

9797
if (binding == null)
9898
{

src/runtime/constructorbinding.cs

+12-12
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal class ConstructorBinding : ExtensionType
3030
public ConstructorBinding(Type type, IntPtr pyTypeHndl, ConstructorBinder ctorBinder) : base()
3131
{
3232
this.type = type;
33-
Runtime.Incref(pyTypeHndl);
33+
Runtime.XIncref(pyTypeHndl);
3434
this.pyTypeHndl = pyTypeHndl;
3535
this.ctorBinder = ctorBinder;
3636
repr = IntPtr.Zero;
@@ -73,7 +73,7 @@ public static IntPtr tp_descr_get(IntPtr op, IntPtr instance, IntPtr owner)
7373
return Exceptions.RaiseTypeError("How in the world could that happen!");
7474
}
7575
}*/
76-
Runtime.Incref(self.pyHandle); // Decref'd by the interpreter.
76+
Runtime.XIncref(self.pyHandle); // Decref'd by the interpreter.
7777
return self.pyHandle;
7878
}
7979

@@ -108,7 +108,7 @@ public static IntPtr mp_subscript(IntPtr op, IntPtr key)
108108
BoundContructor boundCtor = new BoundContructor(self.type, self.pyTypeHndl, self.ctorBinder, ci);
109109

110110
/* Since nothing's chached, do we need the increment???
111-
Runtime.Incref(boundCtor.pyHandle); // Decref'd by the interpreter??? */
111+
Runtime.XIncref(boundCtor.pyHandle); // Decref'd by the interpreter??? */
112112
return boundCtor.pyHandle;
113113
}
114114

@@ -121,7 +121,7 @@ public static IntPtr tp_repr(IntPtr ob)
121121
ConstructorBinding self = (ConstructorBinding)GetManagedObject(ob);
122122
if (self.repr != IntPtr.Zero)
123123
{
124-
Runtime.Incref(self.repr);
124+
Runtime.XIncref(self.repr);
125125
return self.repr;
126126
}
127127
MethodBase[] methods = self.ctorBinder.GetMethods();
@@ -136,7 +136,7 @@ public static IntPtr tp_repr(IntPtr ob)
136136
doc += String.Format("{0}{1}", name, str.Substring(idx));
137137
}
138138
self.repr = Runtime.PyString_FromString(doc);
139-
Runtime.Incref(self.repr);
139+
Runtime.XIncref(self.repr);
140140
return self.repr;
141141
}
142142

@@ -147,8 +147,8 @@ public static IntPtr tp_repr(IntPtr ob)
147147
public static new void tp_dealloc(IntPtr ob)
148148
{
149149
ConstructorBinding self = (ConstructorBinding)GetManagedObject(ob);
150-
Runtime.Decref(self.repr);
151-
Runtime.Decref(self.pyTypeHndl);
150+
Runtime.XDecref(self.repr);
151+
Runtime.XDecref(self.pyTypeHndl);
152152
ExtensionType.FinalizeObject(self);
153153
}
154154
}
@@ -173,7 +173,7 @@ public BoundContructor(Type type, IntPtr pyTypeHndl, ConstructorBinder ctorBinde
173173
: base()
174174
{
175175
this.type = type;
176-
Runtime.Incref(pyTypeHndl);
176+
Runtime.XIncref(pyTypeHndl);
177177
this.pyTypeHndl = pyTypeHndl;
178178
this.ctorBinder = ctorBinder;
179179
ctorInfo = ci;
@@ -217,15 +217,15 @@ public static IntPtr tp_repr(IntPtr ob)
217217
BoundContructor self = (BoundContructor)GetManagedObject(ob);
218218
if (self.repr != IntPtr.Zero)
219219
{
220-
Runtime.Incref(self.repr);
220+
Runtime.XIncref(self.repr);
221221
return self.repr;
222222
}
223223
string name = self.type.FullName;
224224
string str = self.ctorInfo.ToString();
225225
int idx = str.IndexOf("(");
226226
str = String.Format("returns a new {0}{1}", name, str.Substring(idx));
227227
self.repr = Runtime.PyString_FromString(str);
228-
Runtime.Incref(self.repr);
228+
Runtime.XIncref(self.repr);
229229
return self.repr;
230230
}
231231

@@ -236,8 +236,8 @@ public static IntPtr tp_repr(IntPtr ob)
236236
public static new void tp_dealloc(IntPtr ob)
237237
{
238238
BoundContructor self = (BoundContructor)GetManagedObject(ob);
239-
Runtime.Decref(self.repr);
240-
Runtime.Decref(self.pyTypeHndl);
239+
Runtime.XDecref(self.repr);
240+
Runtime.XDecref(self.pyTypeHndl);
241241
ExtensionType.FinalizeObject(self);
242242
}
243243
}

0 commit comments

Comments
 (0)