<div dir="ltr">Hi,<br><div class="gmail_quote"><div dir="ltr"><br>I'm using cython to wrap DJB's primegen library (<a href="http://cr.yp.to/primegen.html" target="_blank">http://cr.yp.to/primegen.html</a>).<br><br>
Here's my pyx file:<br><br><div style="margin-left: 40px; font-family: courier new,monospace;">
cdef extern from "primegen.h":<br> ctypedef unsigned long uint32<br> ctypedef unsigned long long uint64<br><br> ctypedef struct primegen:<br> uint32 buf[16][2048]<br> uint64 p[512]<br> int num<br>
int pos<br> uint64 base<br> uint64 L<br><br> void primegen_init(primegen* pg)<br> uint64 primegen_next(primegen* pg)<br> uint64 primegen_peek(primegen* pg)<br> uint64 primegen_count(primegen* pg, uint64 to)<br>
void primegen_skipto(primegen* pg, uint64 to)<br><br>cdef class PrimeGen:<br> cdef primegen pg<br><br> def __new__(self):<br> primegen_init(&<a href="http://self.pg" target="_blank">self.pg</a>)<br><br>
def next(self):<br>
return primegen_next(&<a href="http://self.pg" target="_blank">self.pg</a>)<br><br> def peek(self):<br> return primegen_peek(&<a href="http://self.pg" target="_blank">self.pg</a>)<br><br> def count(self, uint64 to):<br>
return primegen_count(&<a href="http://self.pg" target="_blank">self.pg</a>, to)<br>
<br> def skipto(self, uint64 to):<br> primegen_skipto(&<a href="http://self.pg" target="_blank">self.pg</a>, to)<br></div><font face="arial,helvetica,sans-serif"><br>Notice that the class has a method called "next". This means that it should be usable as an iterator. Here's what happens if I try to do that:<br>
<br></font><div style="margin-left: 40px;"><font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) <br>[GCC 4.0.1 (Apple Inc. build 5465)] on darwin<br>
Type "help", "copyright", "credits" or "license" for more information.<br></span></font><font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">>>> from primegen import PrimeGen</span></font><br style="font-family: courier new,monospace;">
<font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">>>> class C:</span></font><br style="font-family: courier new,monospace;"><font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">... def __iter__(self):</span></font><br style="font-family: courier new,monospace;">
<font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">... return PrimeGen()</span></font><br style="font-family: courier new,monospace;"><font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">... </span></font><br style="font-family: courier new,monospace;">
<font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">>>> for i in C():</span></font><br style="font-family: courier new,monospace;"><font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">... print i</span></font><br style="font-family: courier new,monospace;">
<font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">... </span></font><br style="font-family: courier new,monospace;"><font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">Traceback (most recent call last):</span></font><br style="font-family: courier new,monospace;">
<font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;"> File "<stdin>", line 1, in <module></span></font><br style="font-family: courier new,monospace;"><font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">TypeError: __iter__ returned non-iterator of type 'primegen.PrimeGen'<br>
<br></span></font></div><font face="arial,helvetica,sans-serif">How come this doesn't work? The generated class definitely has the "next" method required by the iterator protocol.<br><br></font><div style="margin-left: 40px;">
<font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">>>> p = PrimeGen()</span></font><br><font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">
>>> dir(p)</span></font><br><font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
'__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__', 'count', 'next', 'peek', 'skipto']</span></font><br></div><font face="arial,helvetica,sans-serif"><br>Thanks,<br>Alex</font></div>
</div></div>