[capi-sig] exceptions, the right way
mal at egenix.com
Thu Nov 17 11:41:00 CET 2011
> Hi All,
> I am new to Python C/API, I am trying to follow the official docs, but
> I am confused about raising and catching exceptions from an extension.
> For the example provided at
> Extending and Embedding the Python Interpreter
> 2. Defining New Types
> 2.1.2. Providing finer control over data attributes
> eg I don't seem to be able to catch the exceptions raised by the getsetters functions,
> the interpreter will just end up in an ugly crash. Something like:
> del mynoddy.last
> except TypeError, e:
> print "Error: ", e
> doesn't work. I understand that PyErr_SetString just sets the error and does not raises the exception.
> Am I missing something?
> I am using Python ver 2.6.6 on Windows.
> Any advice is greatly appreciated,
I'm not sure I understand what you want to do, but raising
and catching exceptions in C is not hard:
* You raise them by using one of the PyErr_Set*() APIs and return NULL/-1
from your function.
* You catch them by checking for NULL/-1 returns and then asking the
PyErr_Occurred() function to see whether an exception was set. If so,
you can use one of the PyErr_*Matches() functions to determine whether
you want to catch the exception or not. If not, you simply pass on
the NULL/-1 return value to the higher level function in your call
Professional Python Services directly from the Source (#1, Nov 17 2011)
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
More information about the capi-sig