def callback(py_self, py_args, py_kwargs):
    args = () if py_args == ffi.NULL else pypy_convert(py_args)
    kwargs = {} if py_kwargs == ffi.NULL else pypy_convert(py_kwargs)
    fn = pypy_convert(py_self)
    try:
        result = fn(*args, **kwargs)
        return convert(result)
    except Exception as e:
        default_message = "Exception in pymetabiosis callback"
        for py_exc_type, exc_type in reversed(exceptions):
            if isinstance(e, exc_type):
                lib.PyErr_SetString(
                        py_exc_type, ffi.new("char[]", default_message))
                return ffi.NULL
        lib.PyErr_SetString(lib.PyExc_Exception,
                ffi.new("char[]", default_message))
        return ffi.NULL
def convert_unicode(u):
    return ffi.gc(
            lib.PyUnicode_FromString(ffi.new("char[]", u.encode('utf-8'))),
            lib.Py_DECREF)
 def _getattr(self, name):
     c_name = ffi.new("char[]", name)
     py_attr = ffi.gc(
             lib.PyObject_GetAttrString(self.obj, c_name),
             lib.Py_DECREF)
     return self._maybe_pypy_convert(py_attr)
def convert_string(s):
    return ffi.gc(lib.PyString_FromString(ffi.new("char[]", s)), lib.Py_DECREF)
        return convert(result)
    except Exception as e:
        default_message = "Exception in pymetabiosis callback"
        for py_exc_type, exc_type in reversed(exceptions):
            if isinstance(e, exc_type):
                lib.PyErr_SetString(
                        py_exc_type, ffi.new("char[]", default_message))
                return ffi.NULL
        lib.PyErr_SetString(lib.PyExc_Exception,
                ffi.new("char[]", default_message))
        return ffi.NULL


py_method = ffi.new("PyMethodDef*", dict(
    ml_name=ffi.new("char[]", "pypy_callback"),
    ml_meth=callback,
    ml_flags=lib.METH_VARARGS | lib.METH_KEYWORDS,
    ml_doc=ffi.new("char[]", "")
    ))

def convert_function(obj):
    return lib.PyCFunction_New(py_method, convert_unknown(obj))


class MetabiosisWrapper(object):
    def __init__(self, obj, noconvert=False):
        self.obj = obj
        self.noconvert = noconvert

    def __add__(self, b):
        op = pymetabiosis.module.import_module("operator")
        return op.add(self, b)