Skip to content

Commit

Permalink
Merge pull request #42 from jsirois/jsirois/pex/fix_run_except_handling
Browse files Browse the repository at this point in the history
Honor installed sys.excepthook in pex teardown.
  • Loading branch information
wickman committed Feb 8, 2015
2 parents 89bd4ff + 846541c commit 9c2048c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
33 changes: 33 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,39 @@
CHANGES
=======

----------
0.8.6-dev0
----------

* Bug fix: Honor installed sys.excepthook in pex teardown.
`RB #1733 <https://rbcommons.com/s/twitter/r/1733>`_

-----
0.8.5
-----

* Bug fix: Fixup string formatting in pex/bin/pex.py to support Python 2.6
`Pull Request #40 <https://github.com/pantsbuild/pex/pull/40>`_

-----
0.8.4
-----

* Performance improvement: Speed up the best-case scenario of dependency resolution.
`RB #1685 <https://rbcommons.com/s/twitter/r/1685>`_

* Bug fix: Change from uuid4().get_hex() to uuid4().hex to maintain Python3
compatibility of pex.common.
`Pull Request #39 <https://github.com/pantsbuild/pex/pull/39>`_

* Bug fix: Actually cache the results of translation. Previously bdist translations
would be created in a temporary directory even if a cache location was specified.
`RB #1666 <https://rbcommons.com/s/twitter/r/1666>`_

* Bug fix: Support all potential abi tag permutations when determining platform
compatibility.
`Pull Request #33 <https://github.com/pantsbuild/pex/pull/33>`_

-----
0.8.3
-----
Expand Down
8 changes: 4 additions & 4 deletions pex/pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import os
import subprocess
import sys
import traceback
from contextlib import contextmanager
from distutils import sysconfig
from site import USER_SITE
Expand Down Expand Up @@ -273,15 +272,16 @@ def execute(self, args=()):
else:
self.execute_interpreter()
except Exception:
# Catch and print any exceptions before we tear things down in finally, then
# reraise so that the exit status is reflected correctly.
traceback.print_exc()
# Allow the current sys.excepthook to handle this app exception before we tear things down in
# finally, then reraise so that the exit status is reflected correctly.
sys.excepthook(*sys.exc_info())
raise
finally:
# squash all exceptions on interpreter teardown -- the primary type here are
# atexit handlers failing to run because of things such as:
# http://stackoverflow.com/questions/2572172/referencing-other-modules-in-atexit
if 'PEX_TEARDOWN_VERBOSE' not in os.environ:
sys.stderr.flush()
sys.stderr = DevNull()
sys.excepthook = lambda *a, **kw: None

Expand Down
2 changes: 1 addition & 1 deletion pex/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.8.5'
__version__ = '0.8.6-dev0'
18 changes: 18 additions & 0 deletions tests/test_pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ def test_pex_uncaught_exceptions():
assert rc == 1


def test_excepthook_honored():
body = textwrap.dedent("""
import sys
def excepthook(ex_type, ex, tb):
print('Custom hook called with: {0}'.format(ex))
sys.exit(42)
sys.excepthook = excepthook
raise Exception('This is an exception')
""")

so, rc = run_simple_pex_test(body)
assert so == b'Custom hook called with: This is an exception\n', 'Standard out was: %s' % so
assert rc == 42


def test_pex_sys_exit_does_not_raise():
body = "import sys; sys.exit(2)"
so, rc = run_simple_pex_test(body)
Expand Down

0 comments on commit 9c2048c

Please sign in to comment.