From 17c9f530692bb11c325ea8f7d0917ad6cdac88b3 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Tue, 7 Aug 2018 14:21:46 +0200 Subject: [PATCH] python: expose Py_SetPythonHome and Py_GetPythonHome Fixes sbinet/go-python#71. --- init.go | 17 +++++++++++++++++ init_test.go | 9 +++++++++ 2 files changed, 26 insertions(+) diff --git a/init.go b/init.go index be6e399..7f1b485 100644 --- a/init.go +++ b/init.go @@ -2,6 +2,7 @@ package python // #include "go-python.h" // char *gopy_ProgName = NULL; +// char *gopy_PythonHome = NULL; import "C" import ( @@ -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) } @@ -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) +} diff --git a/init_test.go b/init_test.go index 9d4a3e4..f1e73ac 100644 --- a/init_test.go +++ b/init_test.go @@ -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) + } +}