[IronPython] Magic methods on CLR types

Jeffrey Sax jeffrey at extremeoptimization.com
Wed Dec 2 17:38:57 PST 2009


I found 2 issues:

 

1. It looks like __index__ is not called for slice arguments of non-Python
objects (like List<T>'s). Reading PEP 357 (which introduced __index__), I
would expect __index__ to always be called, so that I can be sure that the
slice components are either int, long, or NULL. (I haven't verified this in
CPython.)

 

2. If I supply a  __getitem__ method in my extension type (in my case with a
strongly typed ISlice parameter), the underlying CLR indexers are no longer
picked up.

 

Extender assembly:

 

[assembly: ExtensionType(typeof(List<>), typeof(ListExtensions<>))]

namespace Compatibility.Python {

    public static class ListExtensions<T> {

        public static T __getitem__(List<T> a, ISlice slice) {

            return a[(int)slice.Start]; // Dummy

        }

    }

}

 

IP session 1:

IronPython 2.6 (2.6.10920.0) on .NET 2.0.50727.4927

Type "help", "copyright", "credits" or "license" for more information.

>>> from System.Collections.Generic import List

>>> a=List[int]()

>>> a.Add(55)

>>> a[0]

55

>>> a[0:1.2]

55

>>> b=[1,2,3,4]

>>> b[0:1.2]

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: expected index value, got float

>>> 

 

IP session 2:

IronPython 2.6 (2.6.10920.0) on .NET 2.0.50727.4927

Type "help", "copyright", "credits" or "license" for more information.

>>> import clr

>>> clr.AddReferenceToFile("Compatibility.Python")

>>> from System.Collections.Generic import List

>>> a=List[int]()

>>> a.Add(55)

>>> a[0]

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

TypeError: expected ISlice, got int

>>> 

 

From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of Dino Viehland
Sent: Tuesday, December 01, 2009 8:57 PM
To: Discussion of IronPython
Subject: Re: [IronPython] Magic methods on CLR types

 

If you want IronPython to recognize them you just need to create an assembly
w/ ExtensionTypeAttribute and have it point at the extended and extending
types.  Then the assembly needs to be loaded into the ScriptRuntime using
ScriptRuntime.LoadAssembly.

 

We still need to figure out our story for recognizing and importing normal
.NET extension methods that C# supports.

 

From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of Jeffrey Sax
Sent: Tuesday, December 01, 2009 5:27 PM
To: 'Discussion of IronPython'
Subject: [IronPython] Magic methods on CLR types

 

Is there a way to add magic methods like __repr__, __call__, etc. to CLR
types?

 

1.       Can it be done in the external CLR assembly without polluting the
API for other languages? If so, how?

2.       Can it be done using F# style type augmentations by redefining such
methods on the Python type corresponding to the CLR type? If so, how?

3.       If neither of these alternatives is possible, is there a third way?

 

Thanks!

 

Jeffrey Sax

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ironpython.com/pipermail/users-ironpython.com/attachments/20091202/61a86bff/attachment.htm>


More information about the Users mailing list