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.
This commit is contained in:
Sebastien Binet 2017-08-07 16:17:24 +02:00
parent 76264b4453
commit 23f5f65739
3 changed files with 14 additions and 13 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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() {