Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def init(self, func, *args):
# HACK: should not directly reference lupa here
import lupa
if lupa.lua_type(args) == 'table':
args = args.values()
print ('Expression: ', func, args)
self.func = func
self.args = list(args)
self.output = Signal()
# Workaround for list event bug
self.input = self.args[0]
def proxyMethod(*args, **kwargs):
# We are in the script thread here, we must syncronize with the main
# thread before calling the attribute
condition.acquire()
args = list(args)
if len(args) >= 2 and lua_type(args[1]) == 'table':
# First parameter is a lua table. Handle this as **kwargs call
kwargs = self.wrapLuaToPy(args[1])
del args[1]
for i, __arg in enumerate(args):
args[i] = self.wrapLuaToPy(args[i])
try:
Application().queue(mainThreadCaller, args, kwargs)
condition.wait(20) # Timeout to not let the script hang forever
if 'error' in retval:
self.log("Error during call: %s", retval['error'])
raise AttributeError(retval['error'])
elif 'return' in retval:
return self.wrapPyToLua(retval['return'])
finally:
condition.release()
raise AttributeError('The call to the function "%s" timed out' % attrName)
finally:
self.__threadLock.release()
if state == LuaScript.CLOSING:
self.__setState(LuaScript.CLOSED)
return
if state == LuaScript.LOADING:
self.__load()
elif task is not None:
name, args = task
args = list(args)
for i, arg in enumerate(args):
args[i] = self.wrapPyToLua(arg)
if isinstance(name, (str, unicode)):
func = getattr(self.lua.globals(), name)
self.runningLuaThread = func.coroutine(*args)
elif lua_type(name) == 'thread':
self.runningLuaThread = name
elif lua_type(name) == 'function':
self.runningLuaThread = name.coroutine(*args)
else:
continue
try:
self.__setState(LuaScript.RUNNING)
self.runningLuaThread.send(None)
except StopIteration:
pass
except Exception as error:
self.log("Could not execute function %s: %s", name, error)
self.runningLuaThread = None
self.__setState(LuaScript.IDLE)
def wrapLuaToPy(self, arg):
if lua_type(arg) == 'function':
func = LuaFunctionWrapper(self, arg)
self.references.append(weakref.ref(func))
return func
elif lua_type(arg) == 'table':
table = dict(arg)
for key in table:
# Recursive wrap
table[key] = self.wrapLuaToPy(table[key])
return table
return arg
def wrapLuaToPy(self, arg):
if lua_type(arg) == 'function':
func = LuaFunctionWrapper(self, arg)
self.references.append(weakref.ref(func))
return func
elif lua_type(arg) == 'table':
table = dict(arg)
for key in table:
# Recursive wrap
table[key] = self.wrapLuaToPy(table[key])
return table
return arg
def _convert_lua_result(self, result, nested=True):
from lupa import lua_type
if lua_type(result) == 'table':
for key in (b'ok', b'err'):
if key in result:
msg = self._convert_lua_result(result[key])
if not isinstance(msg, bytes):
# TODO: put in a constant for this
raise redis.ResponseError("wrong number or type of arguments")
if key == b'ok':
return SimpleString(msg)
elif nested:
return redis.ResponseError(msg)
else:
raise redis.ResponseError(msg)
# Convert Lua tables into lists, starting from index 1, mimicking the behavior of StrictRedis.
result_list = []
for index in itertools.count(1):
if index not in result:
def _convert_lua_result(self, result, nested=True):
from lupa import lua_type
if lua_type(result) == 'table':
for key in ('ok', 'err'):
if key in result:
msg = self._convert_lua_result(result[key])
if not isinstance(msg, bytes):
raise ResponseError("wrong number or type of arguments")
if key == 'ok':
return msg
elif nested:
return ResponseError(msg)
else:
raise ResponseError(msg)
# Convert Lua tables into lists, starting from index 1, mimicking the behavior of StrictRedis.
result_list = []
for index in count(1):
if index not in result:
break
def __sandboxInterpreter(self):
for obj in self.lua.globals():
if obj == '_G':
# Allow _G here to not start recursion
continue
if obj not in SAFE_FUNCTIONS:
del self.lua.globals()[obj]
continue
if lua_type(self.lua.globals()[obj]) != 'table':
continue
funcs = SAFE_FUNCTIONS[obj]
for func in self.lua.globals()[obj]:
if func not in funcs:
del self.lua.globals()[obj][func]
self.lua.globals().list = List
self.lua.globals().require = self.__require