[Zope-CVS] CVS: PythonNet/src/runtime - Iterator.cs:1.1 ClassBase.cs:1.5 ClassManager.cs:1.4 ConstructorBinder.cs:1.3 Converter.cs:1.4 Interop.cs:1.2 MethodBinder.cs:1.3 MethodWrapper.cs:1.3 Runtime.cs:1.8 TypeManager.cs:1.7

Brian Lloyd brian at zope.com
Tue Sep 30 19:54:43 EDT 2003


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

Modified Files:
	ClassBase.cs ClassManager.cs ConstructorBinder.cs Converter.cs 
	Interop.cs MethodBinder.cs MethodWrapper.cs Runtime.cs 
	TypeManager.cs 
Added Files:
	Iterator.cs 
Log Message:
Checkin stuff done in last few weeks (add nunit, iterator support)


=== Added File PythonNet/src/runtime/Iterator.cs ===
// Copyright (c) 2001, 2002 Zope Corporation and Contributors.
//
// All Rights Reserved.
//
// This software is subject to the provisions of the Zope Public License,
// Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
// FOR A PARTICULAR PURPOSE.

using System;
using System.Collections;
using System.Reflection;

namespace Python.Runtime {

    //========================================================================
    // Implements a generic Python iterator for IEnumerable objects and 
    // managed array objects. This supports 'for i in object:' in Python.
    //========================================================================

    internal class Iterator : ExtensionType {

	IEnumerator iter;

	public Iterator(IEnumerator e) : base() {
	    this.iter = e;
	}


	//====================================================================
	// Implements support for the Python iteration protocol.
	//====================================================================

	[CallConvCdecl()]
	public static IntPtr tp_iternext(IntPtr ob) {
	    Iterator self = GetManagedObject(ob) as Iterator;
	    if (!self.iter.MoveNext()) {
		Exceptions.SetError(Exceptions.StopIteration, Runtime.PyNone);
		return IntPtr.Zero;
	    }
	    object item = self.iter.Current;
	    return Converter.ToPythonImplicit(item);
	}


	[CallConvCdecl()]
	public static IntPtr tp_iter(IntPtr ob) {
	    Runtime.Incref(ob);
	    return ob;
	}

    }


}


=== PythonNet/src/runtime/ClassBase.cs 1.4 => 1.5 ===
--- PythonNet/src/runtime/ClassBase.cs:1.4	Fri Aug  8 15:49:21 2003
+++ PythonNet/src/runtime/ClassBase.cs	Tue Sep 30 19:54:11 2003
@@ -63,6 +63,34 @@
 	    return -1;
 	}
 
+
+	//====================================================================
+	// Standard iteration support for instances of reflected types.
+	//====================================================================
+
+	[CallConvCdecl()]
+	public static IntPtr tp_iter(IntPtr ob) {
+	    CLRObject co = GetManagedObject(ob) as CLRObject;
+
+	    if (co == null) {
+		Exceptions.SetError(Exceptions.TypeError, "invalid object");
+		return IntPtr.Zero;
+	    }
+
+	    IEnumerable e = co.inst as IEnumerable;
+
+	    if (e == null) {
+		Exceptions.SetError(Exceptions.TypeError,
+				    "iteration over non-sequence"
+				    ); 
+		return IntPtr.Zero;
+	    }
+
+	    Iterator iter = new Iterator(e.GetEnumerator());
+	    return iter.Handle;
+	}
+
+
 	//====================================================================
 	// Standard dealloc implementation for instances of reflected types.
 	//====================================================================


=== PythonNet/src/runtime/ClassManager.cs 1.3 => 1.4 ===
--- PythonNet/src/runtime/ClassManager.cs:1.3	Mon Jul 28 22:28:15 2003
+++ PythonNet/src/runtime/ClassManager.cs	Tue Sep 30 19:54:11 2003
@@ -103,7 +103,7 @@
 	    // Finally, initialize the class __dict__ and return the object.
 
 	    IntPtr pp = Runtime._PyObject_GetDictPtr(impl.tpHandle);
-	    IntPtr dict = Marshal.ReadIntPtr(pp);
+	    IntPtr dict = Marshal.ReadIntPtr(pp, 0);
 	    IDictionaryEnumerator iter;
 	    ManagedType item;
 	    string name;


=== PythonNet/src/runtime/ConstructorBinder.cs 1.2 => 1.3 ===
--- PythonNet/src/runtime/ConstructorBinder.cs:1.2	Mon Jul 28 22:28:15 2003
+++ PythonNet/src/runtime/ConstructorBinder.cs	Tue Sep 30 19:54:11 2003
@@ -54,7 +54,7 @@
 	    }
 
 	    //PythonEngine.EndAllowThreads(ts);
-	    return Converter.ToPython(result, result.GetType());
+	    return Converter.ToPythonImplicit(result);
 	}
 
     }


=== PythonNet/src/runtime/Converter.cs 1.3 => 1.4 ===
--- PythonNet/src/runtime/Converter.cs:1.3	Thu Sep 11 23:49:34 2003
+++ PythonNet/src/runtime/Converter.cs	Tue Sep 30 19:54:11 2003
@@ -127,6 +127,23 @@
 
 	}
 
+
+	//====================================================================
+	// In a few situations, we don't have any advisory type information
+	// when we want to convert an object to Python.
+	//====================================================================
+
+	internal static IntPtr ToPythonImplicit(Object value) {
+	    if (value == null) {
+		IntPtr result = Runtime.PyNone;
+		Runtime.Incref(result);
+		return result;
+	    }
+
+	    return ToPython(value, objectType); 
+	}
+
+
 	//====================================================================
 	// Return a managed object for the given Python object, converting
 	// basic types (string, int, etc.) into required managed objects.


=== PythonNet/src/runtime/Interop.cs 1.1 => 1.2 ===
--- PythonNet/src/runtime/Interop.cs:1.1	Fri Aug  8 15:49:21 2003
+++ PythonNet/src/runtime/Interop.cs	Tue Sep 30 19:54:11 2003
@@ -465,7 +465,7 @@
 		Delegate d = Delegate.CreateDelegate(dt, method);
 		MethodThunk cb = new MethodThunk(d);
 		Marshal.StructureToPtr(cb, temp, false);
-		IntPtr fp = Marshal.ReadIntPtr(temp);
+		IntPtr fp = Marshal.ReadIntPtr(temp, 0);
 		keepAlive.Add(d);
 		return fp;
 	    }


=== PythonNet/src/runtime/MethodBinder.cs 1.2 => 1.3 ===
--- PythonNet/src/runtime/MethodBinder.cs:1.2	Mon Jul 28 22:28:15 2003
+++ PythonNet/src/runtime/MethodBinder.cs	Tue Sep 30 19:54:11 2003
@@ -27,6 +27,8 @@
 	// MethodInfo info;
 	public ArrayList list;
 	public bool FreeThreaded = true;
+	public MethodBase[] methods;
+	public bool init = false;
 
 	internal MethodBinder () {
 	    // this.info = null;
@@ -51,8 +53,10 @@
 	internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw) {
 	    // loop to find match, return invoker w/ or /wo error
 	    int nargs = Runtime.PyTuple_Size(args);
-	    MethodBase[] methods = (MethodBase[])list.ToArray(typeof(MethodBase));
-	    
+	    if (!init) {
+		methods = (MethodBase[])list.ToArray(typeof(MethodBase));
+	    }
+
 	    for (int i = 0; i < methods.Length; i++) {
 		MethodBase mi = methods[i];
 		ParameterInfo[] pi = mi.GetParameters();


=== PythonNet/src/runtime/MethodWrapper.cs 1.2 => 1.3 ===
--- PythonNet/src/runtime/MethodWrapper.cs:1.2	Fri Aug  8 15:49:21 2003
+++ PythonNet/src/runtime/MethodWrapper.cs	Tue Sep 30 19:54:11 2003
@@ -48,7 +48,7 @@
 	    MethodThunk cb = new MethodThunk(mDelegate);
 	    IntPtr pThunk = Marshal.AllocHGlobal(IntPtr.Size);
 	    Marshal.StructureToPtr(cb, pThunk, false);
-	    IntPtr fp = Marshal.ReadIntPtr(pThunk);
+	    IntPtr fp = Marshal.ReadIntPtr(pThunk, 0);
 	    Marshal.FreeHGlobal(pThunk);
 
 	    methodDef = Runtime.PyMem_Malloc(4 * IntPtr.Size);


=== PythonNet/src/runtime/Runtime.cs 1.7 => 1.8 ===
--- PythonNet/src/runtime/Runtime.cs:1.7	Thu Sep 11 23:49:34 2003
+++ PythonNet/src/runtime/Runtime.cs	Tue Sep 30 19:54:11 2003
@@ -24,7 +24,7 @@
 	/// </summary>
 
 	private static IntPtr MainThreadState;
-	private static bool Is32Bit;
+	internal static bool Is32Bit;
 
 	internal static void Initialize() {
 
@@ -194,6 +194,7 @@
 	    }
 	}
 
+
 	[DllImport("python22", CallingConvention=CallingConvention.Cdecl,
 		    ExactSpelling=true, CharSet=CharSet.Ansi)]
 	internal unsafe static extern void
@@ -350,7 +351,7 @@
 	//====================================================================
 
 	// A macro-like method to get the type of a Python object. This is
-	// designed to be lean and mean and avoid managed <-> unmanaged 
+	// designed to be lean and mean in IL & avoid managed <-> unmanaged 
 	// transitions. Note that this does not incref the type object.
 
 	internal unsafe static IntPtr 
@@ -368,7 +369,8 @@
 	}
 
 	// Managed version of the standard Python C API PyObject_Type call.
-	// This version avoids a managed <-> unmanaged transition.
+	// This version avoids a managed <-> unmanaged transition. This one
+	// does incref the returned type object.
 
 	internal unsafe static IntPtr 
 	PyObject_Type(IntPtr op) {


=== PythonNet/src/runtime/TypeManager.cs 1.6 => 1.7 ===
--- PythonNet/src/runtime/TypeManager.cs:1.6	Fri Aug  8 15:49:21 2003
+++ PythonNet/src/runtime/TypeManager.cs	Tue Sep 30 19:54:11 2003
@@ -108,7 +108,7 @@
 
 	private static void InitMethods(IntPtr pytype, Type type) {
 	    IntPtr ppdict = Runtime._PyObject_GetDictPtr(pytype);
-	    IntPtr dict = Marshal.ReadIntPtr(ppdict);
+	    IntPtr dict = Marshal.ReadIntPtr(ppdict, 0);
 	    Type marker = typeof(PythonMethodAttribute);
 
 	    BindingFlags flags = BindingFlags.Public | BindingFlags.Static;




More information about the Zope-CVS mailing list