buggy python interpretter or am I missing something here?

Dave Angel davea at davea.name
Mon Jan 27 00:36:20 EST 2014


 me <noone at all.net> Wrote in message:
> 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.
> 

sys.exit() raises an exception,  and you're deliberately eating
 that exception. 

> 
> ------------------------------------------------------------------------------
> 
> 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:

Bare except is almost never a good idea. It's going to intercept
 the exit exception, plus control C, syntax errors and others.
 Which you'd have known if you printed the exception code.
 

If you're going to catch an exception,  be specific. Otherwise
 expect the unexpected. 


There is a hierarchy of exception classes,  so you could catch a
 fairly generic class. But you do need to distinguish.
 

-- 
DaveA




More information about the Python-list mailing list