Skip to content

Commit

Permalink
Set Python program name during init
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanklosko committed Feb 21, 2025
1 parent a00e641 commit bd3afb1
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches:
- main
- "v*.*"
- jk-init
jobs:
linux:
runs-on: ubuntu-20.04
Expand Down
2 changes: 2 additions & 0 deletions c_src/pythonx/python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ DEF_SYMBOL(Py_IsFalse)
DEF_SYMBOL(Py_IsNone)
DEF_SYMBOL(Py_IsTrue)
DEF_SYMBOL(Py_SetPythonHome)
DEF_SYMBOL(Py_SetProgramName)

dl::LibraryHandle python_library;

Expand Down Expand Up @@ -150,6 +151,7 @@ void load_python_library(std::string path) {
LOAD_SYMBOL(python_library, Py_IsNone)
LOAD_SYMBOL(python_library, Py_IsTrue)
LOAD_SYMBOL(python_library, Py_SetPythonHome)
LOAD_SYMBOL(python_library, Py_SetProgramName)
}

void unload_python_library() {
Expand Down
1 change: 1 addition & 0 deletions c_src/pythonx/python.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ extern int (*Py_IsFalse)(PyObjectPtr);
extern int (*Py_IsNone)(PyObjectPtr);
extern int (*Py_IsTrue)(PyObjectPtr);
extern void (*Py_SetPythonHome)(const wchar_t *);
extern void (*Py_SetProgramName)(const wchar_t *);

// Opens Python dynamic library at the given path and looks up all
// relevant symbols.
Expand Down
8 changes: 8 additions & 0 deletions c_src/pythonx/pythonx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ using namespace python;
std::mutex init_mutex;
bool is_initialized = false;
std::wstring python_home_path_w;
std::wstring python_executable_path_w;
std::map<std::string, std::tuple<PyObjectPtr, PyObjectPtr>> compilation_cache;
std::mutex compilation_cache_mutex;

Expand Down Expand Up @@ -225,6 +226,7 @@ ERL_NIF_TERM py_object_to_binary_term(ErlNifEnv *env, PyObjectPtr py_object) {

fine::Ok<> init(ErlNifEnv *env, std::string python_dl_path,
ErlNifBinary python_home_path,
ErlNifBinary python_executable_path,
std::vector<ErlNifBinary> sys_paths) {
auto init_guard = std::lock_guard<std::mutex>(init_mutex);

Expand All @@ -240,6 +242,10 @@ fine::Ok<> init(ErlNifEnv *env, std::string python_dl_path,
python_home_path_w = std::wstring(
python_home_path.data, python_home_path.data + python_home_path.size);

python_executable_path_w =
std::wstring(python_executable_path.data,
python_executable_path.data + python_executable_path.size);

// As part of the initialization, sys.path is set. It is important
// that it gets set correctly, so that the built-in modules can be
// found, otherwise the initialization fails. This logic is internal
Expand All @@ -259,6 +265,8 @@ fine::Ok<> init(ErlNifEnv *env, std::string python_dl_path,
// [1]: https://docs.python.org/3/using/cmdline.html#envvar-PYTHONHOME
Py_SetPythonHome(python_home_path_w.c_str());

Py_SetProgramName(python_executable_path_w.c_str());

Py_InitializeEx(0);

// In order to use any of the Python C API functions, the calling
Expand Down
4 changes: 2 additions & 2 deletions lib/pythonx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Pythonx do
"""
@spec init(String.t(), String.t(), keyword()) :: :ok
def init(python_dl_path, python_home_path, opts \\ [])
def init(python_dl_path, python_home_path, python_executable_path, opts \\ [])
when is_binary(python_dl_path) and is_binary(python_home_path) and is_list(opts) do
opts = Keyword.validate!(opts, sys_paths: [])

Expand All @@ -50,7 +50,7 @@ defmodule Pythonx do
raise ArgumentError, "the given python home directory does not exist: #{python_home_path}"
end

Pythonx.NIF.init(python_dl_path, python_home_path, opts[:sys_paths])
Pythonx.NIF.init(python_dl_path, python_home_path, python_executable_path, opts[:sys_paths])
end

@doc ~S'''
Expand Down
2 changes: 1 addition & 1 deletion lib/pythonx/nif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Pythonx.NIF do
end
end

def init(_python_dl_path, _python_home_path, _sys_paths), do: err!()
def init(_python_dl_path, _python_home_path, _python_executable_path, _sys_paths), do: err!()
def terminate(), do: err!()
def janitor_decref(_ptr), do: err!()
def none_new(), do: err!()
Expand Down
12 changes: 10 additions & 2 deletions lib/pythonx/uv.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,16 @@ defmodule Pythonx.Uv do

python_home_path = make_windows_slashes(root_dir)

python_executable_path = Path.join(abs_executable_dir, "python.exe")

venv_packages_path =
project_dir
|> Path.join(".venv/Lib/site-packages")
|> make_windows_slashes()

Pythonx.init(python_dl_path, python_home_path, sys_paths: [venv_packages_path])
Pythonx.init(python_dl_path, python_home_path, python_executable_path,
sys_paths: [venv_packages_path]
)

{:unix, osname} ->
dl_extension =
Expand All @@ -128,12 +132,16 @@ defmodule Pythonx.Uv do

python_home_path = root_dir

python_executable_path = Path.join(abs_executable_dir, "python")

venv_packages_path =
project_dir
|> Path.join(".venv/lib/python3*/site-packages")
|> wildcard_one!()

Pythonx.init(python_dl_path, python_home_path, sys_paths: [venv_packages_path])
Pythonx.init(python_dl_path, python_home_path, python_executable_path,
sys_paths: [venv_packages_path]
)
end
end

Expand Down

0 comments on commit bd3afb1

Please sign in to comment.