Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64825

Re: buggy python interpretter or am I missing something here?

Date 2014-01-26 21:04 -0800
From Gary Herron <gary.herron@islandtraining.com>
Subject Re: buggy python interpretter or am I missing something here?
References <pan$57cb8$37426877$4ff183f7$e46f1ba0$1@all.net>
Newsgroups comp.lang.python
Message-ID <mailman.6018.1390799500.18130.python-list@python.org> (permalink)

Show all headers | View raw


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]$
>
>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 03:42 +0000
  buggy python interpretter or am I missing something here? - "TCdaemon.py"  yEnc me <noone@all.net> - 2014-01-27 03:42 +0000
  Re: buggy python interpretter or am I missing something here? Gary Herron <gary.herron@islandtraining.com> - 2014-01-26 21:04 -0800
    Re: buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 06:17 +0000
      Re: buggy python interpretter or am I missing something here? Gary Herron <gary.herron@islandtraining.com> - 2014-01-26 23:03 -0800
        Re: buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 07:20 +0000
  Re:buggy python interpretter or am I missing something here? Dave Angel <davea@davea.name> - 2014-01-27 00:36 -0500
    Re:buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 06:02 +0000
      Re: buggy python interpretter or am I missing something here? Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-01-27 00:47 -0600
        Re: buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 07:17 +0000
          Re: buggy python interpretter or am I missing something here? Alister <alister.ware@ntlworld.com> - 2014-01-27 12:19 +0000
          Re: buggy python interpretter or am I missing something here? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-27 13:48 +0000
          Re: buggy python interpretter or am I missing something here? Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-01-27 10:23 -0600
            Re: buggy python interpretter or am I missing something here? Dan Sommers <dan@tombstonezero.net> - 2014-01-27 16:38 +0000
          Re: buggy python interpretter or am I missing something here? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-27 16:45 +0000
  Re: buggy python interpretter or am I missing something here? Terry Reedy <tjreedy@udel.edu> - 2014-01-27 01:21 -0500
    Re: buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 06:42 +0000
      Re: buggy python interpretter or am I missing something here? Ethan Furman <ethan@stoneleaf.us> - 2014-01-26 23:08 -0800
  Re: buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 06:46 +0000
    Re: buggy python interpretter or am I missing something here? Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-01-27 00:55 -0600
    Re: buggy python interpretter or am I missing something here? Gary Herron <gary.herron@islandtraining.com> - 2014-01-26 23:12 -0800
      Re: buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 07:30 +0000
        Re: buggy python interpretter or am I missing something here? Peter Otten <__peter__@web.de> - 2014-01-27 09:45 +0100
    Re: buggy python interpretter or am I missing something here? Ethan Furman <ethan@stoneleaf.us> - 2014-01-26 23:17 -0800
      Re: buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 07:44 +0000
        Re: buggy python interpretter or am I missing something here? Chris Angelico <rosuav@gmail.com> - 2014-01-27 20:01 +1100
          Re: buggy python interpretter or am I missing something here? me <noone@all.net> - 2014-01-27 09:32 +0000
            Re: buggy python interpretter or am I missing something here? Neil Cerutti <neilc@norwich.edu> - 2014-01-27 12:56 +0000
            Re: buggy python interpretter or am I missing something here? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-27 13:56 +0000
            Re: buggy python interpretter or am I missing something here? Rick Johnson <rantingrickjohnson@gmail.com> - 2014-01-27 07:33 -0800
              Re: buggy python interpretter or am I missing something here? Chris Angelico <rosuav@gmail.com> - 2014-01-28 02:53 +1100
                Re: buggy python interpretter or am I missing something here? Rick Johnson <rantingrickjohnson@gmail.com> - 2014-01-27 12:22 -0800
                Re: buggy python interpretter or am I missing something here? Chris Angelico <rosuav@gmail.com> - 2014-01-28 07:29 +1100
                Re: buggy python interpretter or am I missing something here? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-27 22:25 +0000
                Re: buggy python interpretter or am I missing something here? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-01-30 18:13 +1300
                Re: buggy python interpretter or am I missing something here? Terry Reedy <tjreedy@udel.edu> - 2014-01-30 04:44 -0500
                Re: buggy python interpretter or am I missing something here? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-31 04:06 +0000
                Re: buggy python interpretter or am I missing something here? Kushal Kumaran <kushal.kumaran@gmail.com> - 2014-01-31 10:37 +0530
                Re: buggy python interpretter or am I missing something here? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-01-31 19:59 +1300
              Re: buggy python interpretter or am I missing something here? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-27 16:22 +0000
            Re: buggy python interpretter or am I missing something here? Michael Torrie <torriem@gmail.com> - 2014-01-27 10:52 -0700

csiph-web