-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport.go
More file actions
38 lines (29 loc) · 762 Bytes
/
import.go
File metadata and controls
38 lines (29 loc) · 762 Bytes
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
package python
import (
cpy3 "go.nhat.io/cpy/v3"
"go.nhat.io/once"
)
var modules once.ValuesMap[string, *Object, error]
// ImportModule is a wrapper around the C function PyImport_ImportModule.
func ImportModule(name string) (*Object, error) {
module, err := modules.Do(name, func() (*Object, error) {
module := cpy3.PyImport_ImportModule(name)
if err := LastError(); err != nil {
return nil, err
}
registerFinalizer(func() { module.DecRef() })
return NewObject(module), nil
})
if err != nil {
return nil, err
}
return module, nil
}
// MustImportModule imports a Python module and panics if it fails.
func MustImportModule(name string) *Object {
module, err := ImportModule(name)
if err != nil {
panic(err)
}
return module
}