Make patch for issue in pkgutil.ImpImporter local by using contextmanager.
This commit is contained in:
parent
dc19624248
commit
3ca1e4b7f0
|
@ -1,8 +1,10 @@
|
||||||
""" core implementation of testing process: init, session, runtest loop. """
|
""" core implementation of testing process: init, session, runtest loop. """
|
||||||
from __future__ import absolute_import, division, print_function
|
from __future__ import absolute_import, division, print_function
|
||||||
|
|
||||||
|
import contextlib
|
||||||
import functools
|
import functools
|
||||||
import os
|
import os
|
||||||
|
import pkgutil
|
||||||
import six
|
import six
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
@ -728,16 +730,28 @@ class Session(FSCollector):
|
||||||
"""Convert a dotted module name to path.
|
"""Convert a dotted module name to path.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
import pkgutil
|
@contextlib.contextmanager
|
||||||
|
def _patched_find_module():
|
||||||
|
"""Patch bug in pkgutil.ImpImporter.find_module
|
||||||
|
|
||||||
|
When using pkgutil.find_loader on python<3.4 it removes symlinks
|
||||||
|
from the path due to a call to os.path.realpath. This is not consistent
|
||||||
|
with actually doing the import (in these versions, pkgutil and __import__
|
||||||
|
did not share the same underlying code). This can break conftest
|
||||||
|
discovery for pytest where symlinks are involved.
|
||||||
|
|
||||||
|
The only supported python<3.4 by pytest is python 2.7.
|
||||||
|
"""
|
||||||
if six.PY2: # python 3.4+ uses importlib instead
|
if six.PY2: # python 3.4+ uses importlib instead
|
||||||
def find_module_patched(self, fullname, path=None):
|
def find_module_patched(self, fullname, path=None):
|
||||||
|
# Note: we ignore 'path' argument since it is only used via meta_path
|
||||||
subname = fullname.split(".")[-1]
|
subname = fullname.split(".")[-1]
|
||||||
if subname != fullname and self.path is None:
|
if subname != fullname and self.path is None:
|
||||||
return None
|
return None
|
||||||
if self.path is None:
|
if self.path is None:
|
||||||
path = None
|
path = None
|
||||||
else:
|
else:
|
||||||
|
# original: path = [os.path.realpath(self.path)]
|
||||||
path = [self.path]
|
path = [self.path]
|
||||||
try:
|
try:
|
||||||
file, filename, etc = pkgutil.imp.find_module(subname,
|
file, filename, etc = pkgutil.imp.find_module(subname,
|
||||||
|
@ -746,9 +760,17 @@ class Session(FSCollector):
|
||||||
return None
|
return None
|
||||||
return pkgutil.ImpLoader(fullname, file, filename, etc)
|
return pkgutil.ImpLoader(fullname, file, filename, etc)
|
||||||
|
|
||||||
|
old_find_module = pkgutil.ImpImporter.find_module
|
||||||
pkgutil.ImpImporter.find_module = find_module_patched
|
pkgutil.ImpImporter.find_module = find_module_patched
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
pkgutil.ImpImporter.find_module = old_find_module
|
||||||
|
else:
|
||||||
|
yield
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
with _patched_find_module():
|
||||||
loader = pkgutil.find_loader(x)
|
loader = pkgutil.find_loader(x)
|
||||||
except ImportError:
|
except ImportError:
|
||||||
return x
|
return x
|
||||||
|
@ -757,6 +779,7 @@ class Session(FSCollector):
|
||||||
# This method is sometimes invoked when AssertionRewritingHook, which
|
# This method is sometimes invoked when AssertionRewritingHook, which
|
||||||
# does not define a get_filename method, is already in place:
|
# does not define a get_filename method, is already in place:
|
||||||
try:
|
try:
|
||||||
|
with _patched_find_module():
|
||||||
path = loader.get_filename(x)
|
path = loader.get_filename(x)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
# Retrieve path from AssertionRewritingHook:
|
# Retrieve path from AssertionRewritingHook:
|
||||||
|
|
Loading…
Reference in New Issue