forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythonGH-126789: fix some sysconfig data on late site initializations
(cherry picked from commit acbd5c9) Co-authored-by: Filipe Laíns 🇵🇸 <lains@riseup.net>
- Loading branch information
1 parent
d74f1f4
commit a3e0c59
Showing
4 changed files
with
163 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import contextlib | ||
import logging | ||
import os | ||
import subprocess | ||
import shlex | ||
import sys | ||
import sysconfig | ||
import tempfile | ||
import venv | ||
|
||
|
||
class VirtualEnvironment: | ||
def __init__(self, prefix, **venv_create_args): | ||
self._logger = logging.getLogger(self.__class__.__name__) | ||
venv.create(prefix, **venv_create_args) | ||
self._prefix = prefix | ||
self._paths = sysconfig.get_paths( | ||
scheme='venv', | ||
vars={'base': self.prefix}, | ||
expand=True, | ||
) | ||
|
||
@classmethod | ||
@contextlib.contextmanager | ||
def from_tmpdir(cls, *, prefix=None, dir=None, **venv_create_args): | ||
delete = not bool(os.environ.get('PYTHON_TESTS_KEEP_VENV')) | ||
with tempfile.TemporaryDirectory(prefix=prefix, dir=dir, delete=delete) as tmpdir: | ||
yield cls(tmpdir, **venv_create_args) | ||
|
||
@property | ||
def prefix(self): | ||
return self._prefix | ||
|
||
@property | ||
def paths(self): | ||
return self._paths | ||
|
||
@property | ||
def interpreter(self): | ||
return os.path.join(self.paths['scripts'], os.path.basename(sys.executable)) | ||
|
||
def _format_output(self, name, data, indent='\t'): | ||
if not data: | ||
return indent + f'{name}: (none)' | ||
if len(data.splitlines()) == 1: | ||
return indent + f'{name}: {data}' | ||
else: | ||
prefixed_lines = '\n'.join(indent + '> ' + line for line in data.splitlines()) | ||
return indent + f'{name}:\n' + prefixed_lines | ||
|
||
def run(self, *args, **subprocess_args): | ||
if subprocess_args.get('shell'): | ||
raise ValueError('Running the subprocess in shell mode is not supported.') | ||
default_args = { | ||
'capture_output': True, | ||
'check': True, | ||
} | ||
try: | ||
result = subprocess.run([self.interpreter, *args], **default_args | subprocess_args) | ||
except subprocess.CalledProcessError as e: | ||
if e.returncode != 0: | ||
self._logger.error( | ||
f'Interpreter returned non-zero exit status {e.returncode}.\n' | ||
+ self._format_output('COMMAND', shlex.join(e.cmd)) + '\n' | ||
+ self._format_output('STDOUT', e.stdout.decode()) + '\n' | ||
+ self._format_output('STDERR', e.stderr.decode()) + '\n' | ||
) | ||
raise | ||
else: | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Library/2024-11-13-22-25-57.gh-issue-126789.lKzlc7.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Fixed the values of :py:func:`sysconfig.get_config_vars`, | ||
:py:func:`sysconfig.get_paths`, and their siblings when the :py:mod:`site` | ||
initialization happens after :py:mod:`sysconfig` has built a cache for | ||
:py:func:`sysconfig.get_config_vars`. |