On 01/26/2014 07:42 PM, me wrote:
I'm writing a linux daemon in python 2.x to process batches of GPS/GIS
data and I'm running into something that seems to break the expected
program flow in a REALLY BAD WAY.

Consider the attached template script and execute it with the -h option.
It is falling through to the except: clause even though try to manually
exit with sys.exit(0).  However, if I insert a "raise" into the except
clause then the sys.exit(0) executes properly.

See the attached code and output from when I run it.

Not interested in work-arounds.  I want to understand why it doesn't work
as expected.

Thanks!

TCdaemon.py ----

#! /usr/bin/python

import sys


#
------------------------------------------------------------------------------

defaultparams={ \
     "basedir": "/process", \
     "inpipe": "/tmp/TCdaemonIN", \
     "maxjobs":8, \
     "outpipe": "/tmp/TCdaemonOUT", \
     "ZZZ": 0
     }

#
------------------------------------------------------------------------------

def parse_args(a,d):
     l=len(a)
     idx=1
     try:
         while (idx<l):
             if (a[idx]=="-#"):
                 idx=idx+1
                 d["maxjobs"]=int(a[idx])
             elif (a[idx]=="-d"):
                 idx=idx+1
                 d["basedir"]=a[idx]
             elif (a[idx]=="-h"):
                 print "help goes here"
                 sys.exit(0)
             elif (a[idx]=="-i"):
                 idx=idx+1
                 d["inpipe"]=a[idx]
             elif (a[idx]=="-o"):
                 idx=idx+1
                 d["outpipe"]=a[idx]
             idx=idx+1
     except:
         # raise # -- WTF!!!??? required for -h to not fall through to
here?
         print "%s: error in command line - %s"%(a[0],a[idx:])
         sys.exit(1)

Never *ever* have a bare except like that. If it gets invoked, you have no idea why. A simple typo like ixd instead of idx or a(idx) instead of a[idx] would raise an exception but give you no idea why.

Do
  try:
      ...
  except Exception,e:
      print e
at the absolute minimum.
(Python 3 syntax would differ slightly, but the advice is the same.)

Perhaps printing a traceback along with the exception would help. Add
    traceback.print_exc()


Gary Herron



#
------------------------------------------------------------------------------
#
------------------------------------------------------------------------------
#
------------------------------------------------------------------------------

if (__name__=="__main__"):
     print defaultparams
     parse_args(sys.argv,defaultparams)
     print defaultparams



output.txt ---
[@blackbox new]$ ./TCdaemon.py -h
{'ZZZ': 0, 'basedir': '/process', 'outpipe': '/tmp/TCdaemonOUT',
'maxjobs': 8, 'inpipe': '/tmp/TCdaemonIN'}
help goes here
./TCdaemon.py: error in command line - ['-h']
[@blackbox new]$ echo $?
1
[@blackbox new]$
[@blackbox new]$
[@blackbox new]$ # editing to add "raise" at line 40
[@blackbox new]$
[@blackbox new]$
[@blackbox new]$
[@blackbox new]$ ./TCdaemon.py -h
{'ZZZ': 0, 'basedir': '/process', 'outpipe': '/tmp/TCdaemonOUT',
'maxjobs': 8, 'inpipe': '/tmp/TCdaemonIN'}
help goes here
[@blackbox new]$ echo $?
0
[@blackbox new]$



--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to