[Python.NET] Patch for python 2.5 + unicode support in 1.1

Bernt Røskar Brenna bernt.brenna at gmail.com
Fri Sep 12 10:26:24 CEST 2008


First of all, I am very new to Python.NET.

I needed support for embedding python 2.5 in .NET 1.1 (the code is
hosted in an app. server that is stuck on .NET 1.1 and is not likely
to be upgraded anytime soon).

So I made a few simple changes in the makefile and runtime files.

And also in pyobject and pystring to make it unicode (see my question below).

----

One question:
- PyString.IsStringType() returns false for <type 'unicode'> objects.
Is this the correct behaviour? I notice it also does this in trunk
(.net2.0 - py2.5) - so I suppose my approach in the patch is wrong. In
the patch, I change the impl. of IsStringType, so it returns true for
unicode objects as well.

----

The patch is against branches\1.0-branch\pythonnet


- Bernt

Index: src/runtime/pyobject.cs

===================================================================

--- src/runtime/pyobject.cs	(revision 100)

+++ src/runtime/pyobject.cs	(working copy)

@@ -96,9 +96,7 @@

 	public object AsManagedObject(Type t) {
 	    Object result;
 	    if (!Converter.ToManaged(this.Handle, t, out result, false)) {
-		throw new InvalidCastException(
-			  "cannot convert object to target type"
-			  );
+            throw new InvalidCastException("cannot convert object to
target type");
 	    }
 	    return result;
 	}
@@ -819,7 +817,7 @@

 	/// </remarks>

 	public override string ToString() {
-	    IntPtr strval = Runtime.PyObject_Str(obj);
+        IntPtr strval = Runtime.PyObject_Unicode(obj);
 	    string result = Runtime.GetManagedString(strval);
 	    Runtime.Decref(strval);
 	    return result;
Index: src/runtime/pystring.cs

===================================================================

--- src/runtime/pystring.cs	(revision 100)

+++ src/runtime/pystring.cs	(working copy)

@@ -59,7 +59,7 @@

 	/// </remarks>

 	public PyString(string s) : base() {
-	    obj = Runtime.PyString_FromStringAndSize(s, s.Length);
+        obj = Runtime.PyUnicode_FromUnicode(s, s.Length);
 	    if (obj == IntPtr.Zero) {
 		throw new PythonException();
 	    }
@@ -75,7 +75,7 @@

 	/// </remarks>

 	public static bool IsStringType(PyObject value) {
-	    return Runtime.PyString_Check(value.obj);
+	    return Runtime.IsStringType(value.obj);
 	}

     }
Index: src/runtime/runtime.cs

===================================================================

--- src/runtime/runtime.cs	(revision 100)

+++ src/runtime/runtime.cs	(working copy)

@@ -23,7 +23,12 @@

 	/// before calling any of these methods.
 	/// </summary>

+#if PYVERSION24
 	internal const string dll = "python24";
+#endif
+#if PYVERSION25
+	internal const string dll = "python25";
+#endif
 	internal static bool wrap_exceptions;
 	internal static bool is32bit;

@@ -605,6 +610,10 @@

 	internal unsafe static extern IntPtr
 	PyObject_Str(IntPtr pointer);

+    [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl,
+        ExactSpelling = true, CharSet = CharSet.Ansi)]
+    internal unsafe static extern IntPtr
+    PyObject_Unicode(IntPtr pointer);

 	[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
 		   ExactSpelling=true, CharSet=CharSet.Ansi)]
Index: makefile

===================================================================

--- makefile	(revision 100)

+++ makefile	(working copy)

@@ -1,6 +1,7 @@

 # Makefile for the PythonRuntime .NET assembly and tests. Thanks to
 # Camilo Uribe <kmilo at softhome.net> for contributing Mono support.

+PYVERSION=25
 RELEASE=pythonnet-1.0-rc2-py2.4-clr1.1-src
 RUNNER=
 ILDASM=ildasm
@@ -20,7 +21,7 @@

 Python.Runtime.dll: callconvutil.exe
 	cd src; cd runtime; \
 	$(CSC) /nologo /unsafe /target:library /out:../../Python.Runtime.dll \
-        /recurse:*.cs
+        /recurse:*.cs /define:PYVERSION$(PYVERSION)
 	cd ..; cd ..;
 	$(ILDASM) /nobar Python.Runtime.dll /out=Python.Runtime.il;
 	$(RUNNER) ./callconvutil.exe;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: unicode_10-branch.patch
Type: text/x-patch
Size: 3115 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20080912/a9bfbd7a/attachment.bin>


More information about the PythonDotNet mailing list