[issue3079] sys.exit() called from optparse - bad, bad, bad

Skip Montanaro report at bugs.python.org
Wed Jun 11 19:23:27 CEST 2008


New submission from Skip Montanaro <skip at pobox.com>:

This seems like a bug in optparse.OptionParser:

    def exit(self, status=0, msg=None):
        if msg:
            sys.stderr.write(msg)
        sys.exit(status)

    def error(self, msg):
        """error(msg : string)

        Print a usage message incorporating 'msg' to stderr and exit.
        If you override this in a subclass, it should not return -- it
        should either exit or raise an exception.
        """
        self.print_usage(sys.stderr)
        self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg))

By default I think it should raise an exception when it encounters an
error, not exit.  Programmers shouldn't be forced to subclass code in
the standard library to get recommended practice.

If you feel this behavior can't be changed in 2.6 it should at least
be corrected in 3.0.

The cruel irony is that inside OptionParser.parse_args it actually
catches both BadOptionError and OptionValueError but suppresses them
by calling self.error() within the except block...  *arrgggghhh*...
The correct behavior there is (in my opinion) to get rid of the
try/except statement altogether and just let the exceptions propagate.
Other calls to self.error() should be replaced with suitable raise
statements.

Skip

----------
components: Library (Lib)
keywords: easy
messages: 67999
nosy: skip.montanaro
priority: normal
severity: normal
status: open
title: sys.exit() called from optparse - bad, bad, bad
type: behavior
versions: Python 2.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3079>
_______________________________________


More information about the Python-bugs-list mailing list