exception - Handle c library error in python? -
if i'm using lib written in c in python, such zlib, , error like:
error: error -3 while decompressing: invalid distance far
how handle exception python? presumably exception defined in c module , there no exception class catch on except someexception
?
to call c function python, c function must wrapped python function. cpython, wrapper can written in c of package such swig, in cython, or in python of ctypes
module. wrapper must translate python inputs c values, define c output varibles, call c function, check return code, , either translate c output python , return or translate c code python exception , raise it.
it appears using python's zlib
, wraps c zlib
. defines python exception class error
.
>>> import zlib >>> zlib.error <class 'zlib.error'> >>> issubclass(zlib.error, exception) true
you catch other exception.
>>> try: raise zlib.error('error 99: unknown') except zlib.error e: print(e) error 99: unknown
to consistent exception naming convention, have named itzliberror
.
Comments
Post a Comment