How to raise Python exception from a C extension?

PythonServer Side ProgrammingProgramming

For the above module, we need to prepare following script −

from distutils.core import setup, Extension
setup(name='helloworld', version='1.0', \
ext_modules=[Extension('helloworld', ['hello.c'])])

Now, we use the following command,

$ python install

Once we install the extension, we would be able to import and call that extension in our Python script and catch the exception in it as follows −
import helloworld
print helloworld.helloworld()
except Exception as e:
print str(e)

This would produce the following result −

bad format char passed to Py_BuildValue
Published on 07-Dec-2017 07:39:59