Skip to content

Instantly share code, notes, and snippets.

@dubois
Created March 11, 2014 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dubois/9478877 to your computer and use it in GitHub Desktop.
Save dubois/9478877 to your computer and use it in GitHub Desktop.
Patch to _lupa.pyx to compare tables by identity of lua table rather than by identity of python wrapper
==== //dfp/Main/Common/Code/NonDF/lupa/lupa/_lupa.pyx#1 (text) - //dfp/Main/Common/Code/NonDF/lupa/lupa/_lupa.pyx#2 (text) ==== content
@@ -18,6 +18,7 @@
cdef extern from *:
ctypedef char* const_char_ptr "const char*"
+ctypedef unsigned long uintptr_t
cdef object exc_info
from sys import exc_info
@@ -458,6 +459,45 @@
def __iter__(self):
return _LuaIter(self, KEYS)
+ def __richcmp__(_LuaTable self, other_, int op):
+ if not isinstance(other_, _LuaTable):
+ if op == 2: return False
+ elif op == 3: return True
+ return NotImplemented
+ cdef _LuaTable other = <_LuaTable>other_
+ cdef lua_State* L = self._state
+ cdef uintptr_t selfptr
+ cdef uintptr_t otherptr
+ cdef int ret = 0
+ lock_runtime(self._runtime)
+ try:
+ self.push_lua_object()
+ selfptr = <uintptr_t>lua.lua_topointer(L, -1)
+ lua.lua_pop(L, 1)
+ other.push_lua_object()
+ otherptr = <uintptr_t>lua.lua_topointer(L, -1)
+ lua.lua_pop(L, 1)
+ finally:
+ unlock_runtime(self._runtime)
+ if op == 2: ret = selfptr == otherptr
+ elif op == 3: ret = selfptr != otherptr
+ elif op == 0: ret = selfptr < otherptr
+ elif op == 1: ret = selfptr <= otherptr
+ elif op == 4: ret = selfptr > otherptr
+ elif op == 5: ret = selfptr >= otherptr
+ if ret: return True
+ else: return False
+
+ def __hash__(self):
+ cdef lua_State* L = self._state
+ lock_runtime(self._runtime)
+ try:
+ self.push_lua_object()
+ return <int>lua.lua_topointer(L, -1)
+ finally:
+ lua.lua_pop(L, 1)
+ unlock_runtime(self._runtime)
+
def keys(self):
"""Returns an iterator over the keys of a table that this
object represents. Same as iter(obj).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment