-
-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added option to pass already created LuaState #224
Open
xthebat
wants to merge
3
commits into
scoder:master
Choose a base branch
from
xthebat:lua-state
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import os | ||
import sys | ||
from pathlib import Path | ||
|
||
|
||
cdef extern from "Python.h": | ||
object PyLong_FromVoidPtr(void *p) | ||
|
||
|
||
runtime = None | ||
|
||
|
||
def _lua_eval_intern(code, *args): | ||
# In this scope LuaRuntime returns tuple of (<module_name>, <dll_path>, <return_val>) | ||
return runtime.eval(code, *args)[2] | ||
|
||
|
||
cdef public int initialize_lua_runtime(void* L): | ||
global runtime | ||
|
||
# Convert pointer to Python object to make possible pass it into LuaRuntime constructor | ||
state = PyLong_FromVoidPtr(L) | ||
|
||
print(f"Initialize LuaRuntime at proxy module with lua_State *L = {hex(state)}") | ||
|
||
from lupa import LuaRuntime | ||
|
||
# TODO: Make possible to configure others LuaRuntime options | ||
runtime = LuaRuntime(state=state, encoding="latin-1") | ||
|
||
|
||
cdef void setup_system_path(): | ||
# Add all lua interpreter 'require' paths to Python import paths | ||
paths: list[Path] = [Path(it) for it in _lua_eval_intern("package.path").split(";")] | ||
for path in set(it.parent for it in paths if it.parent.is_dir()): | ||
str_path = str(path) | ||
print(f"Append system path: '{str_path}'") | ||
sys.path.append(str_path) | ||
|
||
|
||
virtualenv_env_variable = "LUA_PYTHON_VIRTUAL_ENV" | ||
|
||
|
||
cdef void initialize_virtualenv(): | ||
virtualenv_path = os.environ.get(virtualenv_env_variable) | ||
if virtualenv_path is None: | ||
print(f"Environment variable '{virtualenv_env_variable}' not set, try to use system Python") | ||
return | ||
|
||
this_file = os.path.join(virtualenv_path, "Scripts", "activate_this.py") | ||
if not os.path.isfile(this_file): | ||
print(f"virtualenv at '{virtualenv_path}' seems corrupted, activation file '{this_file}' not found") | ||
return | ||
|
||
print(f"Activate virtualenv at {virtualenv_env_variable}='{virtualenv_path}'") | ||
exec(open(this_file).read(), {'__file__': this_file}) | ||
|
||
|
||
cdef public int embedded_initialize(void *L): | ||
initialize_virtualenv() | ||
initialize_lua_runtime(L) | ||
setup_system_path() | ||
return 1 | ||
|
||
|
||
cdef extern from *: | ||
""" | ||
PyMODINIT_FUNC PyInit_embedded(void); | ||
|
||
// use void* to make possible not to link proxy module with lua libraries | ||
#define LUA_ENTRY_POINT(x) __declspec(dllexport) int luaopen_ ## x (void *L) | ||
|
||
LUA_ENTRY_POINT(libpylua) { | ||
PyImport_AppendInittab("embedded", PyInit_embedded); | ||
Py_Initialize(); | ||
PyImport_ImportModule("embedded"); | ||
return embedded_initialize(L); | ||
} | ||
""" | ||
|
||
# Export proxy DLL name from this .pyd file | ||
# This name may be used by external Python script installer, e.g. | ||
# | ||
# from lupa import embedded | ||
# dylib_ext = get_dylib_ext_by_os() | ||
# dest_path = os.path.join(dest_dir, f"{embedded.lua_dylib_name}.{dylib_ext}") | ||
# shutil.copyfile(embedded.__file__, dest_path) | ||
lua_dylib_name = "libpylua" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'd better add some checks here, e.g.