[Python.NET] Selecting an overloaded constructor + a MethodBinder.Bind patch

Barton barton at BCDesignsWell.com
Mon Jul 26 01:15:17 CEST 2010


  In case anyone is interested:
I have added a Constructors object named '__overloads__' to the 
ClassObject.__dict__ that has the necessary machinery to select 
overloaded constructors using subscript notation, as in:
from System import String, Char, Int32
s = String.__overloads__[Char, Int32]('A', 10)
or
from System import Array
CharArrType = Array[Char]
StringFromCharArr = String.__overloads__[CharArrType]
s = StringFromCharArr(list('hello'))

Just as in MethodObject, I implement a Descriptor __get__() in managed 
code that returns a CtorMapper for ctor selection and invoking.


Also fixed a bug in MethodBinder.Bind() that threw an unhandled 
InvalidCastException when calling a non-static method on a class rather 
than on an instance of the class.  Here's the patch as it stands:
diff --git a/src/runtime/methodbinder.cs b/src/runtime/methodbinder.cs
index 862a110..f1bc042 100644
-- a/src/runtime/methodbinder.cs
++ b/src/runtime/methodbinder.cs
@@ -294,23 +295,39 @@ namespace Python.Runtime {

                      Object target = null;
                      if ((!mi.IsStatic) && (inst != IntPtr.Zero)) {
-                        CLRObject co = 
(CLRObject)ManagedType.GetManagedObject(
-                                                  inst
-                                                  );
+                        //CLRObject co = 
(CLRObject)ManagedType.GetManagedObject(inst);
+                        // Calling on a ClassObject raises an unhandled 
exception:
+                        // InvalidCastException: Unable to cast object 
of type
+                        // 'Python.Runtime.ClassObject' to type 
'Python.Runtime.CLRObject'
+                        //
+                        //ManagedType mt = 
ManagedType.GetManagedObject(inst);
+                        // The above cast would fail if 
GetManagedObject(inst) returned null.
+                        //CLRObject co = mt as CLRObject;
+
+                        CLRObject co = 
ManagedType.GetManagedObject(inst) as CLRObject;
+
+                        // Sanity check: this ensures a graceful exit 
if someone does
+                        // something intentionally wrong like call a 
non-static method
+                        // on the class rather than on an instance of 
the class.
+                        // XXX maybe better to do this before all the 
other rigmarole.
+                        if (co == null) {
+                            return null;
+                        }
                          target = co.inst;
                      }
-
+                    // target may or may not be null, here
                      return new Binding(mi, target, margs, outs);
                  }
-            }
+            }   // END foreach MethodBase mi in _methods


I sure with that we could get the maintainers of the SourceForge project 
to make some contact...
possibly bring in some fresh blood to the project.

On 7/5/2010 11:29 PM, Barton wrote:
>  The readme clearly states that
>
> from System import String, Char, Int32
> s = String.__overloads__[Char, Int32]('A', 10)
>
> should select the desired constructor, but ...
> it doesn't
>
> Is this something that worked in an earlier version?
> Where there changes that were made that were not committed to the trunk?
> Is this something that anybody else has come up against? fixed???
>
> Thanks, all,
> Barton
> Windows 7, Framework Version: 2.0.50727.42 (AFAIK)
> Python 2.6.5
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org
> http://mail.python.org/mailman/listinfo/pythondotnet
>


More information about the PythonDotNet mailing list