MCPcopy Create free account
hub / github.com/rabbitstack/fibratus / RegisterFn

Method RegisterFn

pkg/filament/cpython/module.go:80–95  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
80func (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}

Callers 2

TestModuleRegisterFnFunction · 0.95
NewFunction · 0.80

Calls 1

SetAttrStringMethod · 0.80

Tested by 1

TestModuleRegisterFnFunction · 0.76