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

2008-09-12 Thread Bernt Røskar Brenna
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  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 @@

/// 

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 @@

/// 

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 @@

/// 

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.
/// 

+#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 <[EMAIL PROTECTED]> 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;
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 @@
 	/// 
 
 	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
=

[Python.NET] Adding support for .NET 1.1 i trunk - is it possible?

2008-09-12 Thread Bernt Røskar Brenna
At least for me it would be useful to have support for .NET 1.1. I am
aware of the 1.0-branch, but I would like to benefit from the 2.5 and
2.6 work that has been done i trunk, as well as any bugfixes not
merged from trunk to 1.0-branch.

I am therefore looking into adding support for 1.1 in trunk. As I am
new to Python.net, it would be nice if someone more knowledgeable had
a quick peek at my planned approach.

Would the following approach work?:

- Where generics is used in Runtime, using conditional compilation to replace:

List aList;
aList = new List();

with

List_of_string aList;
aList = new List_of_string();

OR: Might possibly create sed scripts or similar to create *.net11.cs
files with generics declarations replaced instead.

The various List_of_ and Dictionary_of_ classes would be code
generated and placed in a single code file.

- Use conditional compilation to isolate all GenericUtil-related code.

- Create makefile.net11 for build


Does anyone have any advice to offer, reasons this approach won't
work, insights into how the task is vastly more complicated than I
think etc.?


Bernt
_
Python.NET mailing list - [email protected]
http://mail.python.org/mailman/listinfo/pythondotnet