forked from go-python/cpy3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifecycle.go
287 lines (237 loc) · 7.47 KB
/
lifecycle.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
Unless explicitly stated otherwise all files in this repository are licensed
under the MIT License.
This product includes software developed at Datadog (https://www.datadoghq.com/).
Copyright 2018 Datadog, Inc.
*/
package python3
/*
#include "Python.h"
*/
import "C"
import (
"fmt"
"unsafe"
)
// The argument for Py_SetProgramName, Py_SetPath and Py_SetPythonHome should point to a zero-terminated wide character string in static storage
// whose contents will not change for the duration of the program’s execution
var (
programName *C.wchar_t
pythonPath *C.wchar_t
pythonHome *C.wchar_t
)
//Py_Initialize : https://docs.python.org/3/c-api/init.html#c.Py_Initialize
func Py_Initialize() {
C.Py_Initialize()
}
//Py_InitializeEx : https://docs.python.org/3/c-api/init.html#c.Py_InitializeEx
func Py_InitializeEx(initsigs bool) {
if initsigs {
C.Py_InitializeEx(1)
} else {
C.Py_InitializeEx(0)
}
}
//Py_IsInitialized : https://docs.python.org/3/c-api/init.html#c.Py_IsInitialized
func Py_IsInitialized() bool {
return C.Py_IsInitialized() != 0
}
//Py_FinalizeEx : https://docs.python.org/3/c-api/init.html#c.Py_FinalizeEx
func Py_FinalizeEx() int {
return int(C.Py_FinalizeEx())
}
//Py_Finalize : https://docs.python.org/3/c-api/init.html#c.Py_Finalize
func Py_Finalize() {
C.Py_Finalize()
}
//Py_SetStandardStreamEncoding : https://docs.python.org/3/c-api/init.html#c.Py_SetStandardStreamEncoding
func Py_SetStandardStreamEncoding(encoding, errors string) int {
cencoding := C.CString(encoding)
defer C.free(unsafe.Pointer(cencoding))
cerrors := C.CString(errors)
defer C.free(unsafe.Pointer(cerrors))
return int(C.Py_SetStandardStreamEncoding(cencoding, cerrors))
}
//Py_SetProgramName : https://docs.python.org/3/c-api/init.html#c.Py_SetProgramName
func Py_SetProgramName(name string) error {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
newProgramName := C.Py_DecodeLocale(cname, nil)
if newProgramName == nil {
return fmt.Errorf("fail to call Py_DecodeLocale on '%s'", name)
}
C.Py_SetProgramName(newProgramName)
//no operation is performed if nil
C.PyMem_RawFree(unsafe.Pointer(programName))
programName = newProgramName
return nil
}
//Py_GetProgramName : https://docs.python.org/3/c-api/init.html#c.Py_GetProgramName
func Py_GetProgramName() (string, error) {
wcname := C.Py_GetProgramName()
if wcname == nil {
return "", nil
}
cname := C.Py_EncodeLocale(wcname, nil)
if cname == nil {
return "", fmt.Errorf("fail to call Py_EncodeLocale")
}
defer C.PyMem_Free(unsafe.Pointer(cname))
return C.GoString(cname), nil
}
//Py_GetPrefix : https://docs.python.org/3/c-api/init.html#c.Py_GetPrefix
func Py_GetPrefix() (string, error) {
wcname := C.Py_GetPrefix()
if wcname == nil {
return "", nil
}
cname := C.Py_EncodeLocale(wcname, nil)
if cname == nil {
return "", fmt.Errorf("fail to call Py_EncodeLocale")
}
defer C.PyMem_Free(unsafe.Pointer(cname))
return C.GoString(cname), nil
}
//Py_GetExecPrefix : https://docs.python.org/3/c-api/init.html#c.Py_GetExecPrefix
func Py_GetExecPrefix() (string, error) {
wcname := C.Py_GetExecPrefix()
if wcname == nil {
return "", nil
}
cname := C.Py_EncodeLocale(wcname, nil)
if cname == nil {
return "", fmt.Errorf("fail to call Py_EncodeLocale")
}
defer C.PyMem_Free(unsafe.Pointer(cname))
return C.GoString(cname), nil
}
//Py_GetProgramFullPath : https://docs.python.org/3/c-api/init.html#c.Py_GetProgramFullPath
func Py_GetProgramFullPath() (string, error) {
wcname := C.Py_GetProgramFullPath()
if wcname == nil {
return "", nil
}
cname := C.Py_EncodeLocale(wcname, nil)
if cname == nil {
return "", fmt.Errorf("fail to call Py_EncodeLocale")
}
defer C.PyMem_Free(unsafe.Pointer(cname))
return C.GoString(cname), nil
}
//Py_GetPath : https://docs.python.org/3/c-api/init.html#c.Py_GetPath
func Py_GetPath() (string, error) {
wcname := C.Py_GetPath()
if wcname == nil {
return "", nil
}
cname := C.Py_EncodeLocale(wcname, nil)
if cname == nil {
return "", fmt.Errorf("fail to call Py_EncodeLocale")
}
defer C.PyMem_Free(unsafe.Pointer(cname))
return C.GoString(cname), nil
}
//Py_SetPath : https://docs.python.org/3/c-api/init.html#c.Py_SetPath
func Py_SetPath(path string) error {
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
newPath := C.Py_DecodeLocale(cpath, nil)
if newPath == nil {
return fmt.Errorf("fail to call Py_DecodeLocale on '%s'", path)
}
C.Py_SetPath(newPath)
C.PyMem_RawFree(unsafe.Pointer(pythonPath))
pythonHome = newPath
return nil
}
//Py_GetVersion : https://docs.python.org/3/c-api/init.html#c.Py_GetVersion
func Py_GetVersion() string {
cversion := C.Py_GetVersion()
return C.GoString(cversion)
}
//Py_GetPlatform : https://docs.python.org/3/c-api/init.html#c.Py_GetPlatform
func Py_GetPlatform() string {
cplatform := C.Py_GetPlatform()
return C.GoString(cplatform)
}
//Py_GetCopyright : https://docs.python.org/3/c-api/init.html#c.Py_GetCopyright
func Py_GetCopyright() string {
ccopyright := C.Py_GetCopyright()
return C.GoString(ccopyright)
}
//Py_GetCompiler : https://docs.python.org/3/c-api/init.html#c.Py_GetCompiler
func Py_GetCompiler() string {
ccompiler := C.Py_GetCompiler()
return C.GoString(ccompiler)
}
//Py_GetBuildInfo : https://docs.python.org/3/c-api/init.html#c.Py_GetBuildInfo
func Py_GetBuildInfo() string {
cbuildInfo := C.Py_GetBuildInfo()
return C.GoString(cbuildInfo)
}
//PySys_SetArgvEx : https://docs.python.org/3/c-api/init.html#c.PySys_SetArgvEx
func PySys_SetArgvEx(args []string, updatepath bool) error {
argc := C.int(len(args))
argv := make([]*C.wchar_t, argc, argc)
for i, arg := range args {
carg := C.CString(arg)
defer C.free(unsafe.Pointer(carg))
warg := C.Py_DecodeLocale(carg, nil)
if warg == nil {
return fmt.Errorf("fail to call Py_DecodeLocale on '%s'", arg)
}
// Py_DecodeLocale requires a call to PyMem_RawFree to free the memory
defer C.PyMem_RawFree(unsafe.Pointer(warg))
argv[i] = warg
}
if updatepath {
C.PySys_SetArgvEx(argc, (**C.wchar_t)(unsafe.Pointer(&argv[0])), 1)
} else {
C.PySys_SetArgvEx(argc, (**C.wchar_t)(unsafe.Pointer(&argv[0])), 0)
}
return nil
}
//PySys_SetArgv : https://docs.python.org/3/c-api/init.html#c.PySys_SetArgv
func PySys_SetArgv(args []string) error {
argc := C.int(len(args))
argv := make([]*C.wchar_t, argc, argc)
for i, arg := range args {
carg := C.CString(arg)
defer C.free(unsafe.Pointer(carg))
warg := C.Py_DecodeLocale(carg, nil)
if warg == nil {
return fmt.Errorf("fail to call Py_DecodeLocale on '%s'", arg)
}
// Py_DecodeLocale requires a call to PyMem_RawFree to free the memory
defer C.PyMem_RawFree(unsafe.Pointer(warg))
argv[i] = warg
}
C.PySys_SetArgv(argc, (**C.wchar_t)(unsafe.Pointer(&argv[0])))
return nil
}
//Py_SetPythonHome : https://docs.python.org/3/c-api/init.html#c.Py_SetPythonHome
func Py_SetPythonHome(home string) error {
chome := C.CString(home)
defer C.free(unsafe.Pointer(chome))
newHome := C.Py_DecodeLocale(chome, nil)
if newHome == nil {
return fmt.Errorf("fail to call Py_DecodeLocale on '%s'", home)
}
C.Py_SetPythonHome(newHome)
C.PyMem_RawFree(unsafe.Pointer(pythonHome))
pythonHome = newHome
return nil
}
//Py_GetPythonHome : https://docs.python.org/3/c-api/init.html#c.Py_GetPythonHome
func Py_GetPythonHome() (string, error) {
wchome := C.Py_GetPythonHome()
if wchome == nil {
return "", nil
}
chome := C.Py_EncodeLocale(wchome, nil)
if chome == nil {
return "", fmt.Errorf("fail to call Py_EncodeLocale")
}
defer C.PyMem_Free(unsafe.Pointer(chome))
return C.GoString(chome), nil
}