From 235f88674189547c50b72a11b7975bb231eddee5 Mon Sep 17 00:00:00 2001 From: Ales Erjavec Date: Tue, 10 May 2016 14:17:53 +0200 Subject: BLD: Avoid using os.spawnve in favor of os.spawnv in exec_command The env modifying spawn* function are not thread safe on windows (python issue6476) so avoid using them unless so requested by the caller. References #7607 --- numpy/distutils/exec_command.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'numpy/distutils/exec_command.py') diff --git a/numpy/distutils/exec_command.py b/numpy/distutils/exec_command.py index 50f03feeb..4a4bc67f2 100644 --- a/numpy/distutils/exec_command.py +++ b/numpy/distutils/exec_command.py @@ -363,10 +363,14 @@ def _exec_command( command, use_shell=None, use_tee = None, **env ): else: argv = shlex.split(command) + # `spawn*p` family with path (vp, vpe, ...) are not available on windows. + # Also prefer spawn{v,vp} in favor of spawn{ve,vpe} if no env + # modification is actually requested as the *e* functions are not thread + # safe on windows (https://bugs.python.org/issue6476) if hasattr(os, 'spawnvpe'): - spawn_command = os.spawnvpe + spawn_command = os.spawnvpe if env else os.spawnvp else: - spawn_command = os.spawnve + spawn_command = os.spawnve if env else os.spawnv argv[0] = find_executable(argv[0]) or argv[0] if not os.path.isfile(argv[0]): log.warn('Executable %s does not exist' % (argv[0])) @@ -395,7 +399,7 @@ def _exec_command( command, use_shell=None, use_tee = None, **env ): log.debug('Running %s(%s,%r,%r,os.environ)' \ % (spawn_command.__name__, os.P_WAIT, argv[0], argv)) - if sys.version_info[0] >= 3 and os.name == 'nt': + if env and sys.version_info[0] >= 3 and os.name == 'nt': # Pre-encode os.environ, discarding un-encodable entries, # to avoid it failing during encoding as part of spawn. Failure # is possible if the environment contains entries that are not @@ -431,7 +435,11 @@ def _exec_command( command, use_shell=None, use_tee = None, **env ): else: os.dup2(fout.fileno(), se_fileno) try: - status = spawn_command(os.P_WAIT, argv0, argv, encoded_environ) + # Use spawnv in favor of spawnve, unless necessary + if env: + status = spawn_command(os.P_WAIT, argv0, argv, encoded_environ) + else: + status = spawn_command(os.P_WAIT, argv0, argv) except Exception: errmess = str(get_exception()) status = 999 -- cgit v1.2.1