scoder / lupa

Lua in Python
http://pypi.python.org/pypi/lupa
Other
1.01k stars 135 forks source link

Iterating over a generator-iterator from a Lua function #243

Open mfrigerio17 opened 1 year ago

mfrigerio17 commented 1 year ago

Ok, this seems convoluted and best illustrated with code that reproduces the problem (Ubuntu 22.04, Python 3.10.12, Lupa 2.0, Lua 5.4)

import lupa
lua = lupa.LuaRuntime(unpack_returned_tuples=True)

luaTable = lua.execute('''return { key1 = "value1" } ''')

class Foo:
    def __init__(self, table):
        self.table = table
        print("Foo():", self.table.key1, "\n")

    def getGenerator(self):
        def generator():
            for i in range(1,2):
                print("generator():", self.table.key1, "\n")
                yield i
        return generator()

foo = Foo(luaTable)

# iterate over the generator, prints the right thing
gen = foo.getGenerator()
for i in gen: pass

# using a Lua function to iterate over the generator gives weird result
luaF = lua.execute('''
return function(generator)
  for i in python.iter(generator) do end
end''')

gen = foo.getGenerator()
luaF(gen)

What the code does:

Output:

Foo(): value1 

generator(): value1 

generator(): (<generator object Foo.getGenerator.<locals>.generator at 0x7f4d68a61850>, None, 'value1')

Expected output

Foo(): value1 

generator(): value1 

generator(): value1

When the generator is invoked from within a Lua function (using python.iter) then self.table.key1 mysteriously become a tuple with three elements (<the generator itself>, None, <the actual value>) even though it should just be value1.

Any idea?