This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 140
/
import_test.go
263 lines (191 loc) · 5.7 KB
/
import_test.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
/*
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
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestImportModule(t *testing.T) {
Py_Initialize()
os := PyImport_ImportModule("os")
assert.NotNil(t, os)
os.DecRef()
}
func TestImportModuleEx(t *testing.T) {
Py_Initialize()
test := PyImport_ImportModuleEx("test", nil, nil, nil)
assert.NotNil(t, test)
test.DecRef()
}
func TestImportModuleLevelObject(t *testing.T) {
Py_Initialize()
mathName := PyUnicode_FromString("math")
defer mathName.DecRef()
math := PyImport_ImportModuleLevelObject(mathName, nil, nil, nil, 0)
assert.NotNil(t, math)
math.DecRef()
}
func TestImportModuleLevel(t *testing.T) {
Py_Initialize()
sys := PyImport_ImportModuleLevel("sys", nil, nil, nil, 0)
assert.NotNil(t, sys)
sys.DecRef()
}
func TestImportImport(t *testing.T) {
Py_Initialize()
platformName := PyUnicode_FromString("platform")
defer platformName.DecRef()
platform := PyImport_Import(platformName)
assert.NotNil(t, platform)
platform.DecRef()
}
func TestReloadModule(t *testing.T) {
Py_Initialize()
os := PyImport_ImportModule("os")
assert.NotNil(t, os)
defer os.DecRef()
newOs := PyImport_ReloadModule(os)
assert.NotNil(t, newOs)
defer newOs.DecRef()
// PyImport_ReloadModule return a new reference, pointer should be the same
assert.Equal(t, os, newOs)
}
func TestAddModuleObject(t *testing.T) {
Py_Initialize()
os := PyImport_ImportModule("os")
assert.NotNil(t, os)
defer os.DecRef()
pyName := PyUnicode_FromString("os.new")
defer pyName.DecRef()
new := PyImport_AddModuleObject(pyName)
assert.NotNil(t, new)
}
func TestAddModule(t *testing.T) {
Py_Initialize()
os := PyImport_ImportModule("os")
assert.NotNil(t, os)
defer os.DecRef()
new := PyImport_AddModule("os.new")
assert.NotNil(t, new)
}
func TestExecCodeModule(t *testing.T) {
Py_Initialize()
// fake module
source := PyUnicode_FromString("__version__ = '2.0'")
defer source.DecRef()
filename := PyUnicode_FromString("test_module.py")
defer filename.DecRef()
mode := PyUnicode_FromString("exec")
defer mode.DecRef()
// perform module load
builtins := PyEval_GetBuiltins()
assert.True(t, PyDict_Check(builtins))
compile := PyDict_GetItemString(builtins, "compile")
assert.True(t, PyCallable_Check(compile))
code := compile.CallFunctionObjArgs(source, filename, mode)
assert.NotNil(t, code)
defer code.DecRef()
module := PyImport_ExecCodeModule("test_module", code)
assert.NotNil(t, module)
}
func TestExecCodeModuleEx(t *testing.T) {
Py_Initialize()
// fake module
source := PyUnicode_FromString("__version__ = '2.0'")
defer source.DecRef()
filename := PyUnicode_FromString("test_module.py")
defer filename.DecRef()
mode := PyUnicode_FromString("exec")
defer mode.DecRef()
// perform module load
builtins := PyEval_GetBuiltins()
assert.True(t, PyDict_Check(builtins))
compile := PyDict_GetItemString(builtins, "compile")
assert.True(t, PyCallable_Check(compile))
code := compile.CallFunctionObjArgs(source, filename, mode)
assert.NotNil(t, code)
defer code.DecRef()
module := PyImport_ExecCodeModuleEx("test_module", code, "test_module.py")
assert.NotNil(t, module)
}
func TestExecCodeModuleWithPathnames(t *testing.T) {
Py_Initialize()
// fake module
source := PyUnicode_FromString("__version__ = '2.0'")
defer source.DecRef()
filename := PyUnicode_FromString("test_module.py")
defer filename.DecRef()
mode := PyUnicode_FromString("exec")
defer mode.DecRef()
// perform module load
builtins := PyEval_GetBuiltins()
assert.True(t, PyDict_Check(builtins))
compile := PyDict_GetItemString(builtins, "compile")
assert.True(t, PyCallable_Check(compile))
code := compile.CallFunctionObjArgs(source, filename, mode)
assert.NotNil(t, code)
defer code.DecRef()
module := PyImport_ExecCodeModuleWithPathnames("test_module", code, "test_module.py", "test_module.py")
assert.NotNil(t, module)
}
func TestExecCodeModuleObject(t *testing.T) {
Py_Initialize()
// fake module
source := PyUnicode_FromString("__version__ = '2.0'")
defer source.DecRef()
filename := PyUnicode_FromString("test_module.py")
defer filename.DecRef()
mode := PyUnicode_FromString("exec")
defer mode.DecRef()
// perform module load
builtins := PyEval_GetBuiltins()
assert.True(t, PyDict_Check(builtins))
compile := PyDict_GetItemString(builtins, "compile")
assert.True(t, PyCallable_Check(compile))
code := compile.CallFunctionObjArgs(source, filename, mode)
assert.NotNil(t, code)
defer code.DecRef()
moduleName := PyUnicode_FromString("test_module")
defer moduleName.DecRef()
module := PyImport_ExecCodeModuleObject(moduleName, code, filename, filename)
assert.NotNil(t, module)
}
func TestGetMagicNumber(t *testing.T) {
Py_Initialize()
magicNumber := PyImport_GetMagicNumber()
assert.NotNil(t, magicNumber)
}
func TestGetMagicTag(t *testing.T) {
Py_Initialize()
magicTag := PyImport_GetMagicTag()
assert.NotNil(t, magicTag)
}
func TestGetModuleDict(t *testing.T) {
Py_Initialize()
moduleDict := PyImport_GetModuleDict()
defer moduleDict.DecRef()
assert.True(t, PyDict_Check(moduleDict))
}
func TestGetModule(t *testing.T) {
Py_Initialize()
os := PyImport_ImportModule("os")
assert.NotNil(t, os)
defer os.DecRef()
name := PyUnicode_FromString("os")
defer name.DecRef()
new := PyImport_GetModule(name)
assert.Equal(t, new, os)
}
func TestGetImporter(t *testing.T) {
Py_Initialize()
paths := PySys_GetObject("path")
path := PyList_GetItem(paths, 0)
assert.NotNil(t, path)
importer := PyImport_GetImporter(path)
defer importer.DecRef()
assert.NotNil(t, importer)
}