diff --git a/.travis.yml b/.travis.yml index a59d666..e3160e5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,21 +1,25 @@ language: c sudo: required -dist: trusty +dist: xenial compiler: - gcc env: - LUA_ENV=lua5.1 PY_ENV=python2.7 m_SUFFIX= + - LUA_ENV=lua5.2 PY_ENV=python2.7 m_SUFFIX= - LUA_ENV=lua5.3 PY_ENV=python2.7 m_SUFFIX= - LUA_ENV=lua5.1 PY_ENV=python3.6 m_SUFFIX=m + - LUA_ENV=lua5.2 PY_ENV=python3.6 m_SUFFIX=m - LUA_ENV=lua5.3 PY_ENV=python3.6 m_SUFFIX=m + - LUA_ENV=lua5.1 PY_ENV=python3.8 m_SUFFIX= + - LUA_ENV=lua5.2 PY_ENV=python3.8 m_SUFFIX= + - LUA_ENV=lua5.3 PY_ENV=python3.8 m_SUFFIX= before_install: - - sudo add-apt-repository -y "deb http://ppa.launchpad.net/grilo-team/travis/ubuntu trusty main" - - sudo add-apt-repository -y "deb http://ppa.launchpad.net/fkrull/deadsnakes/ubuntu trusty main" + - sudo add-apt-repository -y ppa:deadsnakes/ppa - sudo apt-get update -qq install: diff --git a/CMakeLists.txt b/CMakeLists.txt index 1422914..9dabdfa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,31 +1,29 @@ -cmake_minimum_required(VERSION 3.0.0) - -set(CMAKE_BUILD_TYPE_INIT "Release") -set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) -set(LIBRARY_OUTPUT_PATH ${EXECUTABLE_OUTPUT_PATH}) - - -project(Lunatic) - -find_package(Lua 5.1 REQUIRED) -find_package(PythonLibs 2.7 REQUIRED) - - -add_subdirectory(src) - -add_library(python MODULE $) -set_target_properties(python PROPERTIES - PREFIX "") - -add_library(lua MODULE $) -if (WIN32) - set_target_properties(lua PROPERTIES - PREFIX "" - SUFFIX ".pyd") -else (WIN32) - set_target_properties(lua PROPERTIES - PREFIX "") -endif (WIN32) - -target_link_libraries(lua ${LUA_LIBRARIES} ${PYTHON_LIBRARIES}) -target_link_libraries(python ${LUA_LIBRARIES} ${PYTHON_LIBRARIES}) +cmake_minimum_required(VERSION 3.12.0) + +set(CMAKE_BUILD_TYPE_INIT "Release") +set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) +set(LIBRARY_OUTPUT_PATH ${EXECUTABLE_OUTPUT_PATH}) + +project(Lunatic) + +find_package(Lua 5.1 REQUIRED) +find_package(Python REQUIRED COMPONENTS Interpreter Development) + +add_subdirectory(src) + +add_library(python MODULE $) +set_target_properties(python PROPERTIES + PREFIX "") + +add_library(lua MODULE $) +if (WIN32) + set_target_properties(lua PROPERTIES + PREFIX "" + SUFFIX ".pyd") +else (WIN32) + set_target_properties(lua PROPERTIES + PREFIX "") +endif (WIN32) + +target_link_libraries(lua ${LUA_LIBRARIES} ${Python_LIBRARIES}) +target_link_libraries(python ${LUA_LIBRARIES} ${Python_LIBRARIES}) diff --git a/README.md b/README.md index a525e2f..d5052b4 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ +# Lunatic-Python + [![Build Status](https://travis-ci.org/bastibe/lunatic-python.svg?branch=master)](https://travis-ci.org/bastibe/lunatic-python) +[![License: LGP-L2.1](https://img.shields.io/badge/license-LGPL%202.1-blue.svg)](https://opensource.org/licenses/LGPL-2.1) Details ======= @@ -14,7 +17,471 @@ Installing ---------- To install, you will need to have the Python and Lua development libraries on your system. If you -do, use the recommended methods (```pip```, ```easy-install```, etc) to install lunatic-python. +do, use the recommended methods (`pip`, `easy-install`, etc) to install lunatic-python. This version has been modified to compile under Ubuntu. I haven't tested it under other distributions, your mileage may vary. + +Introduction +------------ + +Lunatic Python is a two-way bridge between Python and Lua, allowing these languages to intercommunicate. Being two-way means that it allows Lua inside Python, Python inside Lua, Lua inside Python inside Lua, Python inside Lua inside Python, and so on. + +Why? + +Even though the project was born as an experiment, it's already being used in real world projects to integrate features from both languages. Please, let me know if you use it in real world projects. + +Examples + +Lua inside Python +A basic example. + +```lua +>>> import lua +>>> lg = lua.globals() +>>> lg.string + +>>> lg.string.lower + +>>> lg.string.lower("Hello world!") +'hello world!' +``` + +Now, let's put a local object into Lua space. + +```lua +>>> d = {} +>>> lg.d = d +>>> lua.execute("d['key'] = 'value'") +>>> d +{'key': 'value'} +Can we get the reference back from Lua space? +>>> d2 = lua.eval("d") +>>> d is d2 +True +``` + +Good! + +Is the python interface available inside the Lua interpreter? + +```lua +>>> lua.eval("python") + +``` + +Yes, it looks so. Let's nest some evaluations and see a local reference passing through. + +```python +>>> class MyClass: pass +... +>>> obj = MyClass() +>>> obj +<__main__.MyClass instance at 0x403ccb4c> +>>> lua.eval(r"python.eval('lua.eval(\"python.eval(\'obj\')\")')") +<__main__.MyClass instance at 0x403ccb4c> +``` + +Are you still following me? Good. Then you've probably noticed that the Python interpreter state inside the Lua interpreter state is the same as the outside Python we're running. Let's see that in a more comfortable way. + +```python +>>> lua.execute("pg = python.globals()") +>>> lua.eval("pg.obj") +<__main__.MyClass instance at 0x403ccb4c> +``` + +Things get more interesting when we start to really mix Lua and Python code. + +```python +>>> table = lua.eval("table") +>>> def show(key, value): +... print "key is %s and value is %s" % (`key`, `value`) +... +>>> t = lua.eval("{a=1, b=2, c=3}") +>>> table.foreach(t, show) +key is 'a' and value is 1 +key is 'c' and value is 3 +key is 'b' and value is 2 +>>> +``` + +Of course, in this case the same could be achieved easily with Python. + +```python +>>> def show(key, value): +... print "key is %s and value is %s" % (`key`, `value`) +... +>>> t = lua.eval("{a=1, b=2, c=3}") +>>> for k in t: +... show(k, t[k]) +... +key is 'a' and value is 1 +key is 'c' and value is 3 +key is 'b' and value is 2 +``` + +Python inside Lua +----------------- + +Now, let's have a look from another perspective. The basic idea is exactly the same. + +```python +> require("python") +> python.execute("import string") +> pg = python.globals() +> =pg.string + +> =pg.string.lower("Hello world!") +hello world! +``` + +As Lua is mainly an embedding language, getting access to the batteries included in Python may be interesting. + +```python +> re = python.import("re") +> pattern = re.compile("^Hel(lo) world!") +> match = pattern.match("Hello world!") +> =match.group(1) +lo +``` + +Just like in the Python example, let's put a local object in Python space. + +```python +> d = {} +> pg.d = d +> python.execute("d['key'] = 'value'") +> table.foreach(d, print) +key value +``` + +Again, let's grab back the reference from Python space. + +```python +> d2 = python.eval("d") +> print(d == d2) +true +``` + +Is the Lua interface available to Python? + +```pythom +> =python.eval("lua") + +``` + +Good. So let's do the nested trick in Lua as well. + +```python +> t = {} +> =t +table: 0x80fbdb8 +> =python.eval("lua.eval('python.eval(\"lua.eval(\\'t\\')\")')") +table: 0x80fbdb8 +> +``` + +It means that the Lua interpreter state inside the Python interpreter is the same as the outside Lua interpreter state. Let's show that in a more obvious way. + +```lua +> python.execute("lg = lua.globals()") +> =python.eval("lg.t") +table: 0x80fbdb8 +``` + +Now for the mixing example. + +```lua +> function notthree(num) +>> return (num ~= 3) +>> end +> l = python.eval("[1, 2, 3, 4, 5]") +> filter = python.eval("filter") +> =filter(notthree, l) +[1, 2, 4, 5] +``` + +Documentation +============= + +Theory +------ + +The bridging mechanism consists of creating the missing interpreter state inside the host interpreter. That is, when you run the bridging system inside Python, a Lua interpreter is created; when you run the system inside Lua, a Python interpreter is created. +Once both interpreter states are available, these interpreters are provided with the necessary tools to interact freely with each other. The given tools offer not only the ability of executing statements inside the alien interpreter, but also to acquire individual objects and interact with them inside the native state. This magic is done by two special object types, which act bridging native object access to the alien interpreter state. + +Almost every object which is passed between Python and Lua is encapsulated in the language specific bridging object type. The only types which are not encapsulated are strings and numbers, which are converted to the native equivalent objects. +Besides that, the Lua side also has special treatment for encapsulated Python functions and methods. The most obvious way to implement calling of Python objects inside the Lua interpreter is to implement a `__call` function in the bridging object metatable. Unfortunately this mechanism is not supported in certain situations, since some places test if the object type is a function, which is not the case of the bridging object. To overwhelm these problems, Python functions and methods are automatically converted to native Lua function closures, becoming accessible in every Lua context. Callable object instances which are not functions nor methods, on the other hand, will still use the metatable mechanism. Luckily, they may also be converted in a native function closure using the `asfunc()` function, if necessary. + +Attribute vs. Subscript object access +------------------------------------- + +Accessing an attribute or using the subscript operator in Lua give access to the same information. This behavior is reflected in the Python special object that encapsulates Lua objects, allowing Lua tables to be accessed in a more comfortable way, and also giving access to objects which use protected Python keywords (such as the print function). For example: + +```python +>>> string = lua.eval("string") +>>> string.lower + +>>> string["lower"] + +``` + +Using Python from the Lua side requires a little bit more attention, since Python has a more strict syntax than Lua. The later makes no distinction between attribute and subscript access, so we need some way to know what kind of access is desired at a given moment. This control is provided using two functions: `asindx()` and `asattr()`. These functions receive a single Python object as parameter, and return the same object with the given access discipline. Notice that dictionaries and lists use the index discipline by default, while other objects use the attribute discipline. For example: + +```python +> dict = python.eval("{}") +> =dict.keys +nil +> dict.keys = 10 +> print(dict["keys"]) +10 +> =dict +{'keys': 10} +> =dict.keys = 10 +n.asattr(dict) +> =dict.keys +function: 0x80fa9b8 +> =dict.keys() +['keys'] +``` + +Lua inside Python +----------------- + +When executing Python as the host language, the Lua functionality is accessed by importing the lua module. When Lua is the host language, the lua module will already be available in the global Python scope. +Below is a description of the functions available in the lua module. + +```python +lua.execute(statement) +``` + +This function will execute the given statement inside the Lua interpreter state. +Examples: + +```lua +>>> lua.execute("foo = 'bar'") +lua.eval(expression) +``` + +This function will evaluate the given expression inside the Lua interpreter state, and return the result. It may be used to acquire any object from the Lua interpreter state. +Examples: + +```lua +>>> lua.eval("'foo'..2") +'foo2' +>>> lua.eval('string') + +>>> string = lua.eval('string') +>>> string.lower("Hello world!") +'hello world!' +``` + +```lua +lua.globals() +``` + +Return the Lua global scope from the interpreter state. + +Examples: + +```lua +>>> lg = lua.globals() +>>> lg.string.lower("Hello world!") +'hello world!' +>>> lg["string"].lower("Hello world!") +'hello world!' +>>> lg["print"] + +>>> lg["print"]("Hello world!") +Hello world! +``` + +```lua +lua.require(name) +``` + +Executes the require() Lua function, importing the given module. + +Examples: +```lua +>>> lua.require("testmod") +True +>>> lua.execute("func()") +I'm func in testmod! +``` + +Python inside Lua +----------------- + +Unlike Python, Lua has no default path to its modules. Thus, the default path of the real Lua module of Lunatic Python is together with the Python module, and a python.lua stub is provided. This stub must be placed in a path accessible by the Lua require() mechanism, and once imported it will locate the real module and load it. + +Unfortunately, there's a minor inconvenience for our purposes regarding the Lua system which imports external shared objects. The hardcoded behavior of the loadlib() function is to load shared objects without exporting their symbols. This is usually not a problem in the Lua world, but we're going a little beyond their usual requirements here. We're loading the Python interpreter as a shared object, and the Python interpreter may load its own external modules which are compiled as shared objects as well, and these will want to link back to the symbols in the Python interpreter. Luckily, fixing this problem is easier than explaining the problem. It's just a matter of replacing the flag RTLD_NOW in the loadlib.c file of the Lua distribution by the or'ed version RTLD_NOW|RTLD_GLOBAL. This will avoid "undefined symbol" errors which could eventually happen. +Below is a description of the functions available in the python module. + +```python +python.execute(statement) +``` + +This function will execute the given statement inside the Python interpreter state. +Examples: + +```python +> python.execute("foo = 'bar'") +``` + +```python +python.eval(expression) +``` + +This function will evaluate the given expression inside the Python interpreter state, and return the result. It may be used to acquire any object from the Python interpreter state. + +Examples: + +```python +> python.execute("import string") +> =python.eval("string") + +> string = python.eval("string") +> =string.lower("Hello world!") +hello world! +``` + +```python +python.globals() +``` + +Return the Python global scope dictionary from the interpreter state. +Examples: + +```python +> python.execute("import string") +> pg = python.globals() +> =pg.string.lower("Hello world!") +hello world! +> =pg["string"].lower("Hello world!") +hello world! +``` + +```python +python.locals() +``` + +Return the Python local scope dictionary from the interpreter state. +Examples: + +```python +> function luafunc() +>> print(python.globals().var) +>> print(python.locals().var) +>> end +> python.execute("def func():\n var = 'value'\n lua.execute('luafunc()')") +> python.execute("func()") +nil +value +``` + +```python +python.builtins() +``` + +Return the Python builtins module dictionary from the interpreter state. +Examples: + +```python +> pb = python.builtins() +> =pb.len("Hello world!") +12 +``` + +```python +python.import(name) +``` + +Imports and returns the given Python module. +Examples: + +```python +> os = python.import("os") +> =os.getcwd() +/home/niemeyer/src/lunatic-python +``` + +```python +python.asattr(pyobj) +``` + +Return a copy of the given Python object with an attribute access discipline. +Examples: + +```python +> dict = python.eval("{}") +> =dict.keys +nil +> dict.keys = 10 +> print(dict["keys"]) +10 +> =dict +{'keys': 10} +> =dict.keys = 10 +n.asattr(dict) +> =dict.keys +function: 0x80fa9b8 +> =dict.keys() +['keys'] +``` + +```python +python.asindx(pyobj) +``` + +Return a copy of the given Python object with an index access discipline. +Examples: + +```python +> buffer = python.eval("buffer('foobar')") +> =buffer[0] +stdin:1: unknown attribute in python object +stack traceback: + [C]: ? + stdin:1: in main chunk + [C]: ? +> buffer = python.asindx(buffer) +> =buffer[0] +f +``` + +```python +python.asfunc(pyobj) +``` + +Return a copy of the given Python object enclosed in a Lua function closure. This is useful to use Python callable instances in places that require a Lua function. Python methods and functions are automatically converted to Lua functions, and don't require to be explicitly converted. +Examples: + +```python +> python.execute("class Join:\n def __call__(self, *args):\n return '-'.join(args)") +> join = python.eval("Join()") +> =join +<__main__.Join instance at 0x403a864c> +> =join('foo', 'bar') +foo-bar +> =table.foreach({foo='bar'}, join) +stdin:1: bad argument #2 to `foreach' (function expected, got userdata) +stack traceback: + [C]: in function `foreach' + stdin:1: in main chunk + [C]: ? +> =table.foreach({foo='bar'}, python.asfunc(join)) +foo-bar +``` + +License +------- + +Lunatic Python is available under the LGPL license. + +Download +-------- +Available files: +• lunatic-python-1.0.tar.bz2 +Author +Gustavo Niemeyer diff --git a/setup.py b/setup.py index 2fee368..9f0496d 100755 --- a/setup.py +++ b/setup.py @@ -18,8 +18,15 @@ if os.path.isfile("MANIFEST"): os.unlink("MANIFEST") +presult, poutput = commands.getstatusoutput("pkg-config --exists lua5.2") +HAS_LUA5_2 = (presult == 0) + # You may have to change these -LUAVERSION = "5.2" +if HAS_LUA5_2: + LUAVERSION = "5.2" +else: + LUAVERSION = "5.1" + PYTHONVERSION = get_python_version() PYLIBS = ["python" + get_python_version(), "pthread", "util"] PYLIBDIR = [get_python_lib(standard_lib=True) + "/config"] diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b4664b5..3c84b54 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,14 +1,19 @@ -add_library(src OBJECT luainpython.c pythoninlua.c) -set_target_properties(src PROPERTIES - POSITION_INDEPENDENT_CODE TRUE) - -target_include_directories(src PRIVATE ${LUA_INCLUDE_DIR} ${PYTHON_INCLUDE_DIR}) - -target_compile_definitions(src PRIVATE LUA_LIB) -if (WIN32) - target_compile_definitions(src PRIVATE LUA_BUILD_AS_DLL) -endif (WIN32) - -if (CMAKE_COMPILER_IS_GNUCC) - target_compile_options(src PUBLIC -Wall -pedantic -std=c99) -endif (CMAKE_COMPILER_IS_GNUCC) +add_library(src OBJECT luainpython.c pythoninlua.c) +set_target_properties(src PROPERTIES + POSITION_INDEPENDENT_CODE TRUE) + +target_include_directories(src PRIVATE ${LUA_INCLUDE_DIR} ${Python_INCLUDE_DIRS}) + +target_compile_definitions(src PRIVATE LUA_LIB) +if (WIN32) + target_compile_definitions(src PRIVATE LUA_BUILD_AS_DLL) +endif (WIN32) + +if (UNIX) + get_filename_component(PYTHON_LIBRT ${Python_LIBRARIES} NAME) + target_compile_definitions(src PRIVATE PYTHON_LIBRT=${PYTHON_LIBRT}) +endif (UNIX) + +if (CMAKE_COMPILER_IS_GNUCC) + target_compile_options(src PUBLIC -Wall -pedantic -std=c99) +endif (CMAKE_COMPILER_IS_GNUCC) diff --git a/src/luainpython.c b/src/luainpython.c index c55c6ab..52adb9b 100644 --- a/src/luainpython.c +++ b/src/luainpython.c @@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#define PY_SSIZE_T_CLEAN #include /* need this to build with Lua 5.2: enables lua_strlen() macro */ @@ -110,7 +111,12 @@ static PyObject *LuaCall(lua_State *L, PyObject *args) { PyObject *ret = NULL; PyObject *arg; - int nargs, rc, i; +#ifdef PY_SSIZE_T_CLEAN + Py_ssize_t nargs, i; +#else + int nargs, i; +#endif + int rc; if (!PyTuple_Check(args)) { PyErr_SetString(PyExc_TypeError, "tuple expected"); @@ -302,6 +308,9 @@ static PyObject *LuaObject_str(PyObject *obj) } #if LUA_VERSION_NUM == 501 +#ifdef LUA_OK // defined in LuaJIT +#undef LUA_OK +#endif enum { LUA_OK, LUA_OPEQ, LUA_OPLT, LUA_OPLE, @@ -358,7 +367,7 @@ static PyObject* LuaObject_richcmp(PyObject *lhs, PyObject *rhs, int op) PyErr_SetString(PyExc_RuntimeError, lua_tostring(LuaState, -1)); return NULL; } - return lua_toboolean(LuaState, -1) ? Py_True : Py_False; + return LuaConvert(LuaState, -1); } static PyObject *LuaObject_call(PyObject *obj, PyObject *args) @@ -396,9 +405,17 @@ static PyObject *LuaObject_iternext(LuaObject *obj) return ret; } +#ifdef PY_SSIZE_T_CLEAN +static Py_ssize_t LuaObject_length(LuaObject *obj) +#else static int LuaObject_length(LuaObject *obj) +#endif { +#ifdef PY_SSIZE_T_CLEAN + Py_ssize_t len; +#else int len; +#endif lua_rawgeti(LuaState, LUA_REGISTRYINDEX, ((LuaObject*)obj)->ref); len = luaL_len(LuaState, -1); lua_settop(LuaState, 0); @@ -428,54 +445,35 @@ static PyMappingMethods LuaObject_as_mapping = { PyTypeObject LuaObject_Type = { PyVarObject_HEAD_INIT(NULL, 0) - "lua.custom", /*tp_name*/ - sizeof(LuaObject), /*tp_basicsize*/ - 0, /*tp_itemsize*/ - (destructor)LuaObject_dealloc, /*tp_dealloc*/ - 0, /*tp_print*/ - 0, /*tp_getattr*/ - 0, /*tp_setattr*/ - 0, /*tp_compare*/ - LuaObject_str, /*tp_repr*/ - 0, /*tp_as_number*/ - 0, /*tp_as_sequence*/ - &LuaObject_as_mapping, /*tp_as_mapping*/ - 0, /*tp_hash*/ - (ternaryfunc)LuaObject_call, /*tp_call*/ - LuaObject_str, /*tp_str*/ - LuaObject_getattr, /*tp_getattro*/ - LuaObject_setattr, /*tp_setattro*/ - 0, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ - "custom lua object", /*tp_doc*/ - 0, /*tp_traverse*/ - 0, /*tp_clear*/ - LuaObject_richcmp, /*tp_richcompare*/ - 0, /*tp_weaklistoffset*/ - PyObject_SelfIter, /*tp_iter*/ - (iternextfunc)LuaObject_iternext, /*tp_iternext*/ - 0, /*tp_methods*/ - 0, /*tp_members*/ - 0, /*tp_getset*/ - 0, /*tp_base*/ - 0, /*tp_dict*/ - 0, /*tp_descr_get*/ - 0, /*tp_descr_set*/ - 0, /*tp_dictoffset*/ - 0, /*tp_init*/ - PyType_GenericAlloc, /*tp_alloc*/ - PyType_GenericNew, /*tp_new*/ - PyObject_Del, /*tp_free*/ - 0, /*tp_is_gc*/ + .tp_name = "lua.custom", + .tp_basicsize = sizeof(LuaObject), + .tp_dealloc = (destructor)LuaObject_dealloc, + .tp_repr = LuaObject_str, + .tp_as_mapping = &LuaObject_as_mapping, + .tp_call = (ternaryfunc)LuaObject_call, + .tp_str = LuaObject_str, + .tp_getattro = LuaObject_getattr, + .tp_setattro = LuaObject_setattr, + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, + .tp_doc = "custom lua object", + .tp_richcompare = LuaObject_richcmp, + .tp_iter = PyObject_SelfIter, + .tp_iternext = (iternextfunc)LuaObject_iternext, + .tp_alloc = PyType_GenericAlloc, + .tp_new = PyType_GenericNew, + .tp_free = PyObject_Del, }; - PyObject *Lua_run(PyObject *args, int eval) { PyObject *ret; char *buf = NULL; char *s; +#ifdef PY_SSIZE_T_CLEAN + Py_ssize_t len; +#else int len; +#endif if (!PyArg_ParseTuple(args, "s#", &s, &len)) return NULL; diff --git a/src/luainpython.h b/src/luainpython.h index 73ea65c..c5a841e 100644 --- a/src/luainpython.h +++ b/src/luainpython.h @@ -26,6 +26,9 @@ #if LUA_VERSION_NUM == 501 #define luaL_len lua_objlen #define luaL_setfuncs(L, l, nup) luaL_register(L, NULL, (l)) + #ifdef luaL_newlib // defined in LuaJIT + #undef luaL_newlib + #endif #define luaL_newlib(L, l) (lua_newtable(L), luaL_register(L, NULL, (l))) #endif diff --git a/src/pythoninlua.c b/src/pythoninlua.c index f5b5bd5..464be43 100644 --- a/src/pythoninlua.c +++ b/src/pythoninlua.c @@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + #include #if defined(__linux__) # include @@ -115,6 +116,7 @@ static int py_object_call(lua_State *L) // passing a single table forces named keyword call style, e.g. plt.plot{x, y, c='red'} if (nargs==1 && lua_istable(L, 2)) { lua_pushnil(L); /* first key */ + nargs=0; while (lua_next(L, 2) != 0) { if (lua_isnumber(L, -2)) { int i = lua_tointeger(L, -2); @@ -182,8 +184,51 @@ static int py_object_call(lua_State *L) ret = py_convert(L, value); Py_DECREF(value); } else { - PyErr_Print(); - luaL_error(L, "error calling python function"); + char s_exc[1024] = {0}; + char s_traceback[1024] = {0}; + + PyObject *exc_type, *exc_value, *exc_traceback; + PyErr_Fetch(&exc_type, &exc_value, &exc_traceback); + PyErr_NormalizeException(&exc_type, &exc_value, &exc_traceback); + + PyObject *exc_str = PyObject_Str(exc_value); + + // Need not be garbage collected as per documentation of PyUnicode_AsUTF8 + const char *exc_cstr = (exc_str)?PyUnicode_AsUTF8(exc_str):""; + strncpy(s_exc, (!(exc_cstr)?"UNKNOWN ERROR\n":exc_cstr), 1023); + + if (exc_value != NULL && exc_traceback != NULL) { + PyObject *traceback_module = PyImport_ImportModule("traceback"); + if (traceback_module != NULL) { + PyObject *traceback_list = PyObject_CallMethod(traceback_module, + "format_exception", "OOO", exc_type, exc_value, exc_traceback); + if (traceback_list != NULL) { + PyObject *traceback_str = PyUnicode_Join(PyUnicode_FromString(""), traceback_list); + if (traceback_str != NULL) { + // Need not be garbage collected as per documentation of PyUnicode_AsUTF8 + const char *traceback_cstr = PyUnicode_AsUTF8(traceback_str); + if (traceback_cstr != NULL) { + strncpy(s_traceback, traceback_cstr, 1023); + } + Py_XDECREF(traceback_str); + } + Py_XDECREF(traceback_list); + } + Py_XDECREF(traceback_module); + } + } + Py_XDECREF(exc_type); + Py_XDECREF(exc_value); + Py_XDECREF(exc_traceback); + Py_XDECREF(exc_str); + + if (*s_traceback == '\0') { + luaL_error(L, "error calling python function:\nException: %s", s_exc); + } + else { + luaL_error(L, "error calling python function:\nException: %s", s_traceback); + } + } return ret; @@ -271,6 +316,9 @@ static int _p_object_index_get(lua_State *L, py_object *obj, int keyn) } item = PyObject_GetItem(obj->o, key); + if (!item) { + item = PyObject_GetAttr(obj->o, key); + } Py_DECREF(key); @@ -354,7 +402,7 @@ static int py_object_tostring(lua_State *L) if (!repr) { char buf[256]; - snprintf(buf, 256, "python object: %p", obj->o); + snprintf(buf, 256, "python object: %p", (void *)obj->o); lua_pushstring(L, buf); PyErr_Clear(); } @@ -630,12 +678,12 @@ LUA_API int luaopen_python(lua_State *L) */ #if defined(__linux__) # define STR(s) #s -#if PY_MAJOR_VERSION < 3 -# define PYLIB_STR(major, minor) "libpython" STR(major) "." STR(minor) ".so" -#else -# define PYLIB_STR(major, minor) "libpython" STR(major) "." STR(minor) "m.so" +# define PYLIB_STR(s) STR(s) +#if !defined(PYTHON_LIBRT) +# error PYTHON_LIBRT must be defined when building under Linux! #endif - dlopen(PYLIB_STR(PY_MAJOR_VERSION, PY_MINOR_VERSION), RTLD_NOW | RTLD_GLOBAL); + void *ok = dlopen(PYLIB_STR(PYTHON_LIBRT), RTLD_NOW | RTLD_GLOBAL); + assert(ok); (void) ok; #endif Py_Initialize(); diff --git a/tests/test_py.lua b/tests/test_py.lua index a5f4d28..2467596 100644 --- a/tests/test_py.lua +++ b/tests/test_py.lua @@ -40,3 +40,28 @@ bar = 2 assert(python.globals().foo == 1) assert(python.globals().bar == 2) + +python.execute +[[ +def throw_exc(): + raise Exception("THIS EXCEPTION") +]] + +local status, exc = pcall(python.globals().throw_exc) +assert(status == false) +assert(exc == +[[error calling python function: +Exception: Traceback (most recent call last): + File "", line 2, in throw_exc +Exception: THIS EXCEPTION +]], exc) + +local b, e = string.find(exc, "Exception: ", 1); +local ob, oe = b, e; +while (b ~= nil) do + ob, oe = b, e + b, e = string.find(exc, "Exception: ", e+1) +end +local exc_s = (string.sub(exc, oe+1)) +--assert((require "pl.stringx").strip(exc_s) == "THIS EXCEPTION"); +