Selecting an overloaded contructor

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Selecting an overloaded contructor

Barton
  The readme clearly states that

from System import String, Char, Int32
s = String.__overloads__[Char, Int32]('A', 10)

should select the desired constructor, but ...
there is clearly no means in the current (2.0.0.2) version for a
ClassObject to activate the search by means of __getattribute__ (no
tp_getattro).

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 - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|

Re: Selecting an overloaded constructor + a MethodBinder.Bind patch

Barton
  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 - [hidden email]
> http://mail.python.org/mailman/listinfo/pythondotnet
>
_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet
Reply | Threaded
Open this post in threaded view
|

Re: Selecting an overloaded constructor + a MethodBinder.Bind patch

dhirschfeld
Barton <barton@...> writes:

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

Thanks for posting - could be useful for in future (I'm not using PythonDotNET
much at the moment but will be again shortly.)

Not sure if you've seen this but in the spirit of posting patches I thought
I'd mention Alexey Borzenkov's patch:

http://git.kitsu.ru/patched/pythonnet.git?a=commitdiff;h=995109e3ed27d6061dde5376f2968c9fbad894b7

Which fixes the problem of errors in constructors not being passed through to
Python.

HTH,
Dave

_________________________________________________
Python.NET mailing list - [hidden email]
http://mail.python.org/mailman/listinfo/pythondotnet