C API PyErr_Clear vs PyObject_HasAttrString

Robin Becker robin at SPAMREMOVEjessikat.fsnet.co.uk
Sun Nov 28 11:49:16 CET 2004


Andrew MacIntyre wrote:
.....
> 
> Rule:  if you call a Python API function, _always_ check its return
>        status (unless documented as void).  if it fails, check for an
>        exception.  if you want to ignore the exception, clear the
>        exception, otherwise cleanup and bailout.
> 
> This is a paraphrase (from memory) of a previous reply from Tim Peters.
> 
> Notwithstanding this, segfaulting in a Python API function is not welcome
> (especially one documented as always succeeding!).
> 
> I had a look at the source (from a CVS checkout this morning), and the
> only thing that looks like a source of such trouble would be an extension
> class with a tp_getattr implementation.

It is such a beast, but the exciting test case looks like

#####################
import _test
g=_test.gstate()

for a in ('strokeColor','strokeColor'):
	try:
		setattr(g,a,(1,2,3))
		print 'Wrong handling of bad '+a
	except ValueError:
		pass
#####################

so the tp_getattr slot isn't being used.
> 
> If you are using such a beast, you might want to look at the tp_getattr
> implementation first.  Otherwise I'd suggest filing a bug report on SF.
> BTW, have you tried this with 2.4c1?

Can't today, but I'll try and build the cut down extension with 2.4c1 
tomorrow.
-- 
Robin Becker



More information about the Python-list mailing list