Python 2.5 fails where 2.4 works when running external program

Peter Otten __peter__ at web.de
Fri Mar 30 02:39:19 EDT 2007


Heikki Toivonen wrote:

> We have successfully used a script to run external programs for several
> years. Now we upgraded our Python to 2.5, and are hitting a mysterious
> error.
> 
> The expected output from the sample script (see below) with 2.4 looks
> like this:
> 
> ret ['5\n']
> else
> ********************
> ExternalCommandErrorWithOutputList 1 ['Traceback (most recent call
> last):\n', '  File "<string>", line 1, in ?\n', 'ZeroDivisionError:
> integer division or modulo by zero\n']
> ********************
> ret ['6\n', '7\n', '8\n']
> else
> ********************
> 
> With 2.5 we get:
> 
> ret ['5\n']
> else
> ********************
> Exception 'int' object is not iterable
> Traceback (most recent call last):
>   File "...test.py", line 43, in <module>
>     ret = executeCommandReturnOutput(cmd)
>   File "...test.py", line 6, in __init__
>     self.args = args[0]
> TypeError: 'int' object is not iterable
> 
> ********************
> ret ['6\n', '7\n', '8\n']
> else
> ********************
> 
> What is going on? How do we fix this? We'd like to be able to run with
> both python 2.4 and 2.5.

I think Exception.args always was supposed to be a tuple. Starting with 2.5
Python enforces that constraint:

[Python2.4]
>>> e = Exception()
>>> e.args
()
>>> e.args = "abc"
>>> e.args
'abc'
>>> e.args = 1
>>> e.args
1

[Python 2.5]
>>> e = Exception()
>>> e.args
()
>>> e.args = "abc"
>>> e.args
('a', 'b', 'c')
>>> e.args = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

The quick fix is of course

>>> e.args = [1]

but also note that according to the tutorial

http://docs.python.org/tut/node10.html#SECTION0010300000000000000000
"""
But use of .args is discouraged. Instead, the preferred use is to pass a
single argument to an exception (which can be a tuple if multiple arguments
are needed) and have it bound to the message attribute. 
"""

Peter




More information about the Python-list mailing list