lupa icon indicating copy to clipboard operation
lupa copied to clipboard

equality check of python objects in lua

Open mfrigerio17 opened this issue 5 years ago • 2 comments

Hi, The following code fails with TypeError: __eq__() missing 1 required positional argument: 'rhs'

import lupa

lua = lupa.LuaRuntime(unpack_returned_tuples=True)

class foo: 
    def __init__(self): 
        self.a = 1 
    def __eq__(self, rhs): 
        return self.a==rhs.a
    def __hash__(self) :
        return hash(self.a)

f1 = foo()
f2 = foo()

lua.execute('f1 = python.eval("f1")')
lua.execute('f2 = python.eval("f2")')

print(f1==f2)                        # prints 'True' (correct)
lua.execute('print(f1==f2)')         # prints 'false' (wrong)
lua.execute('print(f1.__eq__(f2))')  # prints 'true' (correct)
lua.execute('print(f1.__eq__(f1))')  # fails

Is it a bug or am I missing something? Python 3.8.2, Lupa 1.9 Thanks

mfrigerio17 avatar Jun 01 '20 19:06 mfrigerio17

Yes, I think it makes sense to support this. However, we also have to make sure it produces something reasonable when comparing a Python object to a Lua object.

PR welcome that adds support for the Lua equality operator to Lupa wrapped Python objects, as well as a bunch of tests that show that the behaviour of different Python and Lua objects is "as expected", both left and right of the comparison operator.

scoder avatar Jun 07 '20 07:06 scoder

Should comparison of wrapped objects (both POBJECTs in Lua and Lua objects in Python) follow the host language semantics or the native language semantics?

I would say they should follow the native language semantics, so e.g. Python lists compare by value in Lua and Lua tables compare by identity in Python. See also https://github.com/scoder/lupa/issues/249.

astoff avatar Oct 28 '23 11:10 astoff