🌐 AI搜索 & 代理 主页
Skip to content

Commit a84653a

Browse files
committed
Module imports
1 parent 0a31669 commit a84653a

File tree

3 files changed

+374
-0
lines changed

3 files changed

+374
-0
lines changed

import.go

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package python3
2+
3+
/*
4+
#include "Python.h"
5+
*/
6+
import "C"
7+
8+
import (
9+
"unsafe"
10+
)
11+
12+
//PyImport_ImportModule : https://docs.python.org/3/c-api/import.html#c.PyImport_ImportModule
13+
func PyImport_ImportModule(name string) *PyObject {
14+
cname := C.CString(name)
15+
defer C.free(unsafe.Pointer(cname))
16+
17+
return togo(C.PyImport_ImportModule(cname))
18+
}
19+
20+
//PyImport_ImportModuleEx : https://docs.python.org/3/c-api/import.html#c.PyImport_ImportModuleEx
21+
func PyImport_ImportModuleEx(name string, globals, locals, fromlist *PyObject) *PyObject {
22+
return PyImport_ImportModuleLevel(name, globals, locals, fromlist, 0)
23+
}
24+
25+
//PyImport_ImportModuleLevelObject : https://docs.python.org/3/c-api/import.html#c.PyImport_ImportModuleLevelObject
26+
func PyImport_ImportModuleLevelObject(name, globals, locals, fromlist *PyObject, level int) *PyObject {
27+
return togo(C.PyImport_ImportModuleLevelObject(toc(name), toc(globals), toc(locals), toc(fromlist), C.int(level)))
28+
}
29+
30+
//PyImport_ImportModuleLevel : https://docs.python.org/3/c-api/import.html#c.PyImport_ImportModuleLevel
31+
func PyImport_ImportModuleLevel(name string, globals, locals, fromlist *PyObject, level int) *PyObject {
32+
cname := C.CString(name)
33+
defer C.free(unsafe.Pointer(cname))
34+
35+
return togo(C.PyImport_ImportModuleLevel(cname, toc(globals), toc(locals), toc(fromlist), C.int(level)))
36+
}
37+
38+
//PyImport_Import : https://docs.python.org/3/c-api/import.html#c.PyImport_Import
39+
func PyImport_Import(name *PyObject) *PyObject {
40+
return togo(C.PyImport_Import(toc(name)))
41+
}
42+
43+
//PyImport_ReloadModule : https://docs.python.org/3/c-api/import.html#c.PyImport_ReloadModule
44+
func PyImport_ReloadModule(name *PyObject) *PyObject {
45+
return togo(C.PyImport_ReloadModule(toc(name)))
46+
}
47+
48+
//PyImport_AddModuleObject : https://docs.python.org/3/c-api/import.html#c.PyImport_AddModuleObject
49+
func PyImport_AddModuleObject(name *PyObject) *PyObject {
50+
return togo(C.PyImport_AddModuleObject(toc(name)))
51+
}
52+
53+
//PyImport_AddModule : https://docs.python.org/3/c-api/import.html#c.PyImport_AddModule
54+
func PyImport_AddModule(name string) *PyObject {
55+
cname := C.CString(name)
56+
defer C.free(unsafe.Pointer(cname))
57+
58+
return togo(C.PyImport_AddModule(cname))
59+
}
60+
61+
//PyImport_ExecCodeModule : https://docs.python.org/3/c-api/import.html#c.PyImport_ExecCodeModule
62+
func PyImport_ExecCodeModule(name string, co *PyObject) *PyObject {
63+
cname := C.CString(name)
64+
defer C.free(unsafe.Pointer(cname))
65+
66+
return togo(C.PyImport_ExecCodeModule(cname, toc(co)))
67+
}
68+
69+
//PyImport_ExecCodeModuleEx : https://docs.python.org/3/c-api/import.html#c.PyImport_ExecCodeModuleEx
70+
func PyImport_ExecCodeModuleEx(name string, co *PyObject, pathname string) *PyObject {
71+
cname := C.CString(name)
72+
defer C.free(unsafe.Pointer(cname))
73+
74+
cpathname := C.CString(pathname)
75+
defer C.free(unsafe.Pointer(cpathname))
76+
77+
return togo(C.PyImport_ExecCodeModuleEx(cname, toc(co), cpathname))
78+
}
79+
80+
//PyImport_ExecCodeModuleObject : https://docs.python.org/3/c-api/import.html#c.PyImport_ExecCodeModuleObject
81+
func PyImport_ExecCodeModuleObject(name, co, pathname, cpathname *PyObject) *PyObject {
82+
return togo(C.PyImport_ExecCodeModuleObject(toc(name), toc(co), toc(pathname), toc(cpathname)))
83+
}
84+
85+
//PyImport_ExecCodeModuleWithPathnames : https://docs.python.org/3/c-api/import.html#c.PyImport_ExecCodeModuleWithPathnames
86+
func PyImport_ExecCodeModuleWithPathnames(name string, co *PyObject, pathname string, cpathname string) *PyObject {
87+
cname := C.CString(name)
88+
defer C.free(unsafe.Pointer(cname))
89+
90+
cspathname := C.CString(pathname)
91+
defer C.free(unsafe.Pointer(cspathname))
92+
93+
ccpathname := C.CString(cpathname)
94+
defer C.free(unsafe.Pointer(ccpathname))
95+
96+
return togo(C.PyImport_ExecCodeModuleWithPathnames(cname, toc(co), cspathname, ccpathname))
97+
}
98+
99+
//PyImport_GetMagicNumber : https://docs.python.org/3/c-api/import.html#c.PyImport_GetMagicNumber
100+
func PyImport_GetMagicNumber() int {
101+
return int(C.PyImport_GetMagicNumber())
102+
}
103+
104+
//PyImport_GetMagicTag : https://docs.python.org/3/c-api/import.html#c.PyImport_GetMagicTag
105+
func PyImport_GetMagicTag() string {
106+
cmagicTag := C.PyImport_GetMagicTag()
107+
108+
return C.GoString(cmagicTag)
109+
}
110+
111+
//PyImport_GetModuleDict : https://docs.python.org/3/c-api/import.html#c.PyImport_GetModuleDict
112+
func PyImport_GetModuleDict() *PyObject {
113+
return togo(C.PyImport_GetModuleDict())
114+
}
115+
116+
//PyImport_GetModule : https://docs.python.org/3/c-api/import.html#c.PyImport_GetModule
117+
func PyImport_GetModule(name *PyObject) *PyObject {
118+
return togo(C.PyImport_GetModule(toc(name)))
119+
120+
}
121+
122+
//PyImport_GetImporter : https://docs.python.org/3/c-api/import.html#c.PyImport_GetImporter
123+
func PyImport_GetImporter(path *PyObject) *PyObject {
124+
return togo(C.PyImport_GetImporter(toc(path)))
125+
126+
}
127+
128+
//PyImport_ImportFrozenModuleObject : https://docs.python.org/3/c-api/import.html#c.PyImport_ImportFrozenModuleObject
129+
func PyImport_ImportFrozenModuleObject(name *PyObject) int {
130+
return int(C.PyImport_ImportFrozenModuleObject(toc(name)))
131+
132+
}
133+
134+
//PyImport_ImportFrozenModule : https://docs.python.org/3/c-api/import.html#c.PyImport_ImportFrozenModule
135+
func PyImport_ImportFrozenModule(name string) int {
136+
cname := C.CString(name)
137+
defer C.free(unsafe.Pointer(cname))
138+
139+
return int(C.PyImport_ImportFrozenModule(cname))
140+
141+
}

import_test.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package python3
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestImportModule(t *testing.T) {
10+
Py_Initialize()
11+
12+
os := PyImport_ImportModule("os")
13+
assert.NotNil(t, os)
14+
os.DecRef()
15+
}
16+
17+
func TestImportModuleEx(t *testing.T) {
18+
Py_Initialize()
19+
20+
test := PyImport_ImportModuleEx("test", nil, nil, nil)
21+
assert.NotNil(t, test)
22+
test.DecRef()
23+
}
24+
25+
func TestImportModuleLevelObject(t *testing.T) {
26+
Py_Initialize()
27+
28+
mathName := PyUnicode_FromString("math")
29+
defer mathName.DecRef()
30+
31+
math := PyImport_ImportModuleLevelObject(mathName, nil, nil, nil, 0)
32+
assert.NotNil(t, math)
33+
math.DecRef()
34+
}
35+
36+
func TestImportModuleLevel(t *testing.T) {
37+
Py_Initialize()
38+
39+
sys := PyImport_ImportModuleLevel("sys", nil, nil, nil, 0)
40+
assert.NotNil(t, sys)
41+
sys.DecRef()
42+
}
43+
44+
func TestImportImport(t *testing.T) {
45+
Py_Initialize()
46+
47+
platformName := PyUnicode_FromString("platform")
48+
defer platformName.DecRef()
49+
50+
platform := PyImport_Import(platformName)
51+
assert.NotNil(t, platform)
52+
platform.DecRef()
53+
}
54+
55+
func TestReloadModule(t *testing.T) {
56+
Py_Initialize()
57+
58+
os := PyImport_ImportModule("os")
59+
assert.NotNil(t, os)
60+
defer os.DecRef()
61+
62+
newOs := PyImport_ReloadModule(os)
63+
assert.NotNil(t, newOs)
64+
defer newOs.DecRef()
65+
66+
// PyImport_ReloadModule return a new reference, pointer should be the same
67+
assert.Equal(t, os, newOs)
68+
}
69+
70+
func TestAddModuleObject(t *testing.T) {
71+
Py_Initialize()
72+
73+
os := PyImport_ImportModule("os")
74+
assert.NotNil(t, os)
75+
defer os.DecRef()
76+
77+
pyName := PyUnicode_FromString("os.new")
78+
defer pyName.DecRef()
79+
80+
new := PyImport_AddModuleObject(pyName)
81+
assert.NotNil(t, new)
82+
}
83+
84+
func TestAddModule(t *testing.T) {
85+
Py_Initialize()
86+
87+
os := PyImport_ImportModule("os")
88+
assert.NotNil(t, os)
89+
defer os.DecRef()
90+
91+
new := PyImport_AddModule("os.new")
92+
assert.NotNil(t, new)
93+
}
94+
95+
func TestExecCodeModule(t *testing.T) {
96+
Py_Initialize()
97+
98+
// fake module
99+
source := PyUnicode_FromString("__version__ = '2.0'")
100+
defer source.DecRef()
101+
filename := PyUnicode_FromString("test_module.py")
102+
defer filename.DecRef()
103+
mode := PyUnicode_FromString("exec")
104+
defer mode.DecRef()
105+
106+
// perform module load
107+
builtins := PyEval_GetBuiltins()
108+
assert.True(t, PyDict_Check(builtins))
109+
110+
compile := PyDict_GetItemString(builtins, "compile")
111+
assert.True(t, PyCallable_Check(compile))
112+
113+
code := compile.CallFunctionObjArgs(source, filename, mode)
114+
assert.NotNil(t, code)
115+
defer code.DecRef()
116+
117+
module := PyImport_ExecCodeModule("test_module", code)
118+
assert.NotNil(t, module)
119+
120+
}
121+
122+
func TestGetMagicNumber(t *testing.T) {
123+
Py_Initialize()
124+
125+
magicNumber := PyImport_GetMagicNumber()
126+
assert.NotNil(t, magicNumber)
127+
}
128+
129+
func TestGetMagicTag(t *testing.T) {
130+
Py_Initialize()
131+
132+
magicTag := PyImport_GetMagicTag()
133+
assert.NotNil(t, magicTag)
134+
}
135+
136+
func TestGetModuleDict(t *testing.T) {
137+
Py_Initialize()
138+
139+
moduleDict := PyImport_GetModuleDict()
140+
defer moduleDict.DecRef()
141+
142+
assert.True(t, PyDict_Check(moduleDict))
143+
144+
}
145+
146+
func TestGetModule(t *testing.T) {
147+
Py_Initialize()
148+
149+
os := PyImport_ImportModule("os")
150+
assert.NotNil(t, os)
151+
defer os.DecRef()
152+
153+
name := PyUnicode_FromString("os")
154+
defer name.DecRef()
155+
156+
new := PyImport_GetModule(name)
157+
assert.Equal(t, new, os)
158+
}
159+
160+
func TestGetImporter(t *testing.T) {
161+
Py_Initialize()
162+
163+
paths := PySys_GetObject("path")
164+
path := PyList_GetItem(paths, 0)
165+
166+
assert.NotNil(t, path)
167+
importer := PyImport_GetImporter(path)
168+
defer importer.DecRef()
169+
170+
assert.NotNil(t, importer)
171+
172+
}

module.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package python3
2+
3+
/*
4+
#include "Python.h"
5+
#include "macro.h"
6+
#include "type.h"
7+
*/
8+
import "C"
9+
import "unsafe"
10+
11+
//Module : https://docs.python.org/3/c-api/module.html#c.PyModule_Type
12+
var Module = togo(C._go_PyModule_Type)
13+
14+
//PyModule_Check : https://docs.python.org/3/c-api/module.html#c.PyModule_Check
15+
func PyModule_Check(p *PyObject) bool {
16+
return C._go_PyModule_Check(toc(p)) != 0
17+
}
18+
19+
//PyModule_CheckExact : https://docs.python.org/3/c-api/module.html#c.PyModule_CheckExact
20+
func PyModule_CheckExact(p *PyObject) bool {
21+
return C._go_PyModule_CheckExact(toc(p)) != 0
22+
}
23+
24+
//PyModule_NewObject : https://docs.python.org/3/c-api/module.html#c.PyModule_NewObject
25+
func PyModule_NewObject(name *PyObject) *PyObject {
26+
return togo(C.PyModule_NewObject(toc(name)))
27+
}
28+
29+
//PyModule_New : https://docs.python.org/3/c-api/module.html#c.PyModule_New
30+
func PyModule_New(name string) *PyObject {
31+
cname := C.CString(name)
32+
defer C.free(unsafe.Pointer(cname))
33+
34+
return togo(C.PyModule_New(cname))
35+
}
36+
37+
//PyModule_GetDict : https://docs.python.org/3/c-api/module.html#c.PyModule_GetDict
38+
func PyModule_GetDict(module *PyObject) *PyObject {
39+
return togo(C.PyModule_GetDict(toc(module)))
40+
}
41+
42+
//PyModule_GetNameObject : https://docs.python.org/3/c-api/module.html#c.PyModule_GetNameObject
43+
func PyModule_GetNameObject(module *PyObject) *PyObject {
44+
return togo(C.PyModule_GetNameObject(toc(module)))
45+
}
46+
47+
//PyModule_GetName : https://docs.python.org/3/c-api/module.html#c.PyModule_GetName
48+
func PyModule_GetName(module *PyObject) string {
49+
cname := C.PyModule_GetName(toc(module))
50+
return C.GoString(cname)
51+
}
52+
53+
//PyModule_GetState : https://docs.python.org/3/c-api/module.html#c.PyModule_GetState
54+
func PyModule_GetState(module *PyObject) unsafe.Pointer {
55+
return unsafe.Pointer(C.PyModule_GetNameObject(toc(module)))
56+
}
57+
58+
//PyModule_GetFilenameObject : https://docs.python.org/3/c-api/module.html#c.PyModule_GetFilenameObject
59+
func PyModule_GetFilenameObject(module *PyObject) *PyObject {
60+
return togo(C.PyModule_GetFilenameObject(toc(module)))
61+
}

0 commit comments

Comments
 (0)