Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit 17c9f53

Browse files
committed
python: expose Py_SetPythonHome and Py_GetPythonHome
Fixes #71.
1 parent a69309a commit 17c9f53

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

init.go

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package python
22

33
// #include "go-python.h"
44
// char *gopy_ProgName = NULL;
5+
// char *gopy_PythonHome = NULL;
56
import "C"
67

78
import (
@@ -18,6 +19,7 @@ import (
1819
// will not change for the duration of the program’s execution.
1920
// No code in the Python interpreter will change the contents of this storage.
2021
func Py_SetProgramName(name string) {
22+
C.free(unsafe.Pointer(C.gopy_ProgName))
2123
C.gopy_ProgName = C.CString(name)
2224
C.Py_SetProgramName(C.gopy_ProgName)
2325
}
@@ -41,3 +43,18 @@ func PySys_SetArgv(argv []string) {
4143
}
4244
C.PySys_SetArgv(argc, &cargs[0])
4345
}
46+
47+
// Set the default “home” directory, that is, the location of the standard Python libraries. See PYTHONHOME for the meaning of the argument string.
48+
//
49+
// The argument should point to a zero-terminated character string in static storage whose contents will not change for the duration of the program’s execution. No code in the Python interpreter will change the contents of this storage.
50+
func Py_SetPythonHome(home string) {
51+
C.free(unsafe.Pointer(C.gopy_PythonHome))
52+
C.gopy_PythonHome = C.CString(home)
53+
C.Py_SetPythonHome(C.gopy_PythonHome)
54+
}
55+
56+
// Return the default “home”, that is, the value set by a previous call to Py_SetPythonHome(), or the value of the PYTHONHOME environment variable if it is set.
57+
func Py_GetPythonHome() string {
58+
home := C.Py_GetPythonHome()
59+
return C.GoString(home)
60+
}

init_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@ func TestProgramName(t *testing.T) {
1414
t.Fatalf("got=%q. want=%q", name, want)
1515
}
1616
}
17+
18+
func TestPythonHome(t *testing.T) {
19+
const want = "/usr/lib/go-python"
20+
python.Py_SetPythonHome(want)
21+
got := python.Py_GetPythonHome()
22+
if got != want {
23+
t.Fatalf("got=%q. want=%q", got, want)
24+
}
25+
}

0 commit comments

Comments
 (0)