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

Commit a69309a

Browse files
committed
python: expose Py_SetProgramName and Py_GetProgramName
Fixes #71.
1 parent f976f61 commit a69309a

File tree

3 files changed

+59
-13
lines changed

3 files changed

+59
-13
lines changed

init.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package python
2+
3+
// #include "go-python.h"
4+
// char *gopy_ProgName = NULL;
5+
import "C"
6+
7+
import (
8+
"unsafe"
9+
)
10+
11+
// Py_SetProgramName should be called before Py_Initialize() is called for
12+
// the first time, if it is called at all.
13+
// It tells the interpreter the value of the argv[0] argument to the main()
14+
// function of the program. This is used by Py_GetPath() and some other
15+
// functions below to find the Python run-time libraries relative to the
16+
// interpreter executable. The default value is 'python'. The argument should
17+
// point to a zero-terminated character string in static storage whose contents
18+
// will not change for the duration of the program’s execution.
19+
// No code in the Python interpreter will change the contents of this storage.
20+
func Py_SetProgramName(name string) {
21+
C.gopy_ProgName = C.CString(name)
22+
C.Py_SetProgramName(C.gopy_ProgName)
23+
}
24+
25+
// Py_GetProgramName returns the program name set with Py_SetProgramName(),
26+
// or the default.
27+
// The returned string points into static storage; the caller should not
28+
// modify its value.
29+
func Py_GetProgramName() string {
30+
cname := C.Py_GetProgramName()
31+
return C.GoString(cname)
32+
}
33+
34+
// PySys_SetArgv initializes the 'sys.argv' array in python.
35+
func PySys_SetArgv(argv []string) {
36+
argc := C.int(len(argv))
37+
cargs := make([]*C.char, len(argv))
38+
for idx, arg := range argv {
39+
cargs[idx] = C.CString(arg)
40+
defer C.free(unsafe.Pointer(cargs[idx]))
41+
}
42+
C.PySys_SetArgv(argc, &cargs[0])
43+
}

init_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package python_test
2+
3+
import (
4+
"testing"
5+
6+
python "github.com/sbinet/go-python"
7+
)
8+
9+
func TestProgramName(t *testing.T) {
10+
const want = "foo.exe"
11+
python.Py_SetProgramName(want)
12+
name := python.Py_GetProgramName()
13+
if name != want {
14+
t.Fatalf("got=%q. want=%q", name, want)
15+
}
16+
}

utilities.go

-13
Original file line numberDiff line numberDiff line change
@@ -468,16 +468,3 @@ func PyEval_GetFuncDesc(fct *PyObject) string {
468468
c_name := C.PyEval_GetFuncDesc(topy(fct))
469469
return C.GoString(c_name)
470470
}
471-
472-
// PySys_SetArgv initializes the 'sys.argv' array in python.
473-
func PySys_SetArgv(argv []string) {
474-
argc := C.int(len(argv))
475-
cargs := make([]*C.char, len(argv))
476-
for idx, arg := range argv {
477-
cargs[idx] = C.CString(arg)
478-
defer C.free(unsafe.Pointer(cargs[idx]))
479-
}
480-
C.PySys_SetArgv(argc, &cargs[0])
481-
}
482-
483-
// EOF

0 commit comments

Comments
 (0)