Skip to content

[3.10] bpo-46197: Add isolated flag if currently isolated #30307

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

Closed
wants to merge 5 commits into from

Conversation

kcdodd
Copy link
Contributor

@kcdodd kcdodd commented Dec 30, 2021

https://bugs.python.org/issue46197

Potential resolution of the issue introduced around Python 3.8.7 where ensurepip module would run the bootstrapped pip in a subprocess and no longer obeyed the isolated flag set when run by the venv module. This could cause pip to not install itself in the virtual environment if it found an installation in another path.

Approach taken is simple, to access the isolated flag of the current process, and, if it set, also add it to the sub-process. If the current process was not run as isolated the sub-process will not, restoring this particular case seen in Python <= 3.8.6.

This behavior was manually verified to work in the case that PYTHONPATH contains another installation of pip.

The test was attempted, but the test_venv test appears to fail for another un-related issue persisting from the last commit of the forked branch 3.10 (without the requested change). For clarity that the test does not indicate a problem with this request inspected traces of the failure is included below:

Traceback (most recent call last):
  File "<string>", line 6, in <module>
  File "/media/cdodd/box/projects/contrib/cpython/Lib/runpy.py", line 205, in run_module
    mod_name, mod_spec, code = _get_module_details(mod_name)
  File "/media/cdodd/box/projects/contrib/cpython/Lib/runpy.py", line 129, in _get_module_details
    spec = importlib.util.find_spec(mod_name)
  File "/media/cdodd/box/projects/contrib/cpython/Lib/importlib/util.py", line 103, in find_spec
    return _find_spec(fullname, parent_path)
  File "<frozen importlib._bootstrap>", line 945, in _find_spec
  File "/tmp/tmpr0dlj3q6/lib/python3.10/site-packages/_distutils_hack/__init__.py", line 79, in find_spec
    return method()
  File "/tmp/tmpr0dlj3q6/lib/python3.10/site-packages/_distutils_hack/__init__.py", line 100, in spec_for_pip
    if self.pip_imported_during_build():
  File "/tmp/tmpr0dlj3q6/lib/python3.10/site-packages/_distutils_hack/__init__.py", line 111, in pip_imported_during_build
    return any(
  File "/tmp/tmpr0dlj3q6/lib/python3.10/site-packages/_distutils_hack/__init__.py", line 112, in <genexpr>
    frame.f_globals['__file__'].endswith('setup.py')
KeyError: '__file__'
Traceback (most recent call last):
  File "/media/cdodd/box/projects/contrib/cpython/Lib/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/media/cdodd/box/projects/contrib/cpython/Lib/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/media/cdodd/box/projects/contrib/cpython/Lib/ensurepip/_uninstall.py", line 31, in <module>
    sys.exit(_main())
  File "/media/cdodd/box/projects/contrib/cpython/Lib/ensurepip/_uninstall.py", line 27, in _main
    return ensurepip._uninstall_helper(verbosity=args.verbosity)
  File "/media/cdodd/box/projects/contrib/cpython/Lib/ensurepip/__init__.py", line 231, in _uninstall_helper
    return _run_pip([*args, *reversed(_PACKAGE_NAMES)])
  File "/media/cdodd/box/projects/contrib/cpython/Lib/ensurepip/__init__.py", line 102, in _run_pip
    return subprocess.run( cmd, check = True ).returncode
  File "/media/cdodd/box/projects/contrib/cpython/Lib/subprocess.py", line 524, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/tmp/tmpr0dlj3q6/bin/python', '-I', '-W', 'ignore::DeprecationWarning', '-c', '\nimport runpy\nimport sys\nsys.path = [] + sys.path\nsys.argv[1:] = [\'uninstall\', \'-y\', \'--disable-pip-version-check\', \'pip\', \'setuptools\']\nrunpy.run_module("pip", run_name="__main__", alter_sys=True)\n']' returned non-zero exit status 1.

ERROR

======================================================================
ERROR: test_with_pip (test.test_venv.EnsurePipTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/media/cdodd/box/projects/contrib/cpython/Lib/test/test_venv.py", line 551, in test_with_pip
    self.do_test_with_pip(True)
  File "/media/cdodd/box/projects/contrib/cpython/Lib/test/test_venv.py", line 517, in do_test_with_pip
    out, err = check_output([envpy,
  File "/media/cdodd/box/projects/contrib/cpython/Lib/test/test_venv.py", line 46, in check_output
    raise subprocess.CalledProcessError(
subprocess.CalledProcessError: Command '['/tmp/tmpr0dlj3q6/bin/python', '-W', 'ignore::DeprecationWarning', '-W', 'ignore::ImportWarning', '-I', '-m', 'ensurepip._uninstall']' returned non-zero exit status 1.

https://bugs.python.org/issue46197

@the-knights-who-say-ni

This comment was marked as resolved.

@kcdodd kcdodd changed the title bpo-46197: Add isolated flag if currently isolated [3.10] bpo-46197: Add isolated flag if currently isolated Jan 3, 2022
@github-actions
Copy link

github-actions bot commented Feb 3, 2022

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Feb 3, 2022
@ghost
Copy link

ghost commented May 16, 2022

The following commit authors need to sign the Contributor License Agreement:

Click the button to sign:
CLA not signed

@gvanrossum
Copy link
Member

@kcdodd As stated in the issue, this should be rebased onto main and once accepted and merged there it can be backported. Probably a new PR is easiest.

@gvanrossum
Copy link
Member

I recommend just clicking the CLA button. This is a side effect from the bpo->github migration. You'll only have to do it this once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review stale Stale PR or inactive for long period of time.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants