Thursday, April 2, 2009

Python: Exception TypeError "argument of type 'instance' is not iterable" in 'garbage collection' ignored. Fatal Python error: unexpected exception

Problem: I got a nasty Fatal error message running my Python code: "Exception exceptions.TypeError: "argument of type 'instance' is not iterable" in 'garbage collection' ignored"

Solution: I forgot to use vars() function when checking the existance of a variable in an imported module

-----------------

I got this error in my Python code today, and it took me 20 minutes to understand where the bug was.

Actually the line number showed in the error message was false. The error itself started some lines above and it propagated until the incriminated line.

So this was the error:


Exception exceptions.TypeError: "argument of type 'instance' is not iterable" in 'garbage collection' ignored
Fatal Python error: unexpected exception during garbage collection
Aborted


And the problem was an "IF" statement where I checked for a variable name in an imported module:

My "IF" was this:


if 'fileName' in myModule:
fileName = 'OutputFile_%s.py' % myModule.fileName


To fix the problem I had to correctly add "vars()", of course ;-)


if 'fileName' in vars(myModule):
fileName = 'OutputFile_%s.py' % myModule.fileName


The bug was very stupid, just forgot the "vars()" function. But it was not so easy to find because the error message Python gave was not so helpful.

0 commenti: