Fix `reload()` with modules handled via `python_files`
If a module exists in `sys.modules` already, `load_module` has to return it. Fixes https://bitbucket.org/pytest-dev/pytest/issue/435 --HG-- branch : fix-reload
This commit is contained in:
parent
41e6b04f0b
commit
c629f6b18b
|
@ -146,6 +146,12 @@ class AssertionRewritingHook(object):
|
|||
return self
|
||||
|
||||
def load_module(self, name):
|
||||
# If there is an existing module object named 'fullname' in
|
||||
# sys.modules, the loader must use that existing module. (Otherwise,
|
||||
# the reload() builtin will not work correctly.)
|
||||
if name in sys.modules:
|
||||
return sys.modules[name]
|
||||
|
||||
co, pyc = self.modules.pop(name)
|
||||
# I wish I could just call imp.load_compiled here, but __file__ has to
|
||||
# be set properly. In Python 3.2+, this all would be handled correctly
|
||||
|
|
|
@ -641,3 +641,27 @@ class TestAssertionRewriteHookDetails(object):
|
|||
pyc.write(contents[:strip_bytes], mode='wb')
|
||||
|
||||
assert _read_pyc(source, str(pyc)) is None # no error
|
||||
|
||||
def test_reload_is_same(self, testdir):
|
||||
# A file that will be picked up during collecting.
|
||||
testdir.tmpdir.join("file.py").ensure()
|
||||
testdir.tmpdir.join("pytest.ini").write(py.std.textwrap.dedent("""
|
||||
[pytest]
|
||||
python_files = *.py
|
||||
"""))
|
||||
|
||||
testdir.makepyfile(test_fun="""
|
||||
import sys
|
||||
try:
|
||||
from imp import reload
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
def test_loader():
|
||||
import file
|
||||
assert sys.modules["file"] is reload(file)
|
||||
""")
|
||||
result = testdir.runpytest('-s')
|
||||
result.stdout.fnmatch_lines([
|
||||
"* 1 passed*",
|
||||
])
|
||||
|
|
Loading…
Reference in New Issue