diff --git a/init.go b/init.go new file mode 100644 index 0000000..be6e399 --- /dev/null +++ b/init.go @@ -0,0 +1,43 @@ +package python + +// #include "go-python.h" +// char *gopy_ProgName = NULL; +import "C" + +import ( + "unsafe" +) + +// Py_SetProgramName should be called before Py_Initialize() is called for +// the first time, if it is called at all. +// It tells the interpreter the value of the argv[0] argument to the main() +// function of the program. This is used by Py_GetPath() and some other +// functions below to find the Python run-time libraries relative to the +// interpreter executable. The default value is 'python'. 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_SetProgramName(name string) { + C.gopy_ProgName = C.CString(name) + C.Py_SetProgramName(C.gopy_ProgName) +} + +// Py_GetProgramName returns the program name set with Py_SetProgramName(), +// or the default. +// The returned string points into static storage; the caller should not +// modify its value. +func Py_GetProgramName() string { + cname := C.Py_GetProgramName() + return C.GoString(cname) +} + +// PySys_SetArgv initializes the 'sys.argv' array in python. +func PySys_SetArgv(argv []string) { + argc := C.int(len(argv)) + cargs := make([]*C.char, len(argv)) + for idx, arg := range argv { + cargs[idx] = C.CString(arg) + defer C.free(unsafe.Pointer(cargs[idx])) + } + C.PySys_SetArgv(argc, &cargs[0]) +} diff --git a/init_test.go b/init_test.go new file mode 100644 index 0000000..27d6ded --- /dev/null +++ b/init_test.go @@ -0,0 +1,20 @@ +package python_test + +import ( + "testing" + + python "github.com/sbinet/go-python" +) + +func TestProgramName(t *testing.T) { + const want = "foo.exe" + python.Py_SetProgramName(want) + err := python.Initialize() + if err != nil { + t.Fatal(err) + } + name := python.Py_GetProgramName() + if name != want { + t.Fatalf("got=%q. want=%q", name, want) + } +} diff --git a/utilities.go b/utilities.go index ec5d543..8bdaed2 100644 --- a/utilities.go +++ b/utilities.go @@ -468,16 +468,3 @@ func PyEval_GetFuncDesc(fct *PyObject) string { c_name := C.PyEval_GetFuncDesc(topy(fct)) return C.GoString(c_name) } - -// PySys_SetArgv initializes the 'sys.argv' array in python. -func PySys_SetArgv(argv []string) { - argc := C.int(len(argv)) - cargs := make([]*C.char, len(argv)) - for idx, arg := range argv { - cargs[idx] = C.CString(arg) - defer C.free(unsafe.Pointer(cargs[idx])) - } - C.PySys_SetArgv(argc, &cargs[0]) -} - -// EOF