diff --git a/.github/premake5.lua b/.github/premake5.lua index f4003e0..ceb95bc 100644 --- a/.github/premake5.lua +++ b/.github/premake5.lua @@ -6,11 +6,11 @@ project "pylua" language "C" targetdir "bin" - includedirs { "/usr/include/python3.8", "/usr/include/lua5.4" } + includedirs { "/usr/include/python3.10", "/usr/include/lua5.4" } files { "src/**.h", "src/**.c" } filter { "system:linux" } - links { "python3.8", "lua" } + links { "python3.10", "lua" } filter "configurations:Debug" buildoptions { "-ftest-coverage", "-fprofile-arcs" } diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 3e5b9c4..3e2ea2b 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -9,7 +9,7 @@ jobs: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 with: - python-version: '3.8' + python-version: '3.10' - uses: Vipul-Cariappa/lua-setup-action@main - name: Install Requirements diff --git a/.gitignore b/.gitignore index bf90f52..4bcffe8 100644 --- a/.gitignore +++ b/.gitignore @@ -736,3 +736,4 @@ pylua.make tmp* bin/ obj/ +.clangd/ diff --git a/README.md b/README.md index fdb63d4..0aac32f 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,9 @@ [![codecov](https://codecov.io/gh/Vipul-Cariappa/py-lua/branch/main/graph/badge.svg?token=LOUBWQJGF9)](https://codecov.io/gh/Vipul-Cariappa/py-lua) Bridge between Python and Lua. -This project has been started to provide easy binding between python and lua programming languages. Currently tested with lua version 5.4 and python version 3.8. +This project has been started to provide easy binding between python and lua programming languages. Currently tested with lua version 5.4 and python. -### Currently py-lua supports: +## Currently py-lua supports: - Importing python module into lua program. - Getting string, float, boolean and None data types from python. - Convertion between python dict and lua table. @@ -14,6 +14,7 @@ This project has been started to provide easy binding between python and lua pro - Support to use Object Oriented Programming between the languages. ## Example +### using python inside lua ```python # mymodule.py PI = 3.14 @@ -60,6 +61,30 @@ print(rect.get_area()) print(tostring(rect)) -- Rectangle(x=4.0, y=6.0) +``` +### Using lua inside python +```lua +-- mymodule.lua + +function celsius_to_fahrenheit(x) + return (x * 1.8) + 32 +end + +function fahrenheit_to_celsius(x) + return (x - 32) * .5556 +end +``` + +```python +# main.py +import pylua + +lua_module = pylua.LuaLoad("mymodule.lua") + +ctf_40 = lua_module.celsius_to_fahrenheit(40) +ftc_n40 = lua_module.fahrenheit_to_celsius(-40) + +print(f"{ctf_40 = }, {ftc_n40 = }") # ctf_40 = 104.0, ftc_n40 = -40.0032 ``` ## Building @@ -90,14 +115,6 @@ Copy the shared library pylua.so file to the working directory of your project. If you face any problems while building please ask for help [here](https://github.com/Vipul-Cariappa/py-lua/discussions/new). -## Yet to Implement -- [x] Simple data type convertions -- [x] Calling python functions from lua -- [x] Support list, tuple, dict and set -- [x] Support for generator functions -- [x] Support for working with python classes from lua -- [ ] Calling lua from python (lua bindings for python) - ## Contribution All contributions are welcomed. You can fork the project and create new PR, if you want to contribute. diff --git a/premake5.lua b/premake5.lua index 1e923ce..25d184e 100644 --- a/premake5.lua +++ b/premake5.lua @@ -1,3 +1,4 @@ +---@diagnostic disable: undefined-global workspace "py-lua" configurations { "Debug", "Release" } @@ -6,17 +7,16 @@ project "pylua" language "C" targetdir "bin/%{cfg.buildcfg}" - includedirs { "/path/to/python/header", "/path/to/lua/header" } - libdirs { "/path/to/python/shared_libraries", "/path/to/lua/shared_libraries" } files { "src/**.h", "src/**.c" } filter "configurations:Debug" - links { "python3.8_d", "lua5.4" } + includedirs { "/usr/local/include/python3.10", "/usr/include/lua5.4" } + links { "python3.10", "lua5.4" } defines { "DEBUG", "Py_DEBUG" } symbols "On" filter "configurations:Release" - links { "python3.8", "lua5.4" } - + includedirs { "/usr/include/python3.10", "/usr/include/lua5.4" } + links { "python3.10", "lua5.4" } defines { "NDEBUG" } optimize "On" diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..59deb8b --- /dev/null +++ b/setup.py @@ -0,0 +1,32 @@ +from distutils.core import setup, Extension + + +with open("README.md") as f: + module_discription = f.read() + +module = Extension( + "pylua", + sources=[ + "src/convert.c", + "src/lua_py.c", + "src/py_lua.c", + ], + include_dirs=[ + "src/", + "/usr/include/lua5.4", + ], + libraries=[ + "lua5.4", + ], +) + +setup( + name="pylua", + version="0.0.1", + description="General Purpose Binding between Python & lua", + ext_modules=[module], + author="Vipul Cariappa", + author_email="vipulcariappa@gmail.com", + url="https://github.com/Vipul-Cariappa/py-lua", + long_description=module_discription, +) diff --git a/src/lua_py.h b/src/lua_py.h index c9a0f80..b7d4b78 100644 --- a/src/lua_py.h +++ b/src/lua_py.h @@ -82,12 +82,14 @@ static int raise_error(lua_State* L, const char* msg) while (pTrace != NULL) { PyFrameObject* frame = pTrace->tb_frame; - PyCodeObject* code = frame->f_code; + PyCodeObject* code = PyFrame_GetCode(frame); int lineNr = PyFrame_GetLineNumber(frame); const char* codeName = PyUnicode_AsUTF8(code->co_name); const char* fileName = PyUnicode_AsUTF8(code->co_filename); + Py_DECREF(code); + if (snprintf(traceback_msg, TRACEBACK_STR_LEN, "File \"%s\", line %i, in\n %s\n", fileName, lineNr, codeName) < 0) { LUA_MEMORY_ERROR(L);