<div dir="ltr">Hi,<br><div class="gmail_quote"><div dir="ltr"><br>I&#39;m using cython to wrap DJB&#39;s primegen library (<a href="http://cr.yp.to/primegen.html" target="_blank">http://cr.yp.to/primegen.html</a>).<br><br>
Here&#39;s my pyx file:<br><br><div style="margin-left: 40px; font-family: courier new,monospace;">
cdef extern from &quot;primegen.h&quot;:<br>&nbsp;&nbsp;&nbsp; ctypedef unsigned long uint32<br>&nbsp;&nbsp;&nbsp; ctypedef unsigned long long uint64<br><br>&nbsp;&nbsp;&nbsp; ctypedef struct primegen:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uint32 buf[16][2048]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uint64 p[512]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int num<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int pos<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uint64 base<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uint64 L<br><br>&nbsp;&nbsp;&nbsp; void primegen_init(primegen* pg)<br>&nbsp;&nbsp;&nbsp; uint64 primegen_next(primegen* pg)<br>&nbsp;&nbsp;&nbsp; uint64 primegen_peek(primegen* pg)<br>&nbsp;&nbsp;&nbsp; uint64 primegen_count(primegen* pg, uint64 to)<br>

&nbsp;&nbsp;&nbsp; void primegen_skipto(primegen* pg, uint64 to)<br><br>cdef class PrimeGen:<br>&nbsp;&nbsp;&nbsp; cdef primegen pg<br><br>&nbsp;&nbsp;&nbsp; def __new__(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; primegen_init(&amp;<a href="http://self.pg" target="_blank">self.pg</a>)<br><br>
&nbsp;&nbsp;&nbsp; def next(self):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return primegen_next(&amp;<a href="http://self.pg" target="_blank">self.pg</a>)<br><br>&nbsp;&nbsp;&nbsp; def peek(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return primegen_peek(&amp;<a href="http://self.pg" target="_blank">self.pg</a>)<br><br>&nbsp;&nbsp;&nbsp; def count(self, uint64 to):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return primegen_count(&amp;<a href="http://self.pg" target="_blank">self.pg</a>, to)<br>
<br>&nbsp;&nbsp;&nbsp; def skipto(self, uint64 to):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; primegen_skipto(&amp;<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 &quot;next&quot;.&nbsp; This means that it should be usable as an iterator.&nbsp; Here&#39;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 &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.<br></span></font><font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">&gt;&gt;&gt; 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;">&gt;&gt;&gt; class C:</span></font><br style="font-family: courier new,monospace;"><font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">...&nbsp;&nbsp; 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;">...&nbsp;&nbsp;&nbsp;&nbsp; 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;">&gt;&gt;&gt; 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;">...&nbsp;&nbsp; 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;">&nbsp; File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;</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 &#39;primegen.PrimeGen&#39;<br>

<br></span></font></div><font face="arial,helvetica,sans-serif">How come this doesn&#39;t work?&nbsp; The generated class definitely has the &quot;next&quot; 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;">&gt;&gt;&gt; p = PrimeGen()</span></font><br><font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">
&gt;&gt;&gt; dir(p)</span></font><br><font face="arial,helvetica,sans-serif"><span style="font-family: courier new,monospace;">
[&#39;__class__&#39;, &#39;__delattr__&#39;, &#39;__doc__&#39;, &#39;__getattribute__&#39;, &#39;__hash__&#39;,
&#39;__init__&#39;, &#39;__new__&#39;, &#39;__reduce__&#39;, &#39;__reduce_ex__&#39;, &#39;__repr__&#39;,
&#39;__setattr__&#39;, &#39;__str__&#39;, &#39;count&#39;, &#39;next&#39;, &#39;peek&#39;, &#39;skipto&#39;]</span></font><br></div><font face="arial,helvetica,sans-serif"><br>Thanks,<br>Alex</font></div>
</div></div>