Fix `ImportWarning` triggered by explicit relative imports

This commit is contained in:
Anthony Sottile 2018-06-23 07:55:42 -07:00
parent 630428c611
commit 1d55c49a9a
2 changed files with 11 additions and 0 deletions

View File

@ -0,0 +1 @@
Fix ``ImportWarning`` triggered by explicit relative imports in assertion-rewritten package modules.

View File

@ -45,6 +45,14 @@ else:
return ast.Call(a, b, c, None, None)
if sys.version_info >= (3, 4):
from importlib.util import spec_from_file_location
else:
def spec_from_file_location(*_, **__):
return None
class AssertionRewritingHook(object):
"""PEP302 Import hook which rewrites asserts."""
@ -213,6 +221,8 @@ class AssertionRewritingHook(object):
# Normally, this attribute is 3.2+.
mod.__cached__ = pyc
mod.__loader__ = self
# Normally, this attribute is 3.4+
mod.__spec__ = spec_from_file_location(name, co.co_filename, loader=self)
py.builtin.exec_(co, mod.__dict__)
except: # noqa
if name in sys.modules: