From 23f5f657392e50036f6de4129f58061f682bd968 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Mon, 7 Aug 2017 16:17:24 +0200 Subject: [PATCH] bind/cffi: handle strings from error.Error This CL introduces a convenience function cffi_cgopy_cnv_c2py_errstring to properly handle (and decode) strings coming from error.Error calls. --- _examples/pyerrors/test.py | 2 +- bind/gencffi.go | 9 +++++++++ bind/gencffi_func.go | 16 ++++------------ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/_examples/pyerrors/test.py b/_examples/pyerrors/test.py index 5211b01..587145b 100644 --- a/_examples/pyerrors/test.py +++ b/_examples/pyerrors/test.py @@ -12,7 +12,7 @@ def div(a, b): r = pyerrors.Div(a, b) print("pyerrors.Div(%d, %d) = %d"% (a, b, r)) except Exception as e: - print(str(e)) + print(e) div(5,0) div(5,2) diff --git a/bind/gencffi.go b/bind/gencffi.go index d280f4b..c0a1e84 100644 --- a/bind/gencffi.go +++ b/bind/gencffi.go @@ -129,6 +129,15 @@ class _cffi_helper(object): pystr = pystr.decode('ascii') return pystr + @staticmethod + def cffi_cgopy_cnv_c2py_errstring(c): + s = _cffi_helper.lib._cgopy_ErrorString(c) + pystr = ffi.string(s) + _cffi_helper.lib._cgopy_FreeCString(s) + if _PY3: + pystr = pystr.decode('ascii') + return pystr + @staticmethod def cffi_cgopy_cnv_c2py_int(c): return int(c) diff --git a/bind/gencffi_func.go b/bind/gencffi_func.go index 080b56e..c5eae16 100644 --- a/bind/gencffi_func.go +++ b/bind/gencffi_func.go @@ -64,9 +64,7 @@ func (g *cffiGen) genMethodBody(s Struct, m Func) { case 1: g.wrapper.Printf("if not _cffi_helper.lib._cgopy_ErrorIsNil(cret):\n") g.wrapper.Indent() - g.wrapper.Printf("c_err_str = _cffi_helper.lib._cgopy_ErrorString(cret)\n") - g.wrapper.Printf("py_err_str = ffi.string(c_err_str)\n") - g.wrapper.Printf("_cffi_helper.lib._cgopy_FreeCString(c_err_str)\n") + g.wrapper.Printf("py_err_str = _cffi_helper.cffi_cgopy_cnv_c2py_errstring(cret)\n") g.wrapper.Printf("raise RuntimeError(py_err_str)\n") g.wrapper.Outdent() g.wrapper.Printf("return\n") @@ -74,9 +72,7 @@ func (g *cffiGen) genMethodBody(s Struct, m Func) { case 2: g.wrapper.Printf("if not _cffi_helper.lib._cgopy_ErrorIsNil(cret.r1):\n") g.wrapper.Indent() - g.wrapper.Printf("c_err_str = _cffi_helper.lib._cgopy_ErrorString(cret.r1)\n") - g.wrapper.Printf("py_err_str = ffi.string(c_err_str)\n") - g.wrapper.Printf("_cffi_helper.lib._cgopy_FreeCString(c_err_str)\n") + g.wrapper.Printf("py_err_str = _cffi_helper.cffi_cgopy_cnv_c2py_errstring(cret.r1)\n") g.wrapper.Printf("raise RuntimeError(py_err_str)\n") g.wrapper.Outdent() if res[0].sym.hasConverter() { @@ -212,9 +208,7 @@ func (g *cffiGen) genFuncBody(f Func) { case 1: g.wrapper.Printf("if not _cffi_helper.lib._cgopy_ErrorIsNil(cret):\n") g.wrapper.Indent() - g.wrapper.Printf("c_err_str = _cffi_helper.lib._cgopy_ErrorString(cret)\n") - g.wrapper.Printf("py_err_str = ffi.string(c_err_str)\n") - g.wrapper.Printf("_cffi_helper.lib._cgopy_FreeCString(c_err_str)\n") + g.wrapper.Printf("py_err_str = _cffi_helper.cffi_cgopy_cnv_c2py_errstring(cret)\n") g.wrapper.Printf("raise RuntimeError(py_err_str)\n") g.wrapper.Outdent() g.wrapper.Printf("return\n") @@ -222,9 +216,7 @@ func (g *cffiGen) genFuncBody(f Func) { case 2: g.wrapper.Printf("if not _cffi_helper.lib._cgopy_ErrorIsNil(cret.r1):\n") g.wrapper.Indent() - g.wrapper.Printf("c_err_str = _cffi_helper.lib._cgopy_ErrorString(cret.r1)\n") - g.wrapper.Printf("py_err_str = ffi.string(c_err_str)\n") - g.wrapper.Printf("_cffi_helper.lib._cgopy_FreeCString(c_err_str)\n") + g.wrapper.Printf("py_err_str = _cffi_helper.cffi_cgopy_cnv_c2py_errstring(cret.r1)\n") g.wrapper.Printf("raise RuntimeError(py_err_str)\n") g.wrapper.Outdent() if res[0].sym.hasConverter() {