Updated for 2.1b2 distribution.
[python/dscho.git] / Lib / idlelib / spawn.py
blob22617ed12924aa52b114d44b77116a7cc72a464a
1 # spawn - This is ugly, OS-specific code to spawn a separate process. It
2 # also defines a function for getting the version of a path most
3 # likely to work with cranky API functions.
5 import os
7 def hardpath(path):
8 path = os.path.normcase(os.path.abspath(path))
9 try:
10 import win32api
11 path = win32api.GetShortPathName( path )
12 except:
13 pass
14 return path
16 if hasattr(os, 'fork'):
18 # UNIX-ish operating system: we fork() and exec(), and we have to track
19 # the pids of our children and call waitpid() on them to avoid leaving
20 # zombies in the process table. kill_zombies() does the dirty work, and
21 # should be called periodically.
23 zombies = []
25 def spawn(bin, *args):
26 pid = os.fork()
27 if pid:
28 zombies.append(pid)
29 else:
30 os.execv( bin, (bin, ) + args )
32 def kill_zombies():
33 for z in zombies[:]:
34 stat = os.waitpid(z, os.WNOHANG)
35 if stat[0]==z:
36 zombies.remove(z)
37 elif hasattr(os, 'spawnv'):
39 # Windows-ish OS: we use spawnv(), and stick quotes around arguments
40 # in case they contains spaces, since Windows will jam all the
41 # arguments to spawn() or exec() together into one string. The
42 # kill_zombies function is a noop.
44 def spawn(bin, *args):
45 nargs = [bin]
46 for arg in args:
47 nargs.append( '"'+arg+'"' )
48 os.spawnv( os.P_NOWAIT, bin, nargs )
50 def kill_zombies(): pass
52 else:
53 # If you get here, you may be able to write an alternative implementation
54 # of these functions for your OS.
56 def kill_zombies(): pass
58 raise OSError, 'This OS does not support fork() or spawnv().'