[pypy-dev] Fwd: mapping C# iterator to Python iterator
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()
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
if not self.enumrator.MoveNext()
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
> - 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
>> b) and assignment of GetEnumerator to __iter__ gives following
>> 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
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Pypy-dev