Skip to content

Commit

Permalink
Fix Windows subprocess calls with cwd are broken
Browse files Browse the repository at this point in the history
Signed-off-by: Bernát Gábor <bgabor8@bloomberg.net>
  • Loading branch information
gaborbernat committed Oct 15, 2020
1 parent 644bb08 commit 6875f1a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/changelog/1982.bugfix.rst
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Bump embeded setuptools from ``50.3.0`` to ``50.3.1`` - by :user:`gaborbernat`.
Bump embedded setuptools from ``50.3.0`` to ``50.3.1`` - by :user:`gaborbernat`.
1 change: 1 addition & 0 deletions docs/changelog/1983.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
After importing virtualenv passing cwd to a subprocess calls breaks with ``invalid directory`` - by :user:`gaborbernat`.
8 changes: 7 additions & 1 deletion src/virtualenv/util/subprocess/_win_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,19 @@ def CreateProcess(
wenv = (c_wchar * len(env))()
wenv.value = env

wcwd = None
if cwd is not None:
cwd = unicode(cwd) + unicode("\0")
wcwd = (c_wchar * len(cwd))()
wcwd.value = cwd

pi = PROCESS_INFORMATION()
creation_flags |= CREATE_UNICODE_ENVIRONMENT

if CreateProcessW(
executable, args, None, None,
inherit_handles, creation_flags,
wenv, cwd, byref(si), byref(pi),
wenv, wcwd, byref(si), byref(pi),
):
return (
DUMMY_HANDLE(pi.hProcess), DUMMY_HANDLE(pi.hThread),
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/test_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from __future__ import absolute_import, unicode_literals

import subprocess
import sys

import pytest

from virtualenv.info import IS_WIN, PY2
from virtualenv.util.subprocess import run_cmd


Expand All @@ -8,3 +14,8 @@ def test_run_fail(tmp_path):
assert err
assert not out
assert code


@pytest.mark.skipif(not (PY2 and IS_WIN), reason="subprocess patch only applied on Windows python2")
def test_windows_py2_cwd_works(tmp_path):
subprocess.check_call([sys.executable, "-c", "print(1)"], cwd=str(tmp_path))

0 comments on commit 6875f1a

Please sign in to comment.