Skip to content

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

@mfrigerio17

Description

@mfrigerio17

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:

  • create a Lua table, return it into python
  • have a python object that stores the table into self
    • the object returns an iterator-generator that simply prints an element of the table
  • create and run the generator, in python - works fine
  • create and run the generator, via a Lua function - the table is no longer the same

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions