diff --git a/bind/gen_func.go b/bind/gen_func.go index adcffe1..8f2e606 100644 --- a/bind/gen_func.go +++ b/bind/gen_func.go @@ -260,6 +260,14 @@ func (g *pyGen) genFuncBody(sym *symbol, fsym *Func) { } } } + + // release GIL + g.gofile.Printf("_saved_thread := C.PyEval_SaveThread()\n") + if !rvIsErr && nres != 2 { + // reacquire GIL after return + g.gofile.Printf("defer C.PyEval_RestoreThread(_saved_thread)\n") + } + if isMethod { g.gofile.Printf( `vifc, __err := gopyh.VarFromHandleTry((gopyh.CGoHandle)(_handle), "%s") @@ -407,6 +415,9 @@ if __err != nil { if rvIsErr || nres == 2 { g.gofile.Printf("\n") + // reacquire GIL + g.gofile.Printf("C.PyEval_RestoreThread(_saved_thread)\n") + g.gofile.Printf("if __err != nil {\n") g.gofile.Indent() g.gofile.Printf("estr := C.CString(__err.Error())\n")