Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'elif': 0.05; 'insert': 0.05; 'output': 0.05; '(python': 0.07; 'subject:missing': 0.07; 'sys': 0.07; 'clause': 0.09; 'except:': 0.09; 'executes': 0.09; 'exit': 0.09; 'expected.': 0.09; 'here?': 0.09; 'properly.': 0.09; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'skip:/ 10': 0.09; 'try:': 0.09; 'python': 0.11; 'def': 0.12; 'template': 0.14; 'here"': 0.16; 'idx': 0.16; 'invoked,': 0.16; 'sys.exit(0)': 0.16; 'sys.exit(1)': 0.16; 'typo': 0.16; 'subject:python': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'differ': 0.19; 'help.': 0.21; 'seems': 0.21; 'command': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; '2.x': 0.24; 'received:emailsrvr.com': 0.24; 'skip:% 10': 0.24; 'why.': 0.24; '---': 0.24; 'script': 0.25; 'received:(smtp server)': 0.26; 'gets': 0.27; 'header:In-Reply- To:1': 0.27; 'idea': 0.28; 'skip:p 30': 0.29; '----': 0.29; 'raise': 0.29; "doesn't": 0.30; 'absolute': 0.30; 'skip:( 20': 0.30; 'along': 0.30; "i'm": 0.30; 'code': 0.31; "skip:' 10": 0.31; 'that.': 0.31; 'gary': 0.31; 'option.': 0.31; 'thanks!': 0.32; 'run': 0.32; 'linux': 0.33; 'running': 0.33; 'skip:d 20': 0.34; 'advice': 0.35; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'add': 0.35; 'really': 0.36; 'subject:?': 0.36; 'expected': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'bad': 0.39; 'flow': 0.39; 'skip:. 10': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'manually': 0.60; 'break': 0.61; 'simple': 0.61; 'here': 0.66; 'bare': 0.84; 'subject:here': 0.84 X-Virus-Scanned: OK Date: Sun, 26 Jan 2014 21:04:57 -0800 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: buggy python interpretter or am I missing something here? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 126 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390799500 news.xs4all.nl 2870 [2001:888:2000:d::a6]:42535 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64825 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 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]$ > >