RegisterFn anchors the function to this module. The callable Python object is built from the method definition that specifies the function name, the args expected by the function and the pointer to the Go callback.
(name string, fn interface{}, flags MethFlags)
| 78 | // RegisterFn anchors the function to this module. The callable Python object is built from the method definition |
| 79 | // that specifies the function name, the args expected by the function and the pointer to the Go callback. |
| 80 | func (m *Module) RegisterFn(name string, fn interface{}, flags MethFlags) error { |
| 81 | n := C.CString(name) |
| 82 | defer C.free(unsafe.Pointer(n)) |
| 83 | defs[name] = &C.PyMethodDef{ |
| 84 | ml_name: n, |
| 85 | ml_meth: (C.PyCFunction)(unsafe.Pointer(syscall.NewCallback(fn))), |
| 86 | ml_flags: C.int(flags), |
| 87 | } |
| 88 | mod := C.CString(m.name) |
| 89 | defer C.free(unsafe.Pointer(mod)) |
| 90 | f := C.PyCFunction_NewEx((*C.PyMethodDef)(unsafe.Pointer(defs[name])), m.rawptr, C.PyUnicode_FromString(mod)) |
| 91 | if f == nil { |
| 92 | return fmt.Errorf("unable to attach the %s function to the %s module", name, m.name) |
| 93 | } |
| 94 | return m.SetAttrString(name, f) |
| 95 | } |