From e118682db13ba5d9dfaf92d7f97c705df18f9b73 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Fri, 15 Nov 2013 14:03:57 -0500 Subject: [PATCH] Added test for previous crash on failed import fix Also, rewrote the fix a bit. ref #383. --- _pytest/assertion/rewrite.py | 11 +++++------ testing/acceptance_test.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/_pytest/assertion/rewrite.py b/_pytest/assertion/rewrite.py index 2a94edd89..0ca796b4b 100644 --- a/_pytest/assertion/rewrite.py +++ b/_pytest/assertion/rewrite.py @@ -56,13 +56,12 @@ class AssertionRewritingHook(object): 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 --git a/testing/acceptance_test.py b/testing/acceptance_test.py index dc1ad81e4..b90178217 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -307,6 +307,24 @@ class TestGeneralUsage: '*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: