3 new commits in pytest: https://bitbucket.org/hpk42/pytest/commits/dac4900b78f2/ Changeset: dac4900b78f2 User: hsoft Date: 2013-11-08 22:59:13 Summary: Fix TypeError crash on failed imports under py3.3.
Starting with Python 3.3, NamespacePath passed to importlib hooks seem to have lost the ability to be accessed by index. We wrap the index access in a try..except and wrap the path in a list if it happens. Fixes #383. Affected #: 1 file diff -r 257206079babf818a2819a44f5cf30c64d572287 -r dac4900b78f2046b734ab6d356ee82f910d35b75 _pytest/assertion/rewrite.py --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -57,7 +57,12 @@ lastname = names[-1] pth = None if path is not None and len(path) == 1: - pth = path[0] + try: + pth = path[0] + except TypeError: + # Starting with Python 3.3, `path` started being unsubscriptable, we have to wrap it + # in a list. + pth = list(path)[0] if pth is None: try: fd, fn, desc = imp.find_module(lastname, path) https://bitbucket.org/hpk42/pytest/commits/c9a1939bda35/ Changeset: c9a1939bda35 User: hsoft Date: 2013-11-15 20:03:57 Summary: Added test for previous crash on failed import fix Also, rewrote the fix a bit. ref #383. Affected #: 2 files diff -r dac4900b78f2046b734ab6d356ee82f910d35b75 -r c9a1939bda358b08bdfc0d1129c5fa874cff8f04 _pytest/assertion/rewrite.py --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -56,13 +56,12 @@ names = name.rsplit(".", 1) lastname = names[-1] pth = None - if path is not None and len(path) == 1: - try: + if path is not None: + # Starting with Python 3.3, path is a _NamespacePath(), which + # causes problems if not converted to list. + path = list(path) + if len(path) == 1: pth = path[0] - except TypeError: - # Starting with Python 3.3, `path` started being unsubscriptable, we have to wrap it - # in a list. - pth = list(path)[0] if pth is None: try: fd, fn, desc = imp.find_module(lastname, path) diff -r dac4900b78f2046b734ab6d356ee82f910d35b75 -r c9a1939bda358b08bdfc0d1129c5fa874cff8f04 testing/acceptance_test.py --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -307,6 +307,24 @@ '*ERROR*', ]) assert result.ret == 4 # usage error only if item not found + + def test_namespace_import_doesnt_confuse_import_hook(self, testdir): + # Ref #383. Python 3.3's namespace package messed with our import hooks + # Importing a module that didn't exist, even if the ImportError was + # gracefully handled, would make our test crash. + testdir.mkdir('not_a_package') + p = testdir.makepyfile(""" + try: + from not_a_package import doesnt_exist + except ImportError: + # We handle the import error gracefully here + pass + + def test_whatever(): + pass + """) + res = testdir.runpytest(p.basename) + assert res.ret == 0 class TestInvocationVariants: https://bitbucket.org/hpk42/pytest/commits/89b0f6f0e7d8/ Changeset: 89b0f6f0e7d8 User: hpk42 Date: 2013-11-15 21:02:30 Summary: Merged in hsoft/pytest (pull request #81) Fix TypeError crash on failed imports under py3.3. Affected #: 2 files diff -r 979602dcb416e9b537e109cbe853a0ef3b9bfbbe -r 89b0f6f0e7d8a87edd3fb9d1dcaa8c58d1ed0361 _pytest/assertion/rewrite.py --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -56,8 +56,12 @@ names = name.rsplit(".", 1) lastname = names[-1] pth = None - if path is not None and len(path) == 1: - pth = path[0] + if path is not None: + # Starting with Python 3.3, path is a _NamespacePath(), which + # causes problems if not converted to list. + path = list(path) + if len(path) == 1: + pth = path[0] if pth is None: try: fd, fn, desc = imp.find_module(lastname, path) diff -r 979602dcb416e9b537e109cbe853a0ef3b9bfbbe -r 89b0f6f0e7d8a87edd3fb9d1dcaa8c58d1ed0361 testing/acceptance_test.py --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -307,6 +307,24 @@ '*ERROR*', ]) assert result.ret == 4 # usage error only if item not found + + def test_namespace_import_doesnt_confuse_import_hook(self, testdir): + # Ref #383. Python 3.3's namespace package messed with our import hooks + # Importing a module that didn't exist, even if the ImportError was + # gracefully handled, would make our test crash. + testdir.mkdir('not_a_package') + p = testdir.makepyfile(""" + try: + from not_a_package import doesnt_exist + except ImportError: + # We handle the import error gracefully here + pass + + def test_whatever(): + pass + """) + res = testdir.runpytest(p.basename) + assert res.ret == 0 class TestInvocationVariants: Repository URL: https://bitbucket.org/hpk42/pytest/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email. _______________________________________________ pytest-commit mailing list pytest-commit@python.org https://mail.python.org/mailman/listinfo/pytest-commit