4

I am trying to provide python iterator support to a D class wrapped with pyd.wrap_class. According to the documentation (https://github.com/ariovistus/pyd/wiki/ClassWrap#Iterator_wrapping and http://pyd.readthedocs.org/en/latest/classes.html#iterator-wrapping) the next method should return null to signal termination.

Here is my minimal D example:

import pyd.pyd;
import pyd.pydobject;
import pyd.class_wrap;

class IteratorTest
{
    IteratorTest _iter()
    {
        return this;
    }

    PydObject _next()
    {
        return null;
    }

}

extern(C) void PydMain() {
    module_init();

    wrap_class!(
        IteratorTest,
        Def!(IteratorTest._iter, PyName!("__iter__")),
        Def!(IteratorTest._next, PyName!("next"))
    );
}

However, invoking this with the python test code

for item in IteratorTest() :
    print item

prints me a never ending stream of None. Does anyone know what I am doing wrong here?

harfel
  • 302
  • 3
  • 14
  • 1
    According to the documentation, the signature of the `_next()` method should be `PyObject* next()`. Could it be that you are not using the correct signature? – DejanLekic Apr 08 '16 at 16:39
  • Yeah, I tried that, but I cannot figure out where PyObject is defined. When I try to `import python;` the compiler fails with a file not found error. This is why I followed the docs on http://pyd.readthedocs.org/en/latest/pydobject.html where it is stated that "PydObject wraps a PyObject*". – harfel Apr 08 '16 at 16:51
  • https://github.com/ariovistus/pyd/blob/master/infrastructure/deimos/python/object.d - this is where the PyObject defined. – DejanLekic Apr 08 '16 at 17:07
  • indeed, this solved my issue. thanks!!! – harfel Apr 11 '16 at 13:51
  • Could you please write an answer to your question, so other people can easily find the solution (people rarely read comments, accepted solution is much better). – DejanLekic Apr 12 '16 at 06:45

1 Answers1

0

Thanks to DejanLekic I found the solution to the problem. The correct implementation is (note the changed signature of the _next() method):

import pyd.pyd;
import pyd.class_wrap;
import deimos.python.object;

class IteratorTest
{
    IteratorTest _iter()
    {
        return this;
    }

    PyObject *_next()
    {
        return null;
    }

}

extern(C) void PydMain() {
    module_init();

    wrap_class!(
        IteratorTest,
        Def!(IteratorTest._iter, PyName!("__iter__")),
        Def!(IteratorTest._next, PyName!("next"))
    );
}
harfel
  • 302
  • 3
  • 14