|
22 | 22 | from virtualenv.create.via_global_ref.builtin.cpython.cpython2 import CPython2
|
23 | 23 | from virtualenv.discovery.builtin import get_interpreter
|
24 | 24 | from virtualenv.discovery.py_info import PythonInfo
|
25 |
| -from virtualenv.info import IS_PYPY, IS_WIN, PY3, fs_is_case_sensitive, fs_supports_symlink |
| 25 | +from virtualenv.info import IS_PYPY, IS_WIN, PY2, PY3, fs_is_case_sensitive, fs_supports_symlink |
26 | 26 | from virtualenv.pyenv_cfg import PyEnvCfg
|
27 | 27 | from virtualenv.run import cli_run, session_via_cli
|
28 | 28 | from virtualenv.util.path import Path
|
@@ -451,29 +451,31 @@ def _get_sys_path(flag=None):
|
451 | 451 | assert base == extra_all
|
452 | 452 |
|
453 | 453 |
|
454 |
| -@pytest.fixture |
455 |
| -def creator_with_pyc_only_cp2_modules(tmp_path, monkeypatch, current_creators): |
456 |
| - """ |
457 |
| - Get a current_creator in a context where CPython2.modules only |
458 |
| - references modules that have .pyc files (and no corresponding .py file). |
459 |
| - """ |
460 |
| - monkeypatch.chdir(tmp_path) |
461 |
| - creator = current_creators[2] |
462 |
| - module_name = os.path.relpath("CPython2_patch_modules_module", CURRENT.system_stdlib) |
463 |
| - module_path = Path(CURRENT.system_stdlib) / "{}.pyc".format(module_name) |
464 |
| - with module_path.open(mode="w"): |
465 |
| - pass # just a touch |
466 |
| - |
467 |
| - @classmethod |
468 |
| - def modules(cls): |
469 |
| - return [module_name] |
470 |
| - |
471 |
| - monkeypatch.setattr(CPython2, "modules", modules) |
472 |
| - yield creator |
473 |
| - module_path.unlink() |
474 |
| - |
475 |
| - |
476 |
| -@pytest.mark.skipif(PY3, reason=".py files are only checked for in py2.") |
477 |
| -def test_pyc_only(creator_with_pyc_only_cp2_modules): |
478 |
| - """Ensure that creation can succeed if os.pyc exists (even if os.py has been deleted).""" |
479 |
| - assert creator_with_pyc_only_cp2_modules.can_create(CURRENT) is not None |
| 454 | +@pytest.mark.skipif( |
| 455 | + not PY2 or not ("builtin" in CURRENT.creators().key_to_class), reason="stdlib python files only needed for Python 2" |
| 456 | +) |
| 457 | +def test_pyc_only(tmp_path, mocker, session_app_data): |
| 458 | + """Ensure that creation can succeed if os.pyc exists (even if os.py has been deleted)""" |
| 459 | + interpreter = PythonInfo.from_exe(sys.executable, session_app_data) |
| 460 | + host_pyc = interpreter.stdlib_path("os.pyc") |
| 461 | + if not host_pyc.exists(): |
| 462 | + pytest.skip("missing system os.pyc at {}".format(host_pyc)) |
| 463 | + previous = interpreter.stdlib_path |
| 464 | + |
| 465 | + def stdlib_path(name): |
| 466 | + path = previous(name) |
| 467 | + if name.endswith(".py"): |
| 468 | + |
| 469 | + class _Path(type(path)): |
| 470 | + @staticmethod |
| 471 | + def exists(): |
| 472 | + return False |
| 473 | + |
| 474 | + return _Path(path) |
| 475 | + return path |
| 476 | + |
| 477 | + mocker.patch.object(interpreter, "stdlib_path", side_effect=stdlib_path) |
| 478 | + result = cli_run([ensure_text(str(tmp_path)), "--without-pip", "--activators", ""]) |
| 479 | + assert not (result.creator.stdlib / "os.py").exists() |
| 480 | + assert (result.creator.stdlib / "os.pyc").exists() |
| 481 | + assert 'os.pyc' in result.creator.debug['os'] |
0 commit comments