lupa icon indicating copy to clipboard operation
lupa copied to clipboard

__pairs metamethod is ignored when iterating Lua tables from Python

Open immerrr opened this issue 9 years ago • 1 comments

Here's a simple example:

In [16]: copy, proxy = LuaRuntime().execute("""
obj = {foo=123, bar=345}

proxy = {}
setmetatable(proxy, {
  __index = function(self, key)
    return obj[key]
  end,

  __newindex = function(self, key, value)
    obj[key] = value
  end,

  __pairs = function(self)
    return pairs(obj)
  end,
})

copy = {}
for k,v in pairs(proxy) do
  copy[k] = v
end

return copy, proxy
""")

In [17]: copy['foo']
Out[17]: 123

In [18]: proxy['foo']
Out[18]: 123

In [19]: list(copy.items())
Out[19]: [(u'foo', 123), (u'bar', 345)]

In [20]: list(proxy.items())
Out[20]: []

immerrr avatar Aug 12 '16 18:08 immerrr

Pull request welcome.

scoder avatar Aug 16 '16 14:08 scoder