Skip to content

Remove printing if Decref is called with NULL. Rename Decref/Incref to XDecref/XIncref #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/runtime/classbase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public virtual IntPtr type_subscript(IntPtr idx)
{
Type t = target.MakeGenericType(types);
ManagedType c = (ManagedType)ClassManager.GetClass(t);
Runtime.Incref(c.pyHandle);
Runtime.XIncref(c.pyHandle);
return c.pyHandle;
}

Expand All @@ -71,7 +71,7 @@ public virtual IntPtr type_subscript(IntPtr idx)
public static IntPtr tp_richcompare(IntPtr ob, IntPtr other, int op) {
if (op != Runtime.Py_EQ && op != Runtime.Py_NE)
{
Runtime.Incref(Runtime.PyNotImplemented);
Runtime.XIncref(Runtime.PyNotImplemented);
return Runtime.PyNotImplemented;
}

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

if (ob == other) {
Runtime.Incref(pytrue);
Runtime.XIncref(pytrue);
return pytrue;
}

CLRObject co1 = GetManagedObject(ob) as CLRObject;
CLRObject co2 = GetManagedObject(other) as CLRObject;
if (null == co2) {
Runtime.Incref(pyfalse);
Runtime.XIncref(pyfalse);
return pyfalse;
}

Object o1 = co1.inst;
Object o2 = co2.inst;

if (Object.Equals(o1, o2)) {
Runtime.Incref(pytrue);
Runtime.XIncref(pytrue);
return pytrue;
}

Runtime.Incref(pyfalse);
Runtime.XIncref(pyfalse);
return pyfalse;
}
#else
Expand Down Expand Up @@ -237,11 +237,11 @@ public static void tp_dealloc(IntPtr ob)
IntPtr dict = Marshal.ReadIntPtr(ob, ObjectOffset.DictOffset(ob));
if (dict != IntPtr.Zero)
{
Runtime.Decref(dict);
Runtime.XDecref(dict);
}
Runtime.PyObject_GC_UnTrack(self.pyHandle);
Runtime.PyObject_GC_Del(self.pyHandle);
Runtime.Decref(self.tpHandle);
Runtime.XDecref(self.tpHandle);
self.gcHandle.Free();
}
}
Expand Down
26 changes: 13 additions & 13 deletions src/runtime/classderived.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ internal static IntPtr ToPython(IPythonDerivedType obj)
FieldInfo fi = obj.GetType().GetField("__pyobj__");
CLRObject self = (CLRObject)fi.GetValue(obj);

Runtime.Incref(self.pyHandle);
Runtime.XIncref(self.pyHandle);

// when the C# constructor creates the python object it starts as a weak
// reference with a reference count of 0. Now we're passing this object
Expand Down Expand Up @@ -153,7 +153,7 @@ internal static Type CreateDerivedType(string name,
HashSet<string> pyProperties = new HashSet<string>();
if (py_dict != IntPtr.Zero && Runtime.PyDict_Check(py_dict))
{
Runtime.Incref(py_dict);
Runtime.XIncref(py_dict);
using (PyDict dict = new PyDict(py_dict))
using (PyObject keys = dict.Keys())
{
Expand Down Expand Up @@ -196,7 +196,7 @@ internal static Type CreateDerivedType(string name,
// Add any additional methods and properties explicitly exposed from Python.
if (py_dict != IntPtr.Zero && Runtime.PyDict_Check(py_dict))
{
Runtime.Incref(py_dict);
Runtime.XIncref(py_dict);
using (PyDict dict = new PyDict(py_dict))
using (PyObject keys = dict.Keys())
{
Expand Down Expand Up @@ -588,11 +588,11 @@ public static T InvokeMethod<T>(IPythonDerivedType obj, string methodName, strin
IntPtr gs = Runtime.PyGILState_Ensure();
try
{
Runtime.Incref(self.pyHandle);
Runtime.XIncref(self.pyHandle);
PyObject pyself = new PyObject(self.pyHandle);
disposeList.Add(pyself);

Runtime.Incref(Runtime.PyNone);
Runtime.XIncref(Runtime.PyNone);
PyObject pynone = new PyObject(Runtime.PyNone);
disposeList.Add(pynone);

Expand Down Expand Up @@ -649,11 +649,11 @@ public static void InvokeMethodVoid(IPythonDerivedType obj, string methodName, s
IntPtr gs = Runtime.PyGILState_Ensure();
try
{
Runtime.Incref(self.pyHandle);
Runtime.XIncref(self.pyHandle);
PyObject pyself = new PyObject(self.pyHandle);
disposeList.Add(pyself);

Runtime.Incref(Runtime.PyNone);
Runtime.XIncref(Runtime.PyNone);
PyObject pynone = new PyObject(Runtime.PyNone);
disposeList.Add(pynone);

Expand Down Expand Up @@ -710,7 +710,7 @@ public static T InvokeGetProperty<T>(IPythonDerivedType obj, string propertyName
IntPtr gs = Runtime.PyGILState_Ensure();
try
{
Runtime.Incref(self.pyHandle);
Runtime.XIncref(self.pyHandle);
using (PyObject pyself = new PyObject(self.pyHandle))
using (PyObject pyvalue = pyself.GetAttr(propertyName))
return (T)pyvalue.AsManagedObject(typeof(T));
Expand All @@ -732,7 +732,7 @@ public static void InvokeSetProperty<T>(IPythonDerivedType obj, string propertyN
IntPtr gs = Runtime.PyGILState_Ensure();
try
{
Runtime.Incref(self.pyHandle);
Runtime.XIncref(self.pyHandle);
using (PyObject pyself = new PyObject(self.pyHandle))
using (PyObject pyvalue = new PyObject(Converter.ToPythonImplicit(value)))
pyself.SetAttr(propertyName, pyvalue);
Expand Down Expand Up @@ -766,11 +766,11 @@ public static void InvokeCtor(IPythonDerivedType obj, string origCtorName, Objec
FieldInfo fi = obj.GetType().GetField("__pyobj__");
fi.SetValue(obj, self);

Runtime.Incref(self.pyHandle);
Runtime.XIncref(self.pyHandle);
PyObject pyself = new PyObject(self.pyHandle);
disposeList.Add(pyself);

Runtime.Incref(Runtime.PyNone);
Runtime.XIncref(Runtime.PyNone);
PyObject pynone = new PyObject(Runtime.PyNone);
disposeList.Add(pynone);

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

Runtime.PyGILState_Release(gs);
}
Expand Down Expand Up @@ -848,7 +848,7 @@ public static void Finalize(IPythonDerivedType obj)
// python object.
IntPtr dict = Marshal.ReadIntPtr(self.pyHandle, ObjectOffset.DictOffset(self.pyHandle));
if (dict != IntPtr.Zero)
Runtime.Decref(dict);
Runtime.XDecref(dict);
Runtime.PyObject_GC_Del(self.pyHandle);
self.gcHandle.Free();
}
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/classmanager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private static void InitClassBase(Type type, ClassBase impl)
string docStr = attr.DocString;
doc = Runtime.PyString_FromString(docStr);
Runtime.PyDict_SetItemString(dict, "__doc__", doc);
Runtime.Decref(doc);
Runtime.XDecref(doc);
}

ClassObject co = impl as ClassObject;
Expand All @@ -185,7 +185,7 @@ private static void InitClassBase(Type type, ClassBase impl)
{
doc = co.GetDocString();
Runtime.PyDict_SetItemString(dict, "__doc__", doc);
Runtime.Decref(doc);
Runtime.XDecref(doc);
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/runtime/classobject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public override IntPtr type_subscript(IntPtr idx)
}
Type a = t.MakeArrayType();
ClassBase o = ClassManager.GetClass(a);
Runtime.Incref(o.pyHandle);
Runtime.XIncref(o.pyHandle);
return o.pyHandle;
}

Expand All @@ -159,7 +159,7 @@ public override IntPtr type_subscript(IntPtr idx)
{
GenericType g = ClassManager.GetClass(gtype) as GenericType;
return g.type_subscript(idx);
/*Runtime.Incref(g.pyHandle);
/*Runtime.XIncref(g.pyHandle);
return g.pyHandle;*/
}
return Exceptions.RaiseTypeError("unsubscriptable object");
Expand Down Expand Up @@ -194,7 +194,7 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
if (!Runtime.PyTuple_Check(idx))
{
args = Runtime.PyTuple_New(1);
Runtime.Incref(idx);
Runtime.XIncref(idx);
Runtime.PyTuple_SetItem(args, 0, idx);
free = true;
}
Expand All @@ -209,7 +209,7 @@ public static IntPtr mp_subscript(IntPtr ob, IntPtr idx)
{
if (free)
{
Runtime.Decref(args);
Runtime.XDecref(args);
}
}
return value;
Expand Down Expand Up @@ -243,7 +243,7 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
if (!Runtime.PyTuple_Check(idx))
{
args = Runtime.PyTuple_New(1);
Runtime.Incref(idx);
Runtime.XIncref(idx);
Runtime.PyTuple_SetItem(args, 0, idx);
free = true;
}
Expand All @@ -257,23 +257,23 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
for (int n = 0; n < i; n++)
{
IntPtr item = Runtime.PyTuple_GetItem(args, n);
Runtime.Incref(item);
Runtime.XIncref(item);
Runtime.PyTuple_SetItem(real, n, item);
}

// Add Default Args if needed
for (int n = 0; n < numOfDefaultArgs; n++)
{
IntPtr item = Runtime.PyTuple_GetItem(defaultArgs, n);
Runtime.Incref(item);
Runtime.XIncref(item);
Runtime.PyTuple_SetItem(real, n + i, item);
}
// no longer need defaultArgs
Runtime.Decref(defaultArgs);
Runtime.XDecref(defaultArgs);
i = temp;

// Add value to argument list
Runtime.Incref(v);
Runtime.XIncref(v);
Runtime.PyTuple_SetItem(real, i, v);

try
Expand All @@ -282,11 +282,11 @@ public static int mp_ass_subscript(IntPtr ob, IntPtr idx, IntPtr v)
}
finally
{
Runtime.Decref(real);
Runtime.XDecref(real);

if (free)
{
Runtime.Decref(args);
Runtime.XDecref(args);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/constructorbinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ internal object InvokeRaw(IntPtr inst, IntPtr args, IntPtr kw,

IntPtr eargs = Runtime.PyTuple_New(0);
binding = this.Bind(inst, eargs, kw);
Runtime.Decref(eargs);
Runtime.XDecref(eargs);

if (binding == null)
{
Expand Down
24 changes: 12 additions & 12 deletions src/runtime/constructorbinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal class ConstructorBinding : ExtensionType
public ConstructorBinding(Type type, IntPtr pyTypeHndl, ConstructorBinder ctorBinder) : base()
{
this.type = type;
Runtime.Incref(pyTypeHndl);
Runtime.XIncref(pyTypeHndl);
this.pyTypeHndl = pyTypeHndl;
this.ctorBinder = ctorBinder;
repr = IntPtr.Zero;
Expand Down Expand Up @@ -73,7 +73,7 @@ public static IntPtr tp_descr_get(IntPtr op, IntPtr instance, IntPtr owner)
return Exceptions.RaiseTypeError("How in the world could that happen!");
}
}*/
Runtime.Incref(self.pyHandle); // Decref'd by the interpreter.
Runtime.XIncref(self.pyHandle); // Decref'd by the interpreter.
return self.pyHandle;
}

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

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

Expand All @@ -121,7 +121,7 @@ public static IntPtr tp_repr(IntPtr ob)
ConstructorBinding self = (ConstructorBinding)GetManagedObject(ob);
if (self.repr != IntPtr.Zero)
{
Runtime.Incref(self.repr);
Runtime.XIncref(self.repr);
return self.repr;
}
MethodBase[] methods = self.ctorBinder.GetMethods();
Expand All @@ -136,7 +136,7 @@ public static IntPtr tp_repr(IntPtr ob)
doc += String.Format("{0}{1}", name, str.Substring(idx));
}
self.repr = Runtime.PyString_FromString(doc);
Runtime.Incref(self.repr);
Runtime.XIncref(self.repr);
return self.repr;
}

Expand All @@ -147,8 +147,8 @@ public static IntPtr tp_repr(IntPtr ob)
public static new void tp_dealloc(IntPtr ob)
{
ConstructorBinding self = (ConstructorBinding)GetManagedObject(ob);
Runtime.Decref(self.repr);
Runtime.Decref(self.pyTypeHndl);
Runtime.XDecref(self.repr);
Runtime.XDecref(self.pyTypeHndl);
ExtensionType.FinalizeObject(self);
}
}
Expand All @@ -173,7 +173,7 @@ public BoundContructor(Type type, IntPtr pyTypeHndl, ConstructorBinder ctorBinde
: base()
{
this.type = type;
Runtime.Incref(pyTypeHndl);
Runtime.XIncref(pyTypeHndl);
this.pyTypeHndl = pyTypeHndl;
this.ctorBinder = ctorBinder;
ctorInfo = ci;
Expand Down Expand Up @@ -217,15 +217,15 @@ public static IntPtr tp_repr(IntPtr ob)
BoundContructor self = (BoundContructor)GetManagedObject(ob);
if (self.repr != IntPtr.Zero)
{
Runtime.Incref(self.repr);
Runtime.XIncref(self.repr);
return self.repr;
}
string name = self.type.FullName;
string str = self.ctorInfo.ToString();
int idx = str.IndexOf("(");
str = String.Format("returns a new {0}{1}", name, str.Substring(idx));
self.repr = Runtime.PyString_FromString(str);
Runtime.Incref(self.repr);
Runtime.XIncref(self.repr);
return self.repr;
}

Expand All @@ -236,8 +236,8 @@ public static IntPtr tp_repr(IntPtr ob)
public static new void tp_dealloc(IntPtr ob)
{
BoundContructor self = (BoundContructor)GetManagedObject(ob);
Runtime.Decref(self.repr);
Runtime.Decref(self.pyTypeHndl);
Runtime.XDecref(self.repr);
Runtime.XDecref(self.pyTypeHndl);
ExtensionType.FinalizeObject(self);
}
}
Expand Down
Loading