This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
python: expose Py_SetProgramName and Py_GetProgramName
Fixes #71.
- Loading branch information
Showing
3 changed files
with
59 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters