[Python-Dev] PEP 389: argparse - new command line parsing module

Steven D'Aprano steve at pearwood.info
Sat Oct 3 19:29:52 CEST 2009


On Sun, 4 Oct 2009 03:38:31 am Yuvgoog Greenle wrote:
> Check it out:
>
> def ParseAndRun():
>     crazy_external_function_that_might_exit()
>
>     # Argparse blah blah
>     parser.parse_args()
>
> if __name__ == "__main__":
>     try:
>         ParseAndRun()
>     except SystemExit:
>         # was it crazy_external_function_that_might_exit or an
> argparse error?

Does it matter? What difference does it make?


> I know this might come through as bike shedding but it's just
> customary python that every module have it's own exception types as
> to not mix them up with others.

You are mistaken.

>>> import os
>>> os.path.split(45)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.6/posixpath.py", line 82, in split
    i = p.rfind('/') + 1
AttributeError: 'int' object has no attribute 'rfind'

Plain, ordinary AttributeError, not OSModuleSubclassOfAttributeError.

I could show a thousand other examples. It simply isn't true that all, 
or even most, modules have their own exception types.

There's no reason to treat SystemExit as special in this regard. 
optparse, for example, calls sys.exit(), which operates by raising 
SystemExit. Ordinary SystemExit, not a special module-specific 
subclass.



-- 
Steven D'Aprano


More information about the Python-Dev mailing list