Strange problem with C API

Michael Hudson mwh at python.net
Wed Feb 27 12:18:29 CET 2002


amrit040 at yahoo.com (Amrit) writes:

> Thanx that fixes the problem. So basically i was trying to read one
> more element than what the input had. This explains the bug but what
> i don't understand is why does this thing alternate ! Shouldn't it
> be consistent.

Guessing, but here's a go:

1) when you first call the function, it get's almost all the way
   through before you try to access the extra element.  This sets an
   exception, but as you don't check for error return, the computation
   completes and returns its result (in particular, not NULL).

2) when you call again you call PyArg_ParseTuple with, amongst other
   things, an "f" parameter.  This winds up calling PyFloat_ToDouble
   (or something like that).  There is no way to tell from the return
   value of this function that an error occured, so PyErr_Occurred
   gets called, and finally notices the error set in 1) above.  So
   PyArg_ParseTuple returns NULL, so your function returns NULL, so a
   traceback gets printed and the threadstate's exception gets
   cleared.  

3) you call the function again, and we're back in the situation of 1)
   again.

I suspect that if you call your function once, and then execute
something like:

>>> float("0.1")

you'll get an exception.

In summary, just try to get this right -- the consequences are
confusing.

Cheers,
M.

-- 
  I sense much distrust in you.  Distrust leads to cynicism, cynicism
  leads to bitterness, bitterness leads to the Awareness Of True
  Reality which is referred to by those-who-lack-enlightenment as
  "paranoia".  I approve.    -- David P. Murphy, alt.sysadmin.recovery



More information about the Python-list mailing list