Skip to content

Commit 837a42d

Browse files
committed
gh-148315: Shell-quote the command line in pyvenv.cfg
The `command = ...` record written to `pyvenv.cfg` by `venv.EnvBuilder.create_configuration` was assembled with a plain `' '.join(args)` and an unquoted `sys.executable`. When any of these tokens contained whitespace (for example a Windows user directory like `C:\Users\Z B\...`), the recorded command was no longer a faithful reproduction and truncated at the first space when shell-parsed. Build the full argv as a list and emit it via `shlex.join`, and split `--prompt="..."` into two separate argv tokens so its value is quoted by `shlex.join` as well.
1 parent 639f218 commit 837a42d

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

Lib/venv/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,16 @@ def create_configuration(self, context):
252252
if self.upgrade_deps:
253253
args.append('--upgrade-deps')
254254
if self.orig_prompt is not None:
255-
args.append(f'--prompt="{self.orig_prompt}"')
255+
args.extend(['--prompt', self.orig_prompt])
256256
if not self.scm_ignore_files:
257257
args.append('--without-scm-ignore-files')
258258

259259
args.append(context.env_dir)
260-
args = ' '.join(args)
261-
f.write(f'command = {sys.executable} -m venv {args}\n')
260+
# gh-148315: shell-quote so paths containing whitespace
261+
# (e.g. a Windows user directory with a space) round-trip
262+
# faithfully and the recorded command can be re-executed.
263+
command = shlex.join([sys.executable, '-m', 'venv', *args])
264+
f.write(f'command = {command}\n')
262265

263266
def symlink_or_copy(self, src, dst, relative_symlinks_ok=False):
264267
"""

0 commit comments

Comments
 (0)