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_SetProgramName and Py_GetProgramName
Browse files Browse the repository at this point in the history
Fixes #71.
  • Loading branch information
sbinet committed Aug 6, 2018
1 parent f976f61 commit a69309a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
43 changes: 43 additions & 0 deletions init.go
Original file line number Diff line number Diff line change
@@ -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])
}
16 changes: 16 additions & 0 deletions init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package python_test

import (
"testing"

python "github.com/sbinet/go-python"
)

func TestProgramName(t *testing.T) {
const want = "foo.exe"
python.Py_SetProgramName(want)
name := python.Py_GetProgramName()
if name != want {
t.Fatalf("got=%q. want=%q", name, want)
}
}
13 changes: 0 additions & 13 deletions utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a69309a

Please sign in to comment.