Example #1
0
func InitModule(name string, methods []Method) (*Module, error) {
	cName := C.CString(name)
	defer C.free(unsafe.Pointer(cName))

	m := C.Py_InitModule4(cName, nil, nil, nil, C.PYTHON_API_VERSION)
	if m == nil {
		return nil, exception()
	}

	if len(methods) == 0 {
		return newModule(m), nil
	}

	n := C.PyString_FromString(cName)
	if n == nil {
		return nil, exception()
	}

	d := C.PyModule_GetDict(m)
	if d == 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 C.PyDict_SetItemString(d, C.CString(method.Name), c(pyF)) != 0 {
			return nil, exception()
		}
	}

	return newModule(m), nil
}
Example #2
0
// Return value: Borrowed reference.
func (mod *Module) Dict() *Dict {
	ret := C.PyModule_GetDict(mod.c())
	return newDict(ret)
}
Example #3
0
// PyObject* PyModule_GetDict(PyObject *module)
// Return value: Borrowed reference.
// Return the dictionary object that implements module‘s namespace; this object is the same as the __dict__ attribute of the module object. This function never fails. It is recommended extensions use other PyModule_*() and PyObject_*() functions rather than directly manipulate a module’s __dict__.
func PyModule_GetDict(self *PyObject) *PyObject {
	return togo(C.PyModule_GetDict(topy(self)))
}