-
-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose Lua load arguments (mode and chunkname) #248
Comments
This is a patch that I wrote to expose the chunk name (it works for my needs). I don't have time right now to open a PR, but I'll leave the changes here in case anyone needs them: diff --git a/lupa/_lupa.pyx b/lupa/_lupa.pyx
index 9310e35..34a4d3b 100644
--- a/lupa/_lupa.pyx
+++ b/lupa/_lupa.pyx
@@ -393,36 +393,45 @@ cdef class LuaRuntime:
raise
return 0
- def eval(self, lua_code, *args):
+ def eval(self, lua_code, name = '<python>', *args):
"""Evaluate a Lua expression passed in a string.
"""
assert self._state is not NULL
if isinstance(lua_code, unicode):
lua_code = (<unicode>lua_code).encode(self._source_encoding)
- return run_lua(self, b'return ' + lua_code, args)
+ if not isinstance(name, (bytes, unicode)):
+ raise TypeError("Lua program name must be a string")
+ name = (<unicode>name).encode('UTF-8')
+ return run_lua(self, b'return ' + lua_code, name, args)
- def execute(self, lua_code, *args):
+ def execute(self, lua_code, name = '<python>', *args):
"""Execute a Lua program passed in a string.
"""
assert self._state is not NULL
if isinstance(lua_code, unicode):
lua_code = (<unicode>lua_code).encode(self._source_encoding)
- return run_lua(self, lua_code, args)
+ if not isinstance(name, (bytes, unicode)):
+ raise TypeError("Lua program name must be a string")
+ name = (<unicode>name).encode('UTF-8')
+ return run_lua(self, lua_code, name, args)
- def compile(self, lua_code):
+ def compile(self, lua_code, name = '<python>'):
"""Compile a Lua program into a callable Lua function.
"""
assert self._state is not NULL
cdef const char *err
if isinstance(lua_code, unicode):
lua_code = (<unicode>lua_code).encode(self._source_encoding)
+ if not isinstance(name, (bytes, unicode)):
+ raise TypeError("Lua program name must be a string")
+ name = (<unicode>name).encode('UTF-8')
L = self._state
lock_runtime(self)
old_top = lua.lua_gettop(L)
cdef size_t size
try:
check_lua_stack(L, 1)
- status = lua.luaL_loadbuffer(L, lua_code, len(lua_code), b'<python>')
+ status = lua.luaL_loadbuffer(L, lua_code, len(lua_code), name)
if status == 0:
return py_from_lua(self, L, -1)
else:
@@ -1719,14 +1728,14 @@ cdef build_lua_error_message(LuaRuntime runtime, lua_State* L, int stack_index=-
# calling into Lua
-cdef run_lua(LuaRuntime runtime, bytes lua_code, tuple args):
+cdef run_lua(LuaRuntime runtime, bytes lua_code, bytes name, tuple args):
"""Run Lua code with arguments"""
cdef lua_State* L = runtime._state
lock_runtime(runtime)
old_top = lua.lua_gettop(L)
try:
check_lua_stack(L, 1)
- if lua.luaL_loadbuffer(L, lua_code, len(lua_code), '<python>'):
+ if lua.luaL_loadbuffer(L, lua_code, len(lua_code), name):
error = build_lua_error_message(runtime, L)
if error.startswith("not enough memory"):
raise LuaMemoryError(error) |
Note that you cannot cast a |
Which line are you referring to? Because the Please help me fix the code if needed, thank you. |
A pattern that you could use is:
That way, you end up with a EDIT: Although, this would still fail for subtypes of
|
I would suggest changing the default chunk name to either To compare the results, let say you call
|
BTW, according to the Lua documentation: |
@astoff, could you explain why you'd need to change the |
This is correct. I guess the only caveat is that feeding in some malformed bytecode can crash the interpreter.
Well, Lua doesn't care about string vs. bytes. The way it works internally is this: normally (mode=NULL or "bt"), Lua will first byte-compile the code, then load it. However, if the code starts with With mode = "t" (respectively mode = "b"), Lua will raise an error if the code string starts with (respectively doesn't start with) However, the fact that bytecode is marked by an initial |
…to allow finer control of input and debug output. Closes #248
Ok, got it. #252 adds these options. |
It would be nice to expose the mode and chunkname arguments of Lua's load function in
LuaRunner.{eval,execute,compile}
.Setting mode to
"t"
is important for sanboxing purposes. Specifying the chunk name is less important by I guess it's nice to have.The text was updated successfully, but these errors were encountered: