Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.dll
*.dylib
*.so
*.a
*.o
55 changes: 55 additions & 0 deletions Makefile.luarocks
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions lunatic-python-scm-0.rockspec
Original file line number Diff line number Diff line change
@@ -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)'
}
}
33 changes: 27 additions & 6 deletions src/pythoninlua.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
Expand All @@ -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);
}
Expand Down Expand Up @@ -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"<lua>", 0};
// Python < 3.11 syntax
wchar_t *argv[] = {L"python3", 0};
#else
char *argv[] = {"<lua>", 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.
Expand All @@ -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)
Expand Down