Skip to content

Commit 1e29553

Browse files
committed
Move _patched_find_module to module namespace.
1 parent 3ca1e4b commit 1e29553

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

_pytest/main.py

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,46 @@ def pytest_ignore_collect(path, config):
208208
return False
209209

210210

211+
@contextlib.contextmanager
212+
def _patched_find_module():
213+
"""Patch bug in pkgutil.ImpImporter.find_module
214+
215+
When using pkgutil.find_loader on python<3.4 it removes symlinks
216+
from the path due to a call to os.path.realpath. This is not consistent
217+
with actually doing the import (in these versions, pkgutil and __import__
218+
did not share the same underlying code). This can break conftest
219+
discovery for pytest where symlinks are involved.
220+
221+
The only supported python<3.4 by pytest is python 2.7.
222+
"""
223+
if six.PY2: # python 3.4+ uses importlib instead
224+
def find_module_patched(self, fullname, path=None):
225+
# Note: we ignore 'path' argument since it is only used via meta_path
226+
subname = fullname.split(".")[-1]
227+
if subname != fullname and self.path is None:
228+
return None
229+
if self.path is None:
230+
path = None
231+
else:
232+
# original: path = [os.path.realpath(self.path)]
233+
path = [self.path]
234+
try:
235+
file, filename, etc = pkgutil.imp.find_module(subname,
236+
path)
237+
except ImportError:
238+
return None
239+
return pkgutil.ImpLoader(fullname, file, filename, etc)
240+
241+
old_find_module = pkgutil.ImpImporter.find_module
242+
pkgutil.ImpImporter.find_module = find_module_patched
243+
try:
244+
yield
245+
finally:
246+
pkgutil.ImpImporter.find_module = old_find_module
247+
else:
248+
yield
249+
250+
211251
class FSHookProxy:
212252
def __init__(self, fspath, pm, remove_mods):
213253
self.fspath = fspath
@@ -730,44 +770,6 @@ def _tryconvertpyarg(self, x):
730770
"""Convert a dotted module name to path.
731771
732772
"""
733-
@contextlib.contextmanager
734-
def _patched_find_module():
735-
"""Patch bug in pkgutil.ImpImporter.find_module
736-
737-
When using pkgutil.find_loader on python<3.4 it removes symlinks
738-
from the path due to a call to os.path.realpath. This is not consistent
739-
with actually doing the import (in these versions, pkgutil and __import__
740-
did not share the same underlying code). This can break conftest
741-
discovery for pytest where symlinks are involved.
742-
743-
The only supported python<3.4 by pytest is python 2.7.
744-
"""
745-
if six.PY2: # python 3.4+ uses importlib instead
746-
def find_module_patched(self, fullname, path=None):
747-
# Note: we ignore 'path' argument since it is only used via meta_path
748-
subname = fullname.split(".")[-1]
749-
if subname != fullname and self.path is None:
750-
return None
751-
if self.path is None:
752-
path = None
753-
else:
754-
# original: path = [os.path.realpath(self.path)]
755-
path = [self.path]
756-
try:
757-
file, filename, etc = pkgutil.imp.find_module(subname,
758-
path)
759-
except ImportError:
760-
return None
761-
return pkgutil.ImpLoader(fullname, file, filename, etc)
762-
763-
old_find_module = pkgutil.ImpImporter.find_module
764-
pkgutil.ImpImporter.find_module = find_module_patched
765-
try:
766-
yield
767-
finally:
768-
pkgutil.ImpImporter.find_module = old_find_module
769-
else:
770-
yield
771773

772774
try:
773775
with _patched_find_module():

0 commit comments

Comments
 (0)