[Zope-CVS] CVS: PythonNet/src/runtime - CLRObject.cs:1.2 Exceptions.cs:1.4 ImportHook.cs:1.4 MetaType.cs:1.3 Runtime.cs:1.4

Brian Lloyd brian@zope.com
Fri, 1 Aug 2003 19:56:17 -0400


Update of /cvs-repository/PythonNet/src/runtime
In directory cvs.zope.org:/tmp/cvs-serv27190/src/runtime

Modified Files:
	CLRObject.cs Exceptions.cs ImportHook.cs MetaType.cs 
	Runtime.cs 
Log Message:
Tests all pass after fixing some refcounting bugs (still a bad gc interaction 
though). Probably enough progress to make a preview-2 release.


=== PythonNet/src/runtime/CLRObject.cs 1.1 => 1.2 ===
--- PythonNet/src/runtime/CLRObject.cs:1.1	Mon Jul 14 15:59:51 2003
+++ PythonNet/src/runtime/CLRObject.cs	Fri Aug  1 19:56:10 2003
@@ -25,7 +25,7 @@
 	    PyObjectHead pyObj = new PyObjectHead();
 	    this.gcHandle = GCHandle.Alloc(this);
 	    pyObj.ob_data = (IntPtr) this.gcHandle;
-	    pyObj.ob_refcnt = (IntPtr) 0;
+	    pyObj.ob_refcnt = (IntPtr) 0; // rethink!
 	    pyObj.ob_type = pyType;
 	    this.tpHandle = pyType;
 	    Runtime.Incref(pyType); //??


=== PythonNet/src/runtime/Exceptions.cs 1.3 => 1.4 ===
--- PythonNet/src/runtime/Exceptions.cs:1.3	Wed Jul 30 09:55:50 2003
+++ PythonNet/src/runtime/Exceptions.cs	Fri Aug  1 19:56:10 2003
@@ -34,7 +34,8 @@
 						    BindingFlags.Static)) {
 		IntPtr op = Runtime.PyObject_GetAttrString(module, fi.Name);
 		if (op != IntPtr.Zero) {
-		    Runtime.Incref(op);
+
+		    // Note we own a reference to each exception class.
 		    fi.SetValue(type, op);
 		}
 	    }
@@ -129,13 +130,6 @@
 
 	    PythonException err = e as PythonException;
 	    if (err != null) {
-		if (err.Type == IntPtr.Zero) {
-		    Console.WriteLine("null!");
-		}
-		if (err.Value == IntPtr.Zero) {
-		    Console.WriteLine("null!");
-		}
-
 		Runtime.PyErr_SetObject(err.Type, err.Value);
 		return;
 	    }


=== PythonNet/src/runtime/ImportHook.cs 1.3 => 1.4 ===
--- PythonNet/src/runtime/ImportHook.cs:1.3	Fri Aug  1 10:30:13 2003
+++ PythonNet/src/runtime/ImportHook.cs	Fri Aug  1 19:56:10 2003
@@ -33,7 +33,7 @@
 	    IntPtr dict = Runtime.PyImport_GetModuleDict();
 	    IntPtr mod = Runtime.PyDict_GetItemString(dict, "__builtin__");
 	    py_import = Runtime.PyObject_GetAttrString(mod, "__import__");
-	    Runtime.Incref(py_import);
+
 
 	    // should never die
 	    hook = new StaticMethodWrapper(


=== PythonNet/src/runtime/MetaType.cs 1.2 => 1.3 ===
--- PythonNet/src/runtime/MetaType.cs:1.2	Wed Jul 30 10:04:33 2003
+++ PythonNet/src/runtime/MetaType.cs	Fri Aug  1 19:56:10 2003
@@ -52,8 +52,12 @@
 
 	    PyTypeObject pyTypeObj = new PyTypeObject();
 	    pyTypeObj.tp_name = Marshal.StringToHGlobalAnsi(implType.Name);
+
 	    pyTypeObj.ob_type = Runtime.PyTypeType;
+	    Runtime.Incref(Runtime.PyTypeType);
+
 	    pyTypeObj.tp_base = Runtime.PyTypeType;
+	    Runtime.Incref(Runtime.PyTypeType);
 
 	    int typeSize = Marshal.SizeOf(typeof(PyTypeObject));
 	    pyTypeObj.tp_basicsize = (IntPtr)typeSize;


=== PythonNet/src/runtime/Runtime.cs 1.3 => 1.4 ===
--- PythonNet/src/runtime/Runtime.cs:1.3	Fri Aug  1 10:30:13 2003
+++ PythonNet/src/runtime/Runtime.cs	Fri Aug  1 19:56:10 2003
@@ -37,41 +37,41 @@
 	    IntPtr dict = Runtime.PyImport_GetModuleDict();
 	    IntPtr op = Runtime.PyDict_GetItemString(dict, "__builtin__");
 
-	    PyModuleType = Runtime.PyObject_Type(op);
-	    PyNone = Runtime.PyObject_GetAttrString(op, "None");
-	    PyNoneType = Runtime.PyObject_Type(PyNone);
-	    PyTypeType = Runtime.PyObject_Type(PyNoneType);
+	    PyModuleType = Runtime.PyObject_Type(op); 
+	    PyNone = Runtime.PyObject_GetAttrString(op, "None"); 
+	    PyNoneType = Runtime.PyObject_Type(PyNone); 
+	    PyTypeType = Runtime.PyObject_Type(PyNoneType); 
 
 	    op = Runtime.PyString_FromString("string");
-	    PyStringType = Runtime.PyObject_Type(op);
+	    PyStringType = Runtime.PyObject_Type(op); 
 	    Runtime.Decref(op);
 
 	    op = Runtime.PyUnicode_FromString("unicode");
-	    PyUnicodeType = Runtime.PyObject_Type(op);
+	    PyUnicodeType = Runtime.PyObject_Type(op); 
 	    Runtime.Decref(op);
 
 	    op = Runtime.PyTuple_New(0);
-	    PyTupleType = Runtime.PyObject_Type(op);
+	    PyTupleType = Runtime.PyObject_Type(op); 
 	    Runtime.Decref(op);
 
 	    op = Runtime.PyList_New(0);
-	    PyListType = Runtime.PyObject_Type(op);
+	    PyListType = Runtime.PyObject_Type(op); 
 	    Runtime.Decref(op);
 
 	    op = Runtime.PyDict_New();
-	    PyDictType = Runtime.PyObject_Type(op);
+	    PyDictType = Runtime.PyObject_Type(op); 
 	    Runtime.Decref(op);
 
 	    op = Runtime.PyInt_FromLong(0);
-	    PyIntType = Runtime.PyObject_Type(op);
+	    PyIntType = Runtime.PyObject_Type(op);  
 	    Runtime.Decref(op);
 
 	    op = Runtime.PyLong_FromLong(0);
-	    PyLongType = Runtime.PyObject_Type(op);
+	    PyLongType = Runtime.PyObject_Type(op); 
 	    Runtime.Decref(op);
 
 	    op = Runtime.PyFloat_FromDouble(0);
-	    PyFloatType = Runtime.PyObject_Type(op);
+	    PyFloatType = Runtime.PyObject_Type(op); 
 	    Runtime.Decref(op);
 
 	    IntPtr s = Runtime.PyString_FromString("__temp");
@@ -79,9 +79,9 @@
 	    IntPtr d = Runtime.PyDict_New();
 	    IntPtr c = Runtime.PyClass_New(t, d, s);
 
-	    PyClassType = Runtime.PyObject_Type(c);
+	    PyClassType = Runtime.PyObject_Type(c); 
 	    IntPtr i = Runtime.PyInstance_New(c, t, d);
-	    PyInstanceType = Runtime.PyObject_Type(i);
+	    PyInstanceType = Runtime.PyObject_Type(i); 
 
 	    Runtime.Decref(s);
 	    Runtime.Decref(t);
@@ -115,36 +115,32 @@
 	}
 
 	internal unsafe static void Incref(IntPtr op) {
-	    if (op == IntPtr.Zero) 
-		return;
-	    (*(int *)op)++;
+	    if (op != IntPtr.Zero) {
+		(*(int *)op)++;
+	    }
 	}
 
 
 	internal unsafe static void Decref(IntPtr op) {
 
-	    if (op == IntPtr.Zero) {
-		return;
-	    }
-
-	    if (--(*(int *)op) == 0) {
-
-		// fp = op -> ob_type -> tp_dealloc
-		IntPtr tp = Runtime.PyObject_Type(op);
-		IntPtr fp = Marshal.ReadIntPtr(tp, (6 * IntPtr.Size));
-		if (fp == IntPtr.Zero) {
-		    return;
-		}
-
-		try {
+	    // Py_DECREF is not exposed as a true function from the Python
+	    // dll, so we do the moral equivalent with managed code. This
+	    // also means we need to call an arbitrary function pointer,
+	    // which is not fun from C# :( Note to self: remember to lobby
+	    // Guido or Tim for an exported alias in the runtime!
+
+	    if (op != IntPtr.Zero) {
+		if (--(*(int *)op) == 0) {
+		    //string nn = PyObject_GetTypeName(op);
+		    //Console.WriteLine("decref: {0}", nn);
+		    IntPtr tp = Marshal.ReadIntPtr(op, (1 * IntPtr.Size));
+		    IntPtr fp = Marshal.ReadIntPtr(tp, (6 * IntPtr.Size));
+		    if (fp == IntPtr.Zero) {
+			return;
+		    }
 		    NativeCall.Impl.Void_Call_1(fp, op);
-		} catch {
-		    IntPtr ss = Runtime.PyObject_Str(op);
-		    string ds = Runtime.PyString_AsString(ss);
-		    Console.WriteLine("native call failed!");
+		    return;
 		}
-
-		return;
 	    }
 
 	}
@@ -672,6 +668,7 @@
 	internal unsafe static extern IntPtr
 	PyUnicode_FromEncodedObject(IntPtr ob, IntPtr enc, IntPtr err);
 
+
 	[DllImport("python22", CallingConvention=CallingConvention.Cdecl,
 		   EntryPoint="PyUnicodeUCS2_FromUnicode",
 		   ExactSpelling=true, CharSet=CharSet.Unicode)]
@@ -681,6 +678,9 @@
 	internal static IntPtr PyUnicode_FromString(string s) {
 	    return PyUnicode_FromUnicode(s, (s.Length));
 	}
+
+
+
 
 	[DllImport("python22", CallingConvention=CallingConvention.Cdecl,
 		   EntryPoint="PyUnicodeUCS2_GetSize",