fix issue435: make reload() work when assert rewriting is active.

Thanks Daniel Hahler.
This commit is contained in:
holger krekel 2015-03-23 10:08:47 +01:00
commit f3580bee2d
3 changed files with 33 additions and 0 deletions

View File

@ -1,6 +1,9 @@
2.7.0.dev (compared to 2.6.4) 2.7.0.dev (compared to 2.6.4)
----------------------------- -----------------------------
- fix issue435: make reload() work when assert rewriting is active.
Thanks Daniel Hahler.
- fix issue616: conftest.py files and their contained fixutres are now - fix issue616: conftest.py files and their contained fixutres are now
properly considered for visibility, independently from the exact properly considered for visibility, independently from the exact
current working directory and test arguments that are used. current working directory and test arguments that are used.

View File

@ -146,6 +146,12 @@ class AssertionRewritingHook(object):
return self return self
def load_module(self, name): 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) co, pyc = self.modules.pop(name)
# I wish I could just call imp.load_compiled here, but __file__ has to # 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 # be set properly. In Python 3.2+, this all would be handled correctly

View File

@ -641,3 +641,27 @@ class TestAssertionRewriteHookDetails(object):
pyc.write(contents[:strip_bytes], mode='wb') pyc.write(contents[:strip_bytes], mode='wb')
assert _read_pyc(source, str(pyc)) is None # no error 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*",
])