Mercurial > cpython-lru_cache-weakref
annotate Lib/distutils/spawn.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 01434498e55e |
| children |
| rev | line source |
|---|---|
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
1 """distutils.spawn |
|
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
2 |
|
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
3 Provides the 'spawn()' function, a front-end to various platform- |
|
13803
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
4 specific functions for launching another program in a sub-process. |
|
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
5 Also provides the 'find_executable()' to search the path for a given |
| 14861 | 6 executable name. |
| 7 """ | |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
8 |
|
11897
017e592c0fd2
Changed '__rcsid__' to '__revision__'.
Greg Ward <gward@python.net>
parents:
11665
diff
changeset
|
9 __revision__ = "$Id$" |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
10 |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
11 import sys |
|
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
12 import os |
|
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
13 |
|
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
14 from distutils.errors import DistutilsPlatformError, DistutilsExecError |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21851
diff
changeset
|
15 from distutils import log |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
16 |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
17 def spawn(cmd, search_path=1, verbose=0, dry_run=0): |
|
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
18 """Run another program, specified as a command list 'cmd', in a new process. |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
19 |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
20 'cmd' is just the argument list for the new process, ie. |
| 14861 | 21 cmd[0] is the program to run and cmd[1:] are the rest of its arguments. |
| 22 There is no way to run a program with a name different from that of its | |
| 23 executable. | |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
24 |
| 26324 | 25 If 'search_path' is true (the default), the system's executable |
| 26 search path will be used to find the program; otherwise, cmd[0] | |
| 27 must be the exact path to the executable. If 'dry_run' is true, | |
| 14861 | 28 the command will not actually be run. |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
29 |
| 14861 | 30 Raise DistutilsExecError if running the program fails in any way; just |
| 31 return on success. | |
| 32 """ | |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
33 if os.name == 'posix': |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21851
diff
changeset
|
34 _spawn_posix(cmd, search_path, dry_run=dry_run) |
| 11257 | 35 elif os.name == 'nt': |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21851
diff
changeset
|
36 _spawn_nt(cmd, search_path, dry_run=dry_run) |
|
21851
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
37 elif os.name == 'os2': |
|
23668
ca87119255eb
Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
21851
diff
changeset
|
38 _spawn_os2(cmd, search_path, dry_run=dry_run) |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
39 else: |
|
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
40 raise DistutilsPlatformError, \ |
|
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
41 "don't know how to spawn programs on platform '%s'" % os.name |
|
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
42 |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
43 def _nt_quote_args(args): |
|
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
44 """Quote command-line arguments for DOS/Windows conventions. |
| 11257 | 45 |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
46 Just wraps every argument which contains blanks in double quotes, and |
| 14861 | 47 returns a new argument list. |
| 48 """ | |
|
11933
68e0ef052815
Added '_nt_quote_args()' to deal with whitespace in command-line arguments
Greg Ward <gward@python.net>
parents:
11897
diff
changeset
|
49 # XXX this doesn't seem very robust to me -- but if the Windows guys |
|
68e0ef052815
Added '_nt_quote_args()' to deal with whitespace in command-line arguments
Greg Ward <gward@python.net>
parents:
11897
diff
changeset
|
50 # say it'll work, I guess I'll have to accept it. (What if an arg |
|
68e0ef052815
Added '_nt_quote_args()' to deal with whitespace in command-line arguments
Greg Ward <gward@python.net>
parents:
11897
diff
changeset
|
51 # contains quotes? What other magic characters, other than spaces, |
|
68e0ef052815
Added '_nt_quote_args()' to deal with whitespace in command-line arguments
Greg Ward <gward@python.net>
parents:
11897
diff
changeset
|
52 # have to be escaped? Is there an escaping mechanism other than |
|
68e0ef052815
Added '_nt_quote_args()' to deal with whitespace in command-line arguments
Greg Ward <gward@python.net>
parents:
11897
diff
changeset
|
53 # quoting?) |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
54 for i, arg in enumerate(args): |
|
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
55 if ' ' in arg: |
|
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
56 args[i] = '"%s"' % arg |
|
12058
c6cb0d266839
Duh, it helps if '_nt_quote_args()' actually returns the mutated list,
Greg Ward <gward@python.net>
parents:
12043
diff
changeset
|
57 return args |
|
11933
68e0ef052815
Added '_nt_quote_args()' to deal with whitespace in command-line arguments
Greg Ward <gward@python.net>
parents:
11897
diff
changeset
|
58 |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
59 def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0): |
|
11250
b3f1abd3027f
Patch from Perry Stoll: support for Windows.
Greg Ward <gward@python.net>
parents:
11198
diff
changeset
|
60 executable = cmd[0] |
| 14861 | 61 cmd = _nt_quote_args(cmd) |
|
11250
b3f1abd3027f
Patch from Perry Stoll: support for Windows.
Greg Ward <gward@python.net>
parents:
11198
diff
changeset
|
62 if search_path: |
|
13803
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
63 # either we find one or it stays the same |
| 21267 | 64 executable = find_executable(executable) or executable |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
65 log.info(' '.join([executable] + cmd[1:])) |
|
11250
b3f1abd3027f
Patch from Perry Stoll: support for Windows.
Greg Ward <gward@python.net>
parents:
11198
diff
changeset
|
66 if not dry_run: |
|
b3f1abd3027f
Patch from Perry Stoll: support for Windows.
Greg Ward <gward@python.net>
parents:
11198
diff
changeset
|
67 # spawn for NT requires a full path to the .exe |
|
11665
e998b350a4bb
Catch OSError from 'spawnv()' in '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
11257
diff
changeset
|
68 try: |
| 14861 | 69 rc = os.spawnv(os.P_WAIT, executable, cmd) |
|
11665
e998b350a4bb
Catch OSError from 'spawnv()' in '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
11257
diff
changeset
|
70 except OSError, exc: |
|
e998b350a4bb
Catch OSError from 'spawnv()' in '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
11257
diff
changeset
|
71 # this seems to happen when the command isn't found |
|
e998b350a4bb
Catch OSError from 'spawnv()' in '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
11257
diff
changeset
|
72 raise DistutilsExecError, \ |
|
e998b350a4bb
Catch OSError from 'spawnv()' in '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
11257
diff
changeset
|
73 "command '%s' failed: %s" % (cmd[0], exc[-1]) |
|
11250
b3f1abd3027f
Patch from Perry Stoll: support for Windows.
Greg Ward <gward@python.net>
parents:
11198
diff
changeset
|
74 if rc != 0: |
|
11665
e998b350a4bb
Catch OSError from 'spawnv()' in '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
11257
diff
changeset
|
75 # and this reflects the command running but failing |
|
e998b350a4bb
Catch OSError from 'spawnv()' in '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
11257
diff
changeset
|
76 raise DistutilsExecError, \ |
|
e998b350a4bb
Catch OSError from 'spawnv()' in '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
11257
diff
changeset
|
77 "command '%s' failed with exit status %d" % (cmd[0], rc) |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
78 |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
79 def _spawn_os2(cmd, search_path=1, verbose=0, dry_run=0): |
|
21851
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
80 executable = cmd[0] |
|
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
81 if search_path: |
|
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
82 # either we find one or it stays the same |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
31586
diff
changeset
|
83 executable = find_executable(executable) or executable |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
84 log.info(' '.join([executable] + cmd[1:])) |
|
21851
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
85 if not dry_run: |
|
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
86 # spawnv for OS/2 EMX requires a full path to the .exe |
|
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
87 try: |
|
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
88 rc = os.spawnv(os.P_WAIT, executable, cmd) |
|
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
89 except OSError, exc: |
|
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
90 # this seems to happen when the command isn't found |
|
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
91 raise DistutilsExecError, \ |
|
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
92 "command '%s' failed: %s" % (cmd[0], exc[-1]) |
|
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
93 if rc != 0: |
|
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
94 # and this reflects the command running but failing |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
95 log.debug("command '%s' failed with exit status %d" % (cmd[0], rc)) |
|
21851
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
96 raise DistutilsExecError, \ |
|
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
97 "command '%s' failed with exit status %d" % (cmd[0], rc) |
|
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
98 |
|
71046
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
99 if sys.platform == 'darwin': |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
100 from distutils import sysconfig |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
101 _cfg_target = None |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
102 _cfg_target_split = None |
|
21851
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
103 |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
104 def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0): |
|
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
105 log.info(' '.join(cmd)) |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
106 if dry_run: |
|
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
107 return |
|
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
108 exec_fn = search_path and os.execvp or os.execv |
|
71046
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
109 exec_args = [cmd[0], cmd] |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
110 if sys.platform == 'darwin': |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
111 global _cfg_target, _cfg_target_split |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
112 if _cfg_target is None: |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
113 _cfg_target = sysconfig.get_config_var( |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
114 'MACOSX_DEPLOYMENT_TARGET') or '' |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
115 if _cfg_target: |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
116 _cfg_target_split = [int(x) for x in _cfg_target.split('.')] |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
117 if _cfg_target: |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
118 # ensure that the deployment target of build process is not less |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
119 # than that used when the interpreter was built. This ensures |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
120 # extension modules are built with correct compatibility values |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
121 cur_target = os.environ.get('MACOSX_DEPLOYMENT_TARGET', _cfg_target) |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
122 if _cfg_target_split > [int(x) for x in cur_target.split('.')]: |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
123 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: ' |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
124 'now "%s" but "%s" during configure' |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
125 % (cur_target, _cfg_target)) |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
126 raise DistutilsPlatformError(my_msg) |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
127 env = dict(os.environ, |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
128 MACOSX_DEPLOYMENT_TARGET=cur_target) |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
129 exec_fn = search_path and os.execvpe or os.execve |
|
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
130 exec_args.append(env) |
| 14861 | 131 pid = os.fork() |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
132 |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
133 if pid == 0: # in the child |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
134 try: |
|
71046
01434498e55e
Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents:
54520
diff
changeset
|
135 exec_fn(*exec_args) |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
136 except OSError, e: |
| 14861 | 137 sys.stderr.write("unable to execute %s: %s\n" % |
| 138 (cmd[0], e.strerror)) | |
| 139 os._exit(1) | |
| 21267 | 140 |
| 14861 | 141 sys.stderr.write("unable to execute %s for unknown reasons" % cmd[0]) |
| 142 os._exit(1) | |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
143 else: # in the parent |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
144 # Loop until the child either exits or is terminated by a signal |
|
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
145 # (ie. keep waiting if it's merely stopped) |
|
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
146 while 1: |
|
31586
54685d9ffb87
Make _spawn_posix be ready for EINTR. waitpid(2) can be interrupted
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26324
diff
changeset
|
147 try: |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
148 pid, status = os.waitpid(pid, 0) |
|
31586
54685d9ffb87
Make _spawn_posix be ready for EINTR. waitpid(2) can be interrupted
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26324
diff
changeset
|
149 except OSError, exc: |
|
54685d9ffb87
Make _spawn_posix be ready for EINTR. waitpid(2) can be interrupted
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26324
diff
changeset
|
150 import errno |
|
54685d9ffb87
Make _spawn_posix be ready for EINTR. waitpid(2) can be interrupted
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26324
diff
changeset
|
151 if exc.errno == errno.EINTR: |
|
54685d9ffb87
Make _spawn_posix be ready for EINTR. waitpid(2) can be interrupted
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26324
diff
changeset
|
152 continue |
|
54685d9ffb87
Make _spawn_posix be ready for EINTR. waitpid(2) can be interrupted
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26324
diff
changeset
|
153 raise DistutilsExecError, \ |
|
54685d9ffb87
Make _spawn_posix be ready for EINTR. waitpid(2) can be interrupted
Hye-Shik Chang <hyeshik@gmail.com>
parents:
26324
diff
changeset
|
154 "command '%s' failed: %s" % (cmd[0], exc[-1]) |
| 14861 | 155 if os.WIFSIGNALED(status): |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
156 raise DistutilsExecError, \ |
|
11665
e998b350a4bb
Catch OSError from 'spawnv()' in '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
11257
diff
changeset
|
157 "command '%s' terminated by signal %d" % \ |
| 14861 | 158 (cmd[0], os.WTERMSIG(status)) |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
159 |
| 14861 | 160 elif os.WIFEXITED(status): |
| 161 exit_status = os.WEXITSTATUS(status) | |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
162 if exit_status == 0: |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
163 return # hey, it succeeded! |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
164 else: |
|
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
165 raise DistutilsExecError, \ |
|
11665
e998b350a4bb
Catch OSError from 'spawnv()' in '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
11257
diff
changeset
|
166 "command '%s' failed with exit status %d" % \ |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
167 (cmd[0], exit_status) |
| 21267 | 168 |
| 14861 | 169 elif os.WIFSTOPPED(status): |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
170 continue |
|
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
171 |
|
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
172 else: |
|
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
173 raise DistutilsExecError, \ |
|
11665
e998b350a4bb
Catch OSError from 'spawnv()' in '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
11257
diff
changeset
|
174 "unknown error executing '%s': termination status %d" % \ |
|
11198
e8b24cd17df2
Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
175 (cmd[0], status) |
|
13803
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
176 |
|
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
177 def find_executable(executable, path=None): |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
178 """Tries to find 'executable' in the directories listed in 'path'. |
|
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
179 |
|
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
180 A string listing directories separated by 'os.pathsep'; defaults to |
|
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
181 os.environ['PATH']. Returns the complete filename or None if not found. |
|
13803
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
182 """ |
|
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
183 if path is None: |
|
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
184 path = os.environ['PATH'] |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
185 paths = path.split(os.pathsep) |
|
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
186 base, ext = os.path.splitext(executable) |
|
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
187 |
|
21851
e0fc286d57a4
OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents:
21267
diff
changeset
|
188 if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'): |
|
13803
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
189 executable = executable + '.exe' |
|
54520
4283bc8c9306
improved distutils.spawn test coverage + cleaned it up
Tarek Ziadé <ziade.tarek@gmail.com>
parents:
51047
diff
changeset
|
190 |
|
13803
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
191 if not os.path.isfile(executable): |
|
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
192 for p in paths: |
|
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
193 f = os.path.join(p, executable) |
|
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
194 if os.path.isfile(f): |
|
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
195 # the file exists, we have a shot at spawn working |
|
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
196 return f |
|
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
197 return None |
|
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
198 else: |
|
e8b22bc88d95
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents:
12058
diff
changeset
|
199 return executable |
