wxPython interactive interpreters (plural) may let Python crash

Windows 7 Pro 32 bit, Py 2.6.2, wxPy 2.8.10.1-ansi

I was toying, in fact learning, the Python optparse module.

When I introduced the following in my interactive Python shell, psi,
it causes a Python crash. There is a course a mistake in the code:
incomplete fake_args lists, missing filename parameter.

from optparse import OptionParser
parser = OptionParser()
parser.add_option('-f', '--file', dest='filename')

-f/--file

fake_args = ['-f']
(options, args) = parser.parse_args(fake_args)

[Python crashes and the app window (main wx.Frame) closes]

I repeated the experiment with PyShell, same behaviour.

from optparse import OptionParser
parser = OptionParser()
parser.add_option('-f', '--file', dest='filename')

<Option at 0x30310a8: -f/--file>

fake_args = ['-f']
(options, args) = parser.parse_args(fake_args)

[Python crashes and main frame closes]

With IDLE, the error is intercepted.

from optparse import OptionParser
parser = OptionParser()
parser.add_option('-f', '--file', dest='filename')

<Option at 0x2714620: -f/--file>

fake_args = ['-f']
(options, args) = parser.parse_args(fake_args)

Usage: [options]

: error: -f option requires an argument

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    (options, args) = parser.parse_args(fake_args)
  File "C:\Python26\lib\optparse.py", line 1394, in parse_args
    stop = self._process_args(largs, rargs, values)
  File "C:\Python26\lib\optparse.py", line 1438, in _process_args
    self._process_short_opts(rargs, values)
  File "C:\Python26\lib\optparse.py", line 1532, in
_process_short_opts
    self.error(_("%s option requires an argument") % opt)
  File "C:\Python26\lib\optparse.py", line 1578, in error
    self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg))
  File "C:\Python26\lib\optparse.py", line 1568, in exit
    sys.exit(status)
SystemExit: 2

No idea, from where this may come.

Jean-Michel Fauth, Switzerland

Erratum, Python 2.6.4 and not Python 2.6.2.

   File "C:\Python26\lib\optparse.py", line 1578, in error
     self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg))
   File "C:\Python26\lib\optparse.py", line 1568, in exit
     sys.exit(status)
SystemExit: 2

No idea, from where this may come.

It's not a crash, it's sys.exit doing what it is supposed to do. It raises SystemExit exception which apparently IDLE is catching and printing, but PyCrust and Psi are letting it go, which causes the interpreter to cleanly exit. Try this:

  >>> import sys
  >>> sys.exit()

Now it is debatable whether the fact that optparse is raising a SystemExit exception is a good thing or not, (personally I think not) but that is an argument for another day.

···

On 2/22/10 7:43 AM, jmfauth wrote:

--
Robin Dunn
Software Craftsman

Ok, I got it.
Thanks for the explanation.

jmf