yglukhov / nimpy

Nim - Python bridge
MIT License
1.48k stars 60 forks source link

Iterators not exported when using PyNimObjectExperimental #266

Open etorres404 opened 2 years ago

etorres404 commented 2 years ago

Hi friends!

I was playing around with PyNimObjectExperimental and I found that iterators are not exported. For example:

# mymodule.nim
import nimpy

type TestType = ref object of PyNimObjectExperimental
  myField: string

proc setMyField(self: TestType, value: string) {.exportpy.} =
  self.myField = value

proc getMyField(self: TestType): string {.exportpy.} =
  self.myField

iterator iterate(self: TestType): int {.exportpy.} =
  for i in 0..<10: yield i
# test.py
import nimporter
import mymodule
tt = mymodule.TestType()
tt.setMyField("Hello")
assert(tt.getMyField() == "Hello")
print(list(tt.iterate()))

When I execute test.py I obtain the following error:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    print(list(tt.iterate()))
AttributeError: 'TestType' object has no attribute 'iterate'

This code works if I call the iterate function as mymodule.iterate(tt). I think it could be useful to also have iterators exported for PyNimObjectExperimental types. I'm pretty new to Nim, but I can have a look to that if it is ok :)