astropy/astropy — pull request #1058 by eteq (2013-05-07 04:32:20+00:00)
Title: Prevent KeyError when build_sphinx fails in ``-w`` mode

This addresses a problem that has been particularly apparent in the Travis ``build_sphinx`` builds. Whenever sphing generates warnings, a `KeyError` message appears that often misleads people into thinking that's the error, rather than the sphinx warnings. This is discussed some in #982, although I have more info here.

I managed to trace the problem to the use of `atexit` in the `AstropyBuildSphinx` command. Prior to this PR, when the ``-w`` mode is active for ``build_sphinx``, if sphinx failed it would add an `atexit` handler that raises a `SystemExit` error (that's the same thing as calling `sys.exit`). This was necessary because Travis needs a non-zero exit code to know the build failed. Apparently, though, something in the `threading` module is unhappy with this - I think it is basically trying to exit twice, once when the first exit is hit, and again after the `atexit` handler raises a `SystemExit`. It was working, but the `threading` module was confused by this, and raised errors.

The solution is to instead call `sys.exit` at the very end of the sphinx build. This is not perfect, because if in the future we (or an affiliated package) puts something after the call to `setup` in ``setup.py``, it will be skipped due to the premature `sys.exit`. But I know of no other way to tell python what the exit code should be.

You can see (roughly) the before-and-after result at https://travis-ci.org/eteq/astropy/jobs/6940048 vs. https://travis-ci.org/eteq/astropy/jobs/6941905

cc @astrofrog @iguananaut @keflavich