diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5f71721 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.dll +*.dylib +*.so +*.a +*.o diff --git a/Makefile.luarocks b/Makefile.luarocks new file mode 100644 index 0000000..d19e2d6 --- /dev/null +++ b/Makefile.luarocks @@ -0,0 +1,55 @@ +ifndef VERBOSE +.SILENT: +endif + +ifeq ($(OS), Windows_NT) +LIBEXT = .dll +LIBEXT_S = .dll.a +COPY = copy +RM = del +else ifeq ($(shell uname -s),Darwin) +LIBEXT = .dylib +LIBEXT_S = .a +COPY = cp -v +RM = rm -v -f +else +LIBEXT = .so +LIBEXT_S = .a +COPY = cp -v +RM = rm -v -f +endif + +TARGET = python$(LIBEXT) +TARGET_S = python$(LIBEXT_S) + +all: $(TARGET) $(TARGET_S) + +## + +CFLAGS := -Isrc -fPIC $(shell python3-config --embed --cflags) -DPYTHON_LIBRT=$(shell pkg-config python3-embed --keep-system-libs --libs | sed -e 's/-L//g' -e 's/ -l/\/lib/g')$(LIBEXT) + +LDFLAGS := $(shell python3-config --embed --ldflags) +ARFLAGS = $(shell python3-config --configdir)/libpython*$(LIBEXT_S) + +OBJS := \ + src/pythoninlua.o \ + src/luainpython.o + +$(TARGET): $(OBJS) + echo " LD $@" + $(LD) -shared -o $@ $^ $(LDFLAGS) + +$(TARGET_S): $(OBJS) + echo " AR $@" + ar r $@ $^ $(ARFLAGS) + +%.o: %.c + echo " CC $@" + $(CC) -c -o $@ $< $(CFLAGS) + +install: + $(COPY) $(TARGET) $(LIBDIR)/ + $(COPY) $(TARGET_S) $(LIBDIR)/ + +clean: + $(RM) $(TARGET) $(TARGET_S) $(OBJS) diff --git a/lunatic-python-scm-0.rockspec b/lunatic-python-scm-0.rockspec new file mode 100644 index 0000000..ce942a7 --- /dev/null +++ b/lunatic-python-scm-0.rockspec @@ -0,0 +1,17 @@ +package = 'lunatic-python' +version = 'scm-0' +source = { + url = 'git+https://github.com/bastibe/lunatic-python' +} +description = { + summary = 'Two-way bridge between Python and Lua', + homepage = 'https://github.com/bastibe/lunatic-python', + license = 'LGPL-2.1' +} +build = { + type = 'make', + makefile = 'Makefile.luarocks', + variables = { + LIBDIR = '$(LIBDIR)' + } +} diff --git a/src/pythoninlua.c b/src/pythoninlua.c index 464be43..0d04b2a 100644 --- a/src/pythoninlua.c +++ b/src/pythoninlua.c @@ -184,8 +184,8 @@ static int py_object_call(lua_State *L) ret = py_convert(L, value); Py_DECREF(value); } else { - char s_exc[1024] = {0}; - char s_traceback[1024] = {0}; + char s_exc[4096] = {0}; + char s_traceback[sizeof(s_exc)] = {0}; PyObject *exc_type, *exc_value, *exc_traceback; PyErr_Fetch(&exc_type, &exc_value, &exc_traceback); @@ -195,7 +195,7 @@ static int py_object_call(lua_State *L) // 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); + strncpy(s_exc, (!(exc_cstr)?"UNKNOWN ERROR\n":exc_cstr), sizeof(s_exc)-1); if (exc_value != NULL && exc_traceback != NULL) { PyObject *traceback_module = PyImport_ImportModule("traceback"); @@ -208,7 +208,7 @@ static int py_object_call(lua_State *L) // 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); + strncpy(s_traceback, traceback_cstr, sizeof(s_exc)-1); } Py_XDECREF(traceback_str); } @@ -665,12 +665,25 @@ LUA_API int luaopen_python(lua_State *L) if (!Py_IsInitialized()) { PyObject *luam, *mainm, *maind; +#if PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 11) + // Python 3.11+ syntax + char *argv[] = {"python3", 0}; + + PyConfig config; + PyConfig_InitPythonConfig(&config); + config.isolated = 1; + + PyConfig_SetBytesArgv(&config, 1, argv); +#else #if PY_MAJOR_VERSION >= 3 - wchar_t *argv[] = {L"", 0}; + // Python < 3.11 syntax + wchar_t *argv[] = {L"python3", 0}; #else - char *argv[] = {"", 0}; + // Python 2 syntax + char *argv[] = {"python2", 0}; #endif Py_SetProgramName(argv[0]); +#endif PyImport_AppendInittab("lua", PyInit_lua); /* Loading python library symbols so that dynamic extensions don't throw symbol not found error. @@ -686,8 +699,16 @@ LUA_API int luaopen_python(lua_State *L) assert(ok); (void) ok; #endif +#if PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 11) + // Python 3.11+ syntax + Py_InitializeFromConfig(&config); + PyConfig_Clear(&config); +#else + // Python 2, < 3.11 syntax Py_Initialize(); PySys_SetArgv(1, argv); +#endif + /* Import 'lua' automatically. */ luam = PyImport_ImportModule("lua"); if (!luam)