From radha at leapship.com Thu Nov 3 01:10:34 2011 From: radha at leapship.com (Radhika Bauerle) Date: Wed, 2 Nov 2011 17:10:34 -0700 Subject: [capi-sig] Full time Job opening -Python lead in washington area Message-ID: Hello eveyone: we are looking for a Python lead in washington area. Strong on Python Strong on OOAD Worked on high performance web application Cheers, Radhika Bauerle. Leapship, Inc. radha at leapship.com 408.355.8462 From l_armeanu at yahoo.co.nz Thu Nov 17 11:07:51 2011 From: l_armeanu at yahoo.co.nz (Lee) Date: Thu, 17 Nov 2011 02:07:51 -0800 (PST) Subject: [capi-sig] exceptions, the right way Message-ID: <1321524471.35703.YahooMailNeo@web30701.mail.mud.yahoo.com> 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: try: ??? 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, Lee From mal at egenix.com Thu Nov 17 11:41:00 2011 From: mal at egenix.com (M.-A. Lemburg) Date: Thu, 17 Nov 2011 11:41:00 +0100 Subject: [capi-sig] exceptions, the right way In-Reply-To: <1321524471.35703.YahooMailNeo@web30701.mail.mud.yahoo.com> References: <1321524471.35703.YahooMailNeo@web30701.mail.mud.yahoo.com> Message-ID: <4EC4E4BC.2060606@egenix.com> Lee wrote: > 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: > > try: > 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 stack. Resources: http://docs.python.org/c-api/exceptions.html http://www.velocityreviews.com/forums/t647790-catching-python-exceptions-in-c.html -- Marc-Andre Lemburg eGenix.com 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 http://www.egenix.com/company/contact/ From l_armeanu at yahoo.co.nz Thu Nov 17 20:33:30 2011 From: l_armeanu at yahoo.co.nz (Lee) Date: Thu, 17 Nov 2011 11:33:30 -0800 (PST) Subject: [capi-sig] exceptions, the right way In-Reply-To: <4EC4E4BC.2060606@egenix.com> References: <1321524471.35703.YahooMailNeo@web30701.mail.mud.yahoo.com> <4EC4E4BC.2060606@egenix.com> Message-ID: <1321558410.60044.YahooMailNeo@web30706.mail.mud.yahoo.com> Thanks Marc-Andre, > If not, you simply pass on > the NULL/-1 return value to the higher level function in your call > stack. This is exactly my problem. What I am trying to do (and the example seems to show) is: - define an new type. - define getsetters functions. - say, if one want to delete an attribute, one writes in the setter function: if (value == NULL) { PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute"); return -1; } so, one expects that can do: import noddy2 mynoddy = noddy2.Noddy(number=15) mynoddy.first = "a" try: ??? del mynoddy.last except TypeError, e: ??? print "Error: ", e (passing the PyExc_TypeError to the interpreter). In my situation try/except is ignored and the interpreter crushes. Am I missing something? Lee From hniksic at xemacs.org Fri Nov 18 11:17:08 2011 From: hniksic at xemacs.org (Hrvoje Niksic) Date: Fri, 18 Nov 2011 11:17:08 +0100 Subject: [capi-sig] exceptions, the right way In-Reply-To: <1321558410.60044.YahooMailNeo@web30706.mail.mud.yahoo.com> (Lee's message of "Thu, 17 Nov 2011 11:33:30 -0800 (PST)") References: <1321524471.35703.YahooMailNeo@web30701.mail.mud.yahoo.com> <4EC4E4BC.2060606@egenix.com> <1321558410.60044.YahooMailNeo@web30706.mail.mud.yahoo.com> Message-ID: <87bos9hh0b.fsf@xemacs.org> Lee writes: > This is exactly my problem. What I am trying to do (and the example seems to show) is: > - define an new type. > - define getsetters functions. > - say, if one want to delete an attribute, one writes in the setter function: > > if (value == NULL) { PyErr_SetString(PyExc_TypeError, "Cannot delete the last attribute"); return -1; } Does unchanged noddy2.c work correctly for you? (It seems to contain the exact check you are quoting.) Maybe you are compiling noddy2 with a compiler incompatible to the one used for building Python? > so, one expects that can do: > > import noddy2 > mynoddy = noddy2.Noddy(number=15) > mynoddy.first = "a" > try: > ??? del mynoddy.last > except TypeError, e: > ??? print "Error: ", e This should be possible, yes. From python_capi at behnel.de Fri Nov 18 13:58:14 2011 From: python_capi at behnel.de (Stefan Behnel) Date: Fri, 18 Nov 2011 13:58:14 +0100 Subject: [capi-sig] exceptions, the right way In-Reply-To: <1321524471.35703.YahooMailNeo@web30701.mail.mud.yahoo.com> References: <1321524471.35703.YahooMailNeo@web30701.mail.mud.yahoo.com> Message-ID: <4EC65666.1090908@behnel.de> Lee, 17.11.2011 11:07: > 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. Just in case your actual goal is not to learn the C-API but to write code for it, you may want to take a look at Cython. It's a Python compiler that allows you to quickly write efficient C extensions for CPython without having to know the details of the underlying C-API. Stefan From l_armeanu at yahoo.co.nz Fri Nov 18 23:05:01 2011 From: l_armeanu at yahoo.co.nz (Lee) Date: Fri, 18 Nov 2011 14:05:01 -0800 (PST) Subject: [capi-sig] exceptions, the right way Message-ID: <1321653901.59577.YahooMailNeo@web30706.mail.mud.yahoo.com> Hi, > Maybe you are compiling noddy2 with a > compiler incompatible to the one used for building Python? Thanks Hrvoje for the hint! Actually I was linking against the python26.lib, changed to libpython26.a and it is working. For these tests I was using Dev-C++, the ultimate goal would be to use assembler. But for the moment, no joy with the assembler (even using libpython26.a), it seems that PyExc_TypeError and alike are the only culprits, the rest is OK. (Stefan: Cython looks impressive, I will look at it, if this path I took doesn't work, thanks for the suggestion). Lee From stefan_ml at behnel.de Fri Nov 18 11:37:27 2011 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 18 Nov 2011 11:37:27 +0100 Subject: [capi-sig] exceptions, the right way In-Reply-To: <1321524471.35703.YahooMailNeo@web30701.mail.mud.yahoo.com> References: <1321524471.35703.YahooMailNeo@web30701.mail.mud.yahoo.com> Message-ID: <4EC63567.9040301@behnel.de> Lee, 17.11.2011 11:07: > 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. Just in case your actual goal is not to learn the C-API but to write code for it, you may want to take a look at Cython. It's a Python compiler that allows you to quickly write efficient C extensions for CPython without having to know the details of the underlying C-API. Stefan