func InitModule(name string, methods []Method) (*Module, error) { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) cm := C.PyImport_AddModule(cname) if cm == nil { return nil, exception() } m := newModule(cm) if len(methods) == 0 { return m, nil } defer m.Decref() n := C.PyUnicode_FromString(cname) if n == nil { return nil, exception() } for _, method := range methods { pyF, err := makeCFunction(method.Name, method.Func, method.Doc, n) if err != nil { return nil, err } if m.AddObject(method.Name, pyF); err != nil { return nil, err } } m.Incref() return m, nil }
// PyObject* PyImport_AddModule(const char *name) // Return value: Borrowed reference. // Return the module object corresponding to a module name. The name argument may be of the form package.module. First check the modules dictionary if there’s one there, and if not, create a new one and insert it in the modules dictionary. Return NULL with an exception set on failure. // // Note This function does not load or import the module; if the module wasn’t already loaded, you will get an empty module object. Use PyImport_ImportModule() or one of its variants to import a module. Package structures implied by a dotted name for name are not created if not already present. func PyImport_AddModule(name string) *PyObject { c_name := C.CString(name) defer C.free(unsafe.Pointer(c_name)) return togo(C.PyImport_AddModule(c_name)) }