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


Groups > comp.lang.python > #64836

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

References <pan$57cb8$37426877$4ff183f7$e46f1ba0$1@all.net> <mailman.6020.1390800839.18130.python-list@python.org> <52e5f658$0$29583$862e30e2@ngroups.net>
From Zachary Ware <zachary.ware+pylist@gmail.com>
Date 2014-01-27 00:47 -0600
Subject Re: buggy python interpretter or am I missing something here?
Newsgroups comp.lang.python
Message-ID <mailman.6025.1390805261.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Jan 27, 2014 at 12:02 AM, me <noone@all.net> wrote:
> On Mon, 27 Jan 2014 00:36:20 -0500, Dave Angel wrote:
>
>> sys.exit() raises an exception,  and you're deliberately eating
>>  that exception.
>>
>
> I can buy that sys.exit (may) be throwing an exception...My point of
> contention isn't that I may be throwing one, but why would a subsequent
> "raise" in the except: clause cause the point of program execution to
> jump back up to the sys.exit(0) and then function correctly.

You've missed the point here.  sys.exit() *does* raise an exception:
SystemExit, a subclass of BaseException.  In fact, you can implement
sys.exit in pure Python like so:

def exit(status):
    raise SystemExit(status)

Your bare 'except:' catches that SystemExit along with any other
possible exception; the bare 'raise' in the except clause just
re-raises the same SystemExit, which then does what you meant for it
to do in the first place.

Exception classes form a hierarchy, with BaseException being the base
(obviously :)), "except:" is really just shorthand for "except
BaseException:", and is very rarely a good idea.  Deriving from
BaseException are SystemExit, KeyboardInterrupt, and Exception.
SystemExit is the exception raised by sys.exit(), which if left
unhandled, the interpreter takes as a sign to shut down gracefully.
KeyboardInterrupt is raised by Ctrl+C, or (more accurately, if I'm not
mistaken) by sending SIGINT to the process.  Exception is the base
class for all other exceptions, such as TypeError, ValueError,
OSError, etc., and is what you actually want to catch if you really
don't care what exception is raised but want to handle it (though in
most cases, you either should care, or should at least report what the
exception was, preferably with a traceback...which is easiest to do by
not trying to catch the exception at all).

I would suggest skimming through the Python tutorial and/or language
reference if you haven't before, there's a lot of good information in
there that will make your life with Python much easier.

And, please take this positively, but from your posted code it's
fairly apparent that Python is not your native tongue :).  For
instance, you don't need the backslashes in your defaultparams
assignment; the parser knows it needs to keep looking on subsequent
lines when it hasn't found the closing '}' yet.  Also, you can iterate
over a (sys.argv) in a for loop, replacing 'l', 'idx', and the while
loop; something like

for arg in a:
    if arg == '-h':
        print help # help being defined elsewhere...
    elif arg == '-hh':
        # no really, print help
        print 'help'

And the big one, argument parsing is a solved problem in Python.
Check out the argparse module in the standard library (or if you're
using an older version of Python, try either optparse or getopt.  It's
really a thrice-solved problem ;)).

I hope I've been of some help,

--
Zach

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