Skip to content

pyimport: add support for PY_IGNORE_IMPORTMISMATCH #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion py/_path/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,9 @@ def pyimport(self, modname=None, ensuresyspath=True):
except py.error.ENOENT:
issame = False
if not issame:
raise self.ImportMismatchError(modname, modfile, self)
ignore = os.getenv('PY_IGNORE_IMPORTMISMATCH')
if ignore != '1':
raise self.ImportMismatchError(modname, modfile, self)
return mod
else:
try:
Expand Down
11 changes: 10 additions & 1 deletion testing/path/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,22 @@ def test_pyimport(self, path1):
assert obj.x == 42
assert obj.__name__ == 'execfile'

def test_pyimport_renamed_dir_creates_mismatch(self, tmpdir):
def test_pyimport_renamed_dir_creates_mismatch(self, tmpdir, monkeypatch):
p = tmpdir.ensure("a", "test_x123.py")
p.pyimport()
tmpdir.join("a").move(tmpdir.join("b"))
with pytest.raises(tmpdir.ImportMismatchError):
tmpdir.join("b", "test_x123.py").pyimport()

# Errors can be ignored.
monkeypatch.setenv('PY_IGNORE_IMPORTMISMATCH', '1')
tmpdir.join("b", "test_x123.py").pyimport()

# PY_IGNORE_IMPORTMISMATCH=0 does not ignore error.
monkeypatch.setenv('PY_IGNORE_IMPORTMISMATCH', '0')
with pytest.raises(tmpdir.ImportMismatchError):
tmpdir.join("b", "test_x123.py").pyimport()

def test_pyimport_messy_name(self, tmpdir):
# http://bitbucket.org/hpk42/py-trunk/issue/129
path = tmpdir.ensure('foo__init__.py')
Expand Down