Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def error_repr(e):
"""
Return repr of an exception, for printing as a cell execution result.
"""
if isinstance(e, (ScriptError, lupa.LuaSyntaxError, lupa.LuaError)):
if isinstance(e, ScriptError):
info = e.args[0]
tp = info['type']
else:
info = parse_error_message(e.args[0])
tp = ScriptError.SYNTAX_ERROR
line_num = info.get('line_number', -1)
message = info.get('error', info.get('message'))
return "%s [input]:%s: %s" % (tp, line_num, message)
elif isinstance(e, Exception):
return repr(e)
return ScriptError.UNKNOWN_ERROR
sandboxed=sandboxed,
lua_package_path=lua_package_path,
lua_sandbox_allowed_modules=lua_sandbox_allowed_modules
)
self.splash = Splash(self.lua, self.tab, self.render_options)
self.runner = SplashScriptRunner(
lua=self.lua,
splash=self.splash,
log=self.log,
sandboxed=sandboxed,
)
try:
main_coro = self.get_main(lua_source)
except (ValueError, lupa.LuaSyntaxError, lupa.LuaError) as e:
raise ScriptError("lua_source: " + repr(e))
self.runner.start(
main_coro=main_coro,
return_result=self.return_result,
return_error=self.return_error,
)
try:
try:
# XXX: this ugly formatting is important for exception
# line numbers to be displayed properly!
lua_source = 'local repr = require("repr"); function main(splash) return repr(%s) end' % code
main_coro = self._get_main(lua_source)
except lupa.LuaSyntaxError:
try:
lines = code.splitlines(False)
lua_source = '''local repr = require("repr"); function main(splash) %s
return repr(%s)
end
''' % ("\n".join(lines[:-1]), lines[-1])
main_coro = self._get_main(lua_source)
except lupa.LuaSyntaxError:
lua_source = "function main(splash) %s end" % code
main_coro = self._get_main(lua_source)
except (lupa.LuaSyntaxError, lupa.LuaError) as e:
d = defer.Deferred()
d.addCallbacks(success, error)
d.errback(e)
return d
except Exception:
d = defer.Deferred()
d.addCallbacks(success, error)
d.errback()
return d
d = self.runner.run(main_coro)
d.addCallbacks(success, error)