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

Commit

Permalink
python: expose Py_SetPythonHome and Py_GetPythonHome
Browse files Browse the repository at this point in the history
 Fixes #71.
  • Loading branch information
sbinet committed Aug 7, 2018
1 parent a69309a commit 17c9f53
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
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)
}
}

0 comments on commit 17c9f53

Please sign in to comment.