473,378 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Python 2.5 fails where 2.4 works when running external program

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.

And here is the script:

---CLIP---
import os, traceback, sys

class ExternalCommandErrorWithOutputList(Exception):
def __init__(self,args=None):
if args:
self.args = args[0]
self.outputList = args[1]
else:
self.args = args
self.outputList = []
def executeCommandReturnOutput(args):
args_str = ' '.join(args)

if os.name not in ['nt', 'os2']:
import popen2
p = popen2.Popen4(args_str)
p.tochild.close()
outputList = p.fromchild.readlines()
exitCode = p.wait()
if exitCode == 0:
exitCode = None
else:
exitCode >>= 8
else:
i,k = os.popen4(args_str)
i.close()
outputList = k.readlines()
exitCode = k.close()

if exitCode is not None:
raise ExternalCommandErrorWithOutputList, [exitCode, outputList]

return outputList
if __name__ == "__main__":
for cmd in [['python', '-c', '"print 5"'],
['python', '-c', '"1/0"'],
['python', '-c', '"print 6;import
sys;sys.stdout.flush();print >>sys.stderr, 7;print 8"'],
]:
try:
ret = executeCommandReturnOutput(cmd)
print 'ret', ret
except ExternalCommandErrorWithOutputList, e:
print 'ExternalCommandErrorWithOutputList', e, e.outputList
except Exception, e:
print 'Exception', e
type, value, stack = sys.exc_info()
print ''.join(traceback.format_exception(type, value, stack))
except:
print 'except'
type, value, stack = sys.exc_info()
print ''.join(traceback.format_exception(type, value, stack))
else:
print 'else'
print '*' * 20
---CLIP---

--
Heikki Toivonen
Mar 30 '07 #1
2 1871
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.ht...00000000000000
"""
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

Mar 30 '07 #2
Peter Otten wrote:
I think Exception.args always was supposed to be a tuple. Starting with 2.5
Python enforces that constraint:
[...]
http://docs.python.org/tut/node10.ht...00000000000000
"""
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.
"""
Excellent, thank you!

--
Heikki Toivonen
Mar 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: satish | last post by:
Hello all, I have a shared object executable viz. *cable* which I execute as follows : $ ansyscust71 -custom cable -p ANSYSRF **ansyscust71 is a shell script and is a part of a software...
2
by: Mike | last post by:
We have a large c++ program that runs under Windows NT Embedded to control an instrument. That program has been written to run external python scripts. The computer where this code runs does not...
14
by: Mark Dufour | last post by:
After nine months of hard work, I am proud to introduce my baby to the world: an experimental Python-to-C++ compiler. It can convert many Python programs into optimized C++ code, without any user...
4
by: vagrantbrad | last post by:
I'm using python 2.4 running on Fedora Core 4. I have written a python program called ipscan.py that checks the external ip address of my cable internet connection, and on change, will update the...
16
by: Claudio Grondi | last post by:
What started as a simple test if it is better to load uncompressed data directly from the harddisk or load compressed data and uncompress it (Windows XP SP 2, Pentium4 3.0 GHz system with 3 GByte...
53
by: Michael Tobis | last post by:
Someone asked me to write a brief essay regarding the value-add proposition for Python in the Fortran community. Slightly modified to remove a few climatology-related specifics, here it is. I...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 342 open (-38) / 3712 closed (+54) / 4054 total (+16) Bugs : 951 open (-14) / 6588 closed (+33) / 7539 total (+19) RFE : 257 open...
6
by: nsjmetzger | last post by:
I have a script that runs fine in Windows 2003 (32-bit). It basically calls the Windows defrag command. When I try the exact same code on Windows 2003 (64-bit) I get the following message: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.