Skip to content

Commit

Permalink
updated for python3.11 (#23)
Browse files Browse the repository at this point in the history
* updated for python3.11

* Updated README.md
  • Loading branch information
Vipul-Cariappa authored Jan 16, 2023
1 parent 2c4b1a9 commit 9d00ad2
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -736,3 +736,4 @@ pylua.make
tmp*
bin/
obj/
.clangd/
37 changes: 27 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions premake5.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---@diagnostic disable: undefined-global
workspace "py-lua"
configurations { "Debug", "Release" }

Expand All @@ -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"
32 changes: 32 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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="[email protected]",
url="https://github.com/Vipul-Cariappa/py-lua",
long_description=module_discription,
)
4 changes: 3 additions & 1 deletion src/lua_py.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9d00ad2

Please sign in to comment.