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

python: expose Py_SetPythonHome and Py_GetPythonHome #73

Merged
merged 1 commit into from
Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package python

// #include "go-python.h"
// char *gopy_ProgName = NULL;
// char *gopy_PythonHome = NULL;
import "C"

import (
Expand All @@ -18,6 +19,7 @@ import (
// will not change for the duration of the program’s execution.
// No code in the Python interpreter will change the contents of this storage.
func Py_SetProgramName(name string) {
C.free(unsafe.Pointer(C.gopy_ProgName))
C.gopy_ProgName = C.CString(name)
C.Py_SetProgramName(C.gopy_ProgName)
}
Expand All @@ -41,3 +43,18 @@ func PySys_SetArgv(argv []string) {
}
C.PySys_SetArgv(argc, &cargs[0])
}

// Set the default “home” directory, that is, the location of the standard Python libraries. See PYTHONHOME for the meaning of the argument string.
//
// 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.
func Py_SetPythonHome(home string) {
C.free(unsafe.Pointer(C.gopy_PythonHome))
C.gopy_PythonHome = C.CString(home)
C.Py_SetPythonHome(C.gopy_PythonHome)
}

// 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.
func Py_GetPythonHome() string {
home := C.Py_GetPythonHome()
return C.GoString(home)
}
9 changes: 9 additions & 0 deletions init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ func TestProgramName(t *testing.T) {
t.Fatalf("got=%q. want=%q", name, want)
}
}

func TestPythonHome(t *testing.T) {
const want = "/usr/lib/go-python"
python.Py_SetPythonHome(want)
got := python.Py_GetPythonHome()
if got != want {
t.Fatalf("got=%q. want=%q", got, want)
}
}