[pypy-dev] Fwd: mapping C# iterator to Python iterator

amit regmi regmi.amit at gmail.com
Tue Dec 4 19:41:22 CET 2007


I think the best way to solve this is to call .GetEnumerator explicitly and
use the resulting enumerator and access values using the enumerators
MoveNext() , Reset() and Current()
something like.

enmtor = self.GetEnumerator()
and use enmtor in the next() as I mentioned below.
but is this likely in the code we have ?

---------- Forwarded message ----------
From: amit <regmi.amit at gmail.com>
Date: Dec 4, 2007 10:05 AM
Subject: Re: [pypy-dev] mapping C# iterator to Python iterator
To: Antonio Cuni <anto.cuni at gmail.com>
Cc: PyPy Dev <pypy-dev at codespeak.net>


Antonio Cuni wrote:
> amit wrote:
>> Problems mapping the two functions
>>     d['__iter__'] = d['GetEnumerator']
>>     d['next'] = d['MoveNext']
>>
>> to make C# objects iterable in PyPy-Cli module.
>>
>> a) MoveNext() is not available in both "methods" and "staticmethods"
>> passed to build_wrapper
>
> sure, that's expected. MoveNext() is a method defined for the
> IEnumerator interface, not for IEnumerable.
>
> Moreover, I just realized that there is a discrepancy between .NET's
> MoveNext() and Python's next() methods: the former only advances the
> iterator without returning the current object, while the latter both
> advances the iterator and returns the current objects; also, we should
> throw StopIterationError when we reach the end of the iterator. In
> other words, next() should be implemented this way:
>
> def next(self):
>     if not self.MoveNext():
>         raise StopIterationError
>     return self.get_Current()
This makes a lot of sense. I think what you mean is
   self.enumrator = self.GetEnumerator()     # this returns IEnumerator

   def next(self):
          if not self.enumrator.MoveNext()
                  raise StopIteration
          return self.enumrator.Current()         #  or  self.get_Current()

but MoveNext() can not be called this way. I have tried it. And MoveNext
is not available to self as your example shows.

>
> To summarize:
>
>   - for classes implementing IEnumerable: you should map __iter__ to
> GetEnumerator
>   - for classes implementing IEnumerator: you should add a next()
> method like the one above, and add an __iter__ that returns self.
>
I think classes only implement IEnumerable. and IEnumerable uses
IEnumerator as below:

public interface IEnumerable
{
   IEnumerator GetEnumerator();
}


>> b) and assignment of GetEnumerator to __iter__ gives following
>> TypeError.
>>     TypeError: Can't convert object
>> System.Collections.ArrayList+SimpleEnumerator to Python
> could you check in a failing test please?
>
I have checked in failing test in test_clr.py

- AmitReg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20071204/b5103e5d/attachment.html>


More information about the Pypy-dev mailing list