forked from sbinet/go-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython.go
40 lines (33 loc) · 814 Bytes
/
python.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// simplistic wrapper around the python C-API
package python
//#include "Python.h"
//#include <stdlib.h>
//#include <string.h>
import "C"
import (
"fmt"
)
// Initialize initializes the python interpreter and its GIL
func Initialize() error {
// make sure the python interpreter has been initialized
if C.Py_IsInitialized() == 0 {
C.Py_Initialize()
}
if C.Py_IsInitialized() == 0 {
return fmt.Errorf("python: could not initialize the python interpreter")
}
// make sure the GIL is correctly initialized
if C.PyEval_ThreadsInitialized() == 0 {
C.PyEval_InitThreads()
}
if C.PyEval_ThreadsInitialized() == 0 {
return fmt.Errorf("python: could not initialize the GIL")
}
return nil
}
// Finalize shutdowns the python interpreter
func Finalize() error {
C.Py_Finalize()
return nil
}
// EOF