Skip to content

Commit bd3afb1

Browse files
committed
Set Python program name during init
1 parent a00e641 commit bd3afb1

File tree

7 files changed

+25
-5
lines changed

7 files changed

+25
-5
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
branches:
66
- main
77
- "v*.*"
8+
- jk-init
89
jobs:
910
linux:
1011
runs-on: ubuntu-20.04

c_src/pythonx/python.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ DEF_SYMBOL(Py_IsFalse)
7777
DEF_SYMBOL(Py_IsNone)
7878
DEF_SYMBOL(Py_IsTrue)
7979
DEF_SYMBOL(Py_SetPythonHome)
80+
DEF_SYMBOL(Py_SetProgramName)
8081

8182
dl::LibraryHandle python_library;
8283

@@ -150,6 +151,7 @@ void load_python_library(std::string path) {
150151
LOAD_SYMBOL(python_library, Py_IsNone)
151152
LOAD_SYMBOL(python_library, Py_IsTrue)
152153
LOAD_SYMBOL(python_library, Py_SetPythonHome)
154+
LOAD_SYMBOL(python_library, Py_SetProgramName)
153155
}
154156

155157
void unload_python_library() {

c_src/pythonx/python.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ extern int (*Py_IsFalse)(PyObjectPtr);
131131
extern int (*Py_IsNone)(PyObjectPtr);
132132
extern int (*Py_IsTrue)(PyObjectPtr);
133133
extern void (*Py_SetPythonHome)(const wchar_t *);
134+
extern void (*Py_SetProgramName)(const wchar_t *);
134135

135136
// Opens Python dynamic library at the given path and looks up all
136137
// relevant symbols.

c_src/pythonx/pythonx.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ using namespace python;
2323
std::mutex init_mutex;
2424
bool is_initialized = false;
2525
std::wstring python_home_path_w;
26+
std::wstring python_executable_path_w;
2627
std::map<std::string, std::tuple<PyObjectPtr, PyObjectPtr>> compilation_cache;
2728
std::mutex compilation_cache_mutex;
2829

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

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

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

245+
python_executable_path_w =
246+
std::wstring(python_executable_path.data,
247+
python_executable_path.data + python_executable_path.size);
248+
243249
// As part of the initialization, sys.path is set. It is important
244250
// that it gets set correctly, so that the built-in modules can be
245251
// found, otherwise the initialization fails. This logic is internal
@@ -259,6 +265,8 @@ fine::Ok<> init(ErlNifEnv *env, std::string python_dl_path,
259265
// [1]: https://docs.python.org/3/using/cmdline.html#envvar-PYTHONHOME
260266
Py_SetPythonHome(python_home_path_w.c_str());
261267

268+
Py_SetProgramName(python_executable_path_w.c_str());
269+
262270
Py_InitializeEx(0);
263271

264272
// In order to use any of the Python C API functions, the calling

lib/pythonx.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ defmodule Pythonx do
3838
3939
"""
4040
@spec init(String.t(), String.t(), keyword()) :: :ok
41-
def init(python_dl_path, python_home_path, opts \\ [])
41+
def init(python_dl_path, python_home_path, python_executable_path, opts \\ [])
4242
when is_binary(python_dl_path) and is_binary(python_home_path) and is_list(opts) do
4343
opts = Keyword.validate!(opts, sys_paths: [])
4444

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

53-
Pythonx.NIF.init(python_dl_path, python_home_path, opts[:sys_paths])
53+
Pythonx.NIF.init(python_dl_path, python_home_path, python_executable_path, opts[:sys_paths])
5454
end
5555

5656
@doc ~S'''

lib/pythonx/nif.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ defmodule Pythonx.NIF do
1212
end
1313
end
1414

15-
def init(_python_dl_path, _python_home_path, _sys_paths), do: err!()
15+
def init(_python_dl_path, _python_home_path, _python_executable_path, _sys_paths), do: err!()
1616
def terminate(), do: err!()
1717
def janitor_decref(_ptr), do: err!()
1818
def none_new(), do: err!()

lib/pythonx/uv.ex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,16 @@ defmodule Pythonx.Uv do
106106

107107
python_home_path = make_windows_slashes(root_dir)
108108

109+
python_executable_path = Path.join(abs_executable_dir, "python.exe")
110+
109111
venv_packages_path =
110112
project_dir
111113
|> Path.join(".venv/Lib/site-packages")
112114
|> make_windows_slashes()
113115

114-
Pythonx.init(python_dl_path, python_home_path, sys_paths: [venv_packages_path])
116+
Pythonx.init(python_dl_path, python_home_path, python_executable_path,
117+
sys_paths: [venv_packages_path]
118+
)
115119

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

129133
python_home_path = root_dir
130134

135+
python_executable_path = Path.join(abs_executable_dir, "python")
136+
131137
venv_packages_path =
132138
project_dir
133139
|> Path.join(".venv/lib/python3*/site-packages")
134140
|> wildcard_one!()
135141

136-
Pythonx.init(python_dl_path, python_home_path, sys_paths: [venv_packages_path])
142+
Pythonx.init(python_dl_path, python_home_path, python_executable_path,
143+
sys_paths: [venv_packages_path]
144+
)
137145
end
138146
end
139147

0 commit comments

Comments
 (0)