Mercurial > cpython-lru_cache-weakref
annotate Lib/os.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 4f73962aaba6 |
| children | 24d668454cb0 |
| rev | line source |
|---|---|
| 1463 | 1 # os.py -- either mac, dos or posix depending on what system we're on. |
| 580 | 2 |
| 3 # This exports: | |
| 4 # - all functions from either posix or mac, e.g., os.unlink, os.stat, etc. | |
|
772
d945cf33a64f
Added definition for pardir ('..')
Guido van Rossum <guido@python.org>
parents:
580
diff
changeset
|
5 # - os.path is either module posixpath or macpath |
| 580 | 6 # - os.name is either 'posix' or 'mac' |
| 7 # - os.curdir is a string representing the current directory ('.' or ':') | |
|
772
d945cf33a64f
Added definition for pardir ('..')
Guido van Rossum <guido@python.org>
parents:
580
diff
changeset
|
8 # - os.pardir is a string representing the parent directory ('..' or '::') |
| 806 | 9 # - os.sep is the (or a most common) pathname separator ('/' or ':') |
| 1745 | 10 # - os.pathsep is the component separator used in $PATH etc |
| 11 # - os.defpath is the default search path for executables | |
| 580 | 12 |
| 13 # Programs that import and use 'os' stand a better chance of being | |
| 14 # portable between different platforms. Of course, they must then | |
| 15 # only use functions that are defined by all platforms (e.g., unlink | |
| 16 # and opendir), and leave all pathname manipulation to os.path | |
| 17 # (e.g., split and join). | |
| 18 | |
| 1745 | 19 _osindex = { |
| 20 'posix': ('.', '..', '/', ':', ':/bin:/usr/bin'), | |
| 21 'dos': ('.', '..', '\\', ';', '.;C:\\bin'), | |
| 22 'nt': ('.', '..', '\\', ';', '.;C:\\bin'), | |
|
2542
49df1576fb54
set mac path separator to newline
Guido van Rossum <guido@python.org>
parents:
2539
diff
changeset
|
23 'mac': (':', '::', ':', '\n', ':'), |
| 1745 | 24 } |
|
772
d945cf33a64f
Added definition for pardir ('..')
Guido van Rossum <guido@python.org>
parents:
580
diff
changeset
|
25 |
|
1785
dd167bfd7a08
do fake "import posix" for freeze.py script
Guido van Rossum <guido@python.org>
parents:
1745
diff
changeset
|
26 # For freeze.py script: |
|
dd167bfd7a08
do fake "import posix" for freeze.py script
Guido van Rossum <guido@python.org>
parents:
1745
diff
changeset
|
27 if 0: |
|
dd167bfd7a08
do fake "import posix" for freeze.py script
Guido van Rossum <guido@python.org>
parents:
1745
diff
changeset
|
28 import posix |
|
1932
5bcaaed902f5
Import posixpath for freeze script
Guido van Rossum <guido@python.org>
parents:
1785
diff
changeset
|
29 import posixpath |
|
1785
dd167bfd7a08
do fake "import posix" for freeze.py script
Guido van Rossum <guido@python.org>
parents:
1745
diff
changeset
|
30 |
| 1745 | 31 import sys |
| 32 for name in _osindex.keys(): | |
| 33 if name in sys.builtin_module_names: | |
| 34 curdir, pardir, sep, pathsep, defpath = _osindex[name] | |
| 35 exec 'from %s import *' % name | |
| 36 exec 'import %spath' % name | |
| 37 exec 'path = %spath' % name | |
| 38 exec 'del %spath' % name | |
| 39 try: | |
| 40 exec 'from %s import _exit' % name | |
| 41 except ImportError: | |
| 42 pass | |
|
2921
0c50ec8e8358
make sure os.environ exists (maybe empty)
Guido van Rossum <guido@python.org>
parents:
2542
diff
changeset
|
43 try: |
|
0c50ec8e8358
make sure os.environ exists (maybe empty)
Guido van Rossum <guido@python.org>
parents:
2542
diff
changeset
|
44 environ |
|
0c50ec8e8358
make sure os.environ exists (maybe empty)
Guido van Rossum <guido@python.org>
parents:
2542
diff
changeset
|
45 except: |
|
0c50ec8e8358
make sure os.environ exists (maybe empty)
Guido van Rossum <guido@python.org>
parents:
2542
diff
changeset
|
46 environ = {} # Make sure os.environ exists, at least |
| 1745 | 47 break |
| 48 else: | |
| 49 del name | |
| 50 raise ImportError, 'no os specific module found' | |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
51 |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
52 def execl(file, *args): |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
53 execv(file, args) |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
54 |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
55 def execle(file, *args): |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
56 env = args[-1] |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
57 execve(file, args[:-1], env) |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
58 |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
59 def execlp(file, *args): |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
60 execvp(file, args) |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
61 |
|
2539
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
62 def execlpe(file, *args): |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
63 env = args[-1] |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
64 execvpe(file, args[:-1], env) |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
65 |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
66 def execvp(file, args): |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
67 _execvpe(file, args) |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
68 |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
69 def execvpe(file, args, env): |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
70 _execvpe(file, args, env) |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
71 |
| 1745 | 72 _notfound = None |
|
2539
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
73 def _execvpe(file, args, env = None): |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
74 if env: |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
75 func = execve |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
76 argrest = (args, env) |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
77 else: |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
78 func = execv |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
79 argrest = (args,) |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
80 env = environ |
| 1745 | 81 global _notfound |
| 82 head, tail = path.split(file) | |
| 83 if head: | |
|
2539
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
84 apply(func, (file,) + argrest) |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
85 return |
|
2539
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
86 if env.has_key('PATH'): |
|
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
87 envpath = env['PATH'] |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
88 else: |
| 1745 | 89 envpath = defpath |
| 90 import string | |
| 91 PATH = string.splitfields(envpath, pathsep) | |
| 92 if not _notfound: | |
| 93 import tempfile | |
| 94 # Exec a file that is guaranteed not to exist | |
| 95 try: execv(tempfile.mktemp(), ()) | |
| 96 except error, _notfound: pass | |
| 97 exc, arg = error, _notfound | |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
98 for dir in PATH: |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
99 fullname = path.join(dir, file) |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
100 try: |
|
2539
534a97c400cc
add execvpe -- mix of execvp and execve
Guido van Rossum <guido@python.org>
parents:
1932
diff
changeset
|
101 apply(func, (fullname,) + argrest) |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
102 except error, (errno, msg): |
| 1745 | 103 if errno != arg[0]: |
|
1403
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
104 exc, arg = error, (errno, msg) |
|
6278933d02e3
* string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents:
1197
diff
changeset
|
105 raise exc, arg |
| 1745 | 106 |
| 107 # Provide listdir for Windows NT that doesn't have it built in | |
| 108 if name == 'nt': | |
| 109 try: | |
| 110 _tmp = listdir | |
| 111 del _tmp | |
| 112 except NameError: | |
| 113 def listdir(name): | |
| 114 if path.ismount(name): | |
| 115 list = ['.'] | |
| 116 else: | |
| 117 list = ['.', '..'] | |
| 118 f = popen('dir/l/b ' + name, 'r') | |
| 119 line = f.readline() | |
| 120 while line: | |
| 121 list.append(line[:-1]) | |
| 122 line = f.readline() | |
| 123 return list | |
|
3805
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
124 |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
125 |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
126 # Change environ to automatically call putenv() if it exists |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
127 try: |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
128 _putenv = putenv |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
129 except NameError: |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
130 _putenv = None |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
131 if _putenv: |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
132 import UserDict |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
133 |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
134 class _Environ(UserDict.UserDict): |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
135 def __init__(self, environ): |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
136 UserDict.UserDict.__init__(self) |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
137 self.data = environ |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
138 def __setitem__(self, key, item): |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
139 putenv(key, item) |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
140 self.data[key] = item |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
141 |
|
4f73962aaba6
Added hook to os.environ to call putenv(), if it exists.
Guido van Rossum <guido@python.org>
parents:
2921
diff
changeset
|
142 environ = _Environ(environ) |
