Python brokeness???

Gordon McMillan gmcm at hypernet.com
Thu Jul 6 18:42:03 EDT 2000


Arinté wrote: 

>I have an embedded app with a call like this:
>PyObject *open(PyObject* self,PyObject* args){
> long ret(1);
>
> g_lastDevice = pyposs.getDeviceFromPy(args);
> if(!g_lastDevice){
>  PyErr_Clear();
>  PyErr_SetString(PyExc_TypeError,"No Device");
>  return NULL;
> }
> else
> {
>  PyObject* deqme =Py_BuildValue("i",0);
>  PyObject* comm =PyObject_GetItem(args, deqme);
>  PosArgEx* p=createArgs(comm);
>  ret = g_lastDevice->open(p);
>  if(p){
>   createPyArgs(comm,p);
>   delete p;
>  }
>   ioctls.getIOCtls(g_lastDevice->getName());
> }
> PyErr_Clear();
> return Py_BuildValue("i",ret);
>}
>
>Now, before I put that PyErr_Clear in the script that I ran with a
>range(-1,10) used to give error len() of unsized object or give me
>IndexError: string index out of range.  

Actually, there are 2 PyErr_Clears. I think you mean the 2nd one. 

There are quite a few places above that Python could be setting an error. 
Note that reaching the end of a list means an IndexError gets set. And you 
have a PyObject_GetItem(args, ...) without ever checking that args is a 
sequence. 

Leaving uncleared errors causes strange things to happen later. Which 
matches the symptoms.

You should be checking for NULL returns from those calls that return 
PyObject *s. If you get NULL, there's almost undoubtedly an error set.





More information about the Python-list mailing list