|
| 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 | +} |
0 commit comments