Skip to content
This repository has been archived by the owner on Sep 14, 2018. It is now read-only.

importextsion for generic container interface #1289

Closed
swn1 opened this issue May 5, 2016 · 8 comments
Closed

importextsion for generic container interface #1289

swn1 opened this issue May 5, 2016 · 8 comments

Comments

@swn1
Copy link

swn1 commented May 5, 2016

I am trying to implement a thin layer of glue between pyzmq and clrzmq4. The latter exposes some key functionality as extension methods over IEnumerable, a generic interface type specialized to a concrete type exposed by the same library.

Working in IronPython 2.7.5 on the 4.0 framework, I've not been able to make any headway on getting this to work. The library code in question is here:
https://github.com/zeromq/clrzmq4/blob/master/ZPollItems.cs
#1229 appears to be relevant but doesn't specifically address interfaces, I'm wondering if that's why it isn't working for me? I'd prefer not to have to push a change into the clrzmq4 project.

@jdhardy
Copy link
Member

jdhardy commented May 13, 2016

I assume you've tried using clr.ImportExtensions and it's not working, which means the mechanism probably isn't adding them to generic types. I seem to recall this being a known issue.

Does calling the extension method as a plain static method work? That is:

sockets  = ... # some IEnumerable<ZSocket>
incoming, error = ZPollItems.PollIn(sockets, items) # instead of sockets.PollIn(items)

Side note: those Poll methods are basically the worst-case scenario for binding Python to .NET - extension methods, out/ref parameters, default values - all of which should be handled, but sheesh.

@swn1
Copy link
Author

swn1 commented May 13, 2016

Pretty much a perfect storm -- that was my conclusion too. I've bypassed the issue by branching the clrzmq4 library and directly implementing the core of the backend namespace in C#. Our lawyers are all up in my business about shipping a compiled modified library but I had to stop cutting bait and start fishing. The repro instructions from #1290 will set you up, just checkout the pyzmq repro from before the changes to clrzmq4, the "Polling is hard" changeset swn1/pyzmq@5db4b3b

@swn1
Copy link
Author

swn1 commented May 13, 2016

I don't recall if I tried calling it as a static method. I think I did but IEnumerable is a (generic) interface and I don't see any way in IronPython to instantiate something that will cause the overload resolution to pick the right overload. If I recall correctly I did try to set up such a call and gave up.

@jdhardy
Copy link
Member

jdhardy commented May 14, 2016

So I had a chance to actually play with this in an interpreter, just using Linq. Let me know if there's something I've missed in your scenario, because it seems to work for me:

import clr
clr.AddReference("System.Core")
from System.Collections.Generic import List, IEnumerable

l = List[int]()
l.Add(1)
list(l.Concat(l)) # Error!

m = [2, 3, 4]
list(m.Concat(l)) # Error!

from System.Linq import Enumerable
clr.ImportExtensions(Enumerable)

list(l.Concat(l))
list(m.Concat(l))

So it should be picking up the extensions, even if you're using a plain Python list.

@swn1
Copy link
Author

swn1 commented May 16, 2016

My understanding of importextensions was broken. I didn't try the construction you're using above. Based on a breadcrumb I found somewhere I thought I understood the operand of ImportExtensions to be a Python type that would be patched with any suitable methods. So I was trying to construct a concrete type that would do what I needed. I think I understand what your example is doing now, I'll back up and take another shot at ZeroMQ. Thank you!

@slide
Copy link
Contributor

slide commented Aug 1, 2016

It looks like this was resolved, is that correct?

@swn1
Copy link
Author

swn1 commented Aug 1, 2016

I have been working on other tasks and was hoping to return to the
IronPython integration, including the Jupyter client library port, this
month. I believe that with the tips given it will prove to be an operator
error situation but I can't confirm that as of today.

On Mon, Aug 1, 2016 at 3:13 PM, Alex Earl notifications@github.com wrote:

It looks like this was resolved, is that correct?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#1289 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGL1D47FuP7o_4Pp2miGlbrk5lJNq6Dpks5qblNhgaJpZM4IYC_c
.

@swn1
Copy link
Author

swn1 commented Sep 19, 2016

I could not make it work with a plain python list but when I switched to a System.List[ZSocket] instance it picked up the extension method right away. To get the correct overload I also had to change some of the other actual parameters to specific C# types. Thanks for the pointers!

@slide slide closed this as completed Oct 8, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants