def __init__(self, tab, return_func, render_options, sandboxed, lua_package_path, lua_sandbox_allowed_modules): """ :param splash.browser_tab.BrowserTab tab: BrowserTab object :param callable return_func: function that continues the script :param splash.render_options.RenderOptions render_options: arguments :param bool sandboxed: whether the script should be sandboxed :param str lua_package_path: paths to add to Lua package.path :param iterable lua_sandbox_allowed_modules: a list of modules allowed to be required from a sandbox """ self.tab = tab self.sandboxed = sandboxed self.lua = self._create_runtime(lua_package_path) self._setup_lua_sandbox(lua_sandbox_allowed_modules) self._return = return_func self._exceptions = [] self._command_ids = itertools.count() self.args = python2lua(self.lua, render_options.data) commands = {} for name in dir(self): value = getattr(self, name) if is_command(value): commands[name] = self.lua.table_from({ 'is_async': getattr(value, '_is_async'), 'returns_error_flag': getattr(value, '_returns_error_flag', False), }) self.commands = python2lua(self.lua, commands)
def __init__(self, tab, return_func, render_options, sandboxed): """ :param splash.browser_tab.BrowserTab tab: BrowserTab object :param callable return_func: function that continues the script :param splash.render_options.RenderOptions render_options: arguments :param bool sandboxed: whether the script should be sandboxed """ self.lua = self._create_runtime() self.tab = tab self.sandboxed = sandboxed self._return = return_func self._exceptions = [] self._command_ids = itertools.count() self.args = python2lua(self.lua, render_options.data) commands = {} for name in dir(self): value = getattr(self, name) if is_command(value): commands[name] = self.lua.table_from({ 'is_async': getattr(value, '_is_async'), 'returns_error_flag': getattr(value, '_returns_error_flag', False), }) self.commands = python2lua(self.lua, commands)
def test_list_modified_correct(self): func = self.lua.eval(""" function (arr) table.insert(arr, "bar") return arr end """) arr = python2lua(self.lua, [3, 4]) arr2 = func(arr) self.assertEqual(lua2python(self.lua, arr2), [3, 4, "bar"])
def test_list_modified_incorrect(self): func = self.lua.eval(""" function (arr) arr["foo"] = "bar" return arr end """) arr = python2lua(self.lua, [3, 4]) arr2 = func(arr) with pytest.raises(ValueError): lua2python(self.lua, arr2)
def test_sparse_list(self): func1 = self.lua.eval(""" function (arr) arr[5] = "foo" return arr end """) func2 = self.lua.eval(""" function (arr) arr[100000] = "foo" return arr end """) arr = python2lua(self.lua, [1, 2]) arr1 = lua2python(self.lua, func1(arr)) self.assertEqual(arr1, [1, 2, None, None, "foo"]) with pytest.raises(ValueError): arr2 = lua2python(self.lua, func2(arr))
def assertSurvivesConversion(self, obj): lua_obj = python2lua(self.lua, obj) py_obj = lua2python(self.lua, lua_obj) self.assertEqual(obj, py_obj)
def wrapper(self, *args, **kwargs): res = meth(self, *args, **kwargs) return python2lua(self.lua, res)
def python2lua(self, obj, **kwargs): return python2lua(self.lua, obj, **kwargs)
def python2lua(self, *args, **kwargs): return python2lua(self._lua, *args, **kwargs)
def assertSurvivesConversion(self, obj, encoding='utf8'): lua_obj = python2lua(self.lua, obj, encoding=encoding) py_obj = lua2python(self.lua, lua_obj, encoding=encoding) self.assertEqual(obj, py_obj) self.assertEqual(obj.__class__, py_obj.__class__)
def test_dict_recursive(self): dct = {} dct["x"] = dct with pytest.raises(ValueError): python2lua(self.lua, dct)
def wrapper(self, *args, **kwargs): res = meth(self, *args, **kwargs) if isinstance(res, tuple): return tuple(python2lua(self.lua, r) for r in res) else: return python2lua(self.lua, res)
def test_list_recursive(self): lst = [] lst.append(lst) with pytest.raises(ValueError): python2lua(self.lua, lst)