Mercurial > cpython-lru_cache-weakref
annotate Lib/macpath.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 | ca02d9278180 |
| children | 666152b77027 |
| rev | line source |
|---|---|
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
1 # module 'macpath' -- pathname (or -related) operations for the Macintosh |
| 103 | 2 |
|
579
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
3 import string |
| 103 | 4 import mac |
|
579
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
5 from stat import * |
| 103 | 6 |
|
579
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
7 |
|
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
8 # Normalize the case of a pathname. Dummy in Posix, but string.lower here. |
|
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
9 |
|
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
10 normcase = string.lower |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
11 |
| 103 | 12 |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
13 # Return true if a path is absolute. |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
14 # On the Mac, relative paths begin with a colon, |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
15 # but as a special case, paths with no colons at all are also relative. |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
16 # Anything else is absolute (the string up to the first colon is the |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
17 # volume name). |
| 103 | 18 |
| 19 def isabs(s): | |
| 20 return ':' in s and s[0] <> ':' | |
| 21 | |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
22 |
| 360 | 23 # Join two pathnames. |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
24 # The result is equivalent to what the second pathname would refer to |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
25 # if the first pathname were the current directory. |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
26 |
| 360 | 27 def join(s, t): |
| 103 | 28 if (not s) or isabs(t): return t |
| 559 | 29 if t[:1] == ':': t = t[1:] |
| 103 | 30 if ':' not in s: |
| 31 s = ':' + s | |
| 32 if s[-1:] <> ':': | |
| 33 s = s + ':' | |
| 34 return s + t | |
| 35 | |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
36 |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
37 # Split a pathname in two parts: the directory leading up to the final bit, |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
38 # and the basename (the filename, without colons, in that directory). |
| 360 | 39 # The result (s, t) is such that join(s, t) yields the original argument. |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
40 |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
41 def split(s): |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
42 if ':' not in s: return '', s |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
43 colon = 0 |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
44 for i in range(len(s)): |
| 559 | 45 if s[i] == ':': colon = i+1 |
|
1810
d892ac4b4fb9
* Lib/linecache.py: don't crash on empty filename
Guido van Rossum <guido@python.org>
parents:
1086
diff
changeset
|
46 return s[:colon-1], s[colon:] |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
47 |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
48 |
| 3793 | 49 # Split a path in root and extension. |
| 50 # The extension is everything starting at the last dot in the last | |
| 51 # pathname component; the root is everything before that. | |
| 52 # It is always true that root + ext == p. | |
| 53 | |
| 54 def splitext(p): | |
| 55 root, ext = '', '' | |
| 56 for c in p: | |
| 57 if c == ':': | |
| 58 root, ext = root + ext + c, '' | |
| 59 elif c == '.': | |
| 60 if ext: | |
| 61 root, ext = root + ext, c | |
| 62 else: | |
| 63 ext = c | |
| 64 elif ext: | |
| 65 ext = ext + c | |
| 66 else: | |
| 67 root = root + c | |
| 68 return root, ext | |
| 69 | |
| 70 | |
|
2947
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
71 # Split a pathname into a drive specification and the rest of the |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
72 # path. Useful on DOS/Windows/NT; on the Mac, the drive is always |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
73 # empty (don't use the volume name -- it doesn't have the same |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
74 # syntactic and semantic oddities as DOS drive letters, such as there |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
75 # being a separate current directory per drive). |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
76 |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
77 def splitdrive(p): |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
78 return '', p |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
79 |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
80 |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
81 # Short interfaces to split() |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
82 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
83 def dirname(s): return split(s)[0] |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
84 def basename(s): return split(s)[1] |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
85 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
86 |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
87 # Return true if the pathname refers to an existing directory. |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
88 |
| 103 | 89 def isdir(s): |
| 90 try: | |
| 91 st = mac.stat(s) | |
| 92 except mac.error: | |
| 93 return 0 | |
| 94 return S_ISDIR(st[ST_MODE]) | |
| 95 | |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
96 |
| 2260 | 97 # Return true if the pathname refers to a symbolic link. |
| 98 # (Always false on the Mac, until we understand Aliases.) | |
| 99 | |
| 100 def islink(s): | |
| 101 return 0 | |
| 102 | |
| 103 | |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
104 # Return true if the pathname refers to an existing regular file. |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
105 |
| 103 | 106 def isfile(s): |
| 107 try: | |
| 108 st = mac.stat(s) | |
| 109 except mac.error: | |
| 110 return 0 | |
| 111 return S_ISREG(st[ST_MODE]) | |
| 112 | |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
113 |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
114 # Return true if the pathname refers to an existing file or directory. |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
115 |
| 103 | 116 def exists(s): |
| 117 try: | |
| 118 st = mac.stat(s) | |
| 119 except mac.error: | |
| 120 return 0 | |
| 121 return 1 | |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
122 |
|
3299
47763ac44c48
Added (dummy) expanduser and expandvar methods
Jack Jansen <jack.jansen@cwi.nl>
parents:
2947
diff
changeset
|
123 # |
|
47763ac44c48
Added (dummy) expanduser and expandvar methods
Jack Jansen <jack.jansen@cwi.nl>
parents:
2947
diff
changeset
|
124 # dummy expandvars to retain interface-compatability with other |
|
47763ac44c48
Added (dummy) expanduser and expandvar methods
Jack Jansen <jack.jansen@cwi.nl>
parents:
2947
diff
changeset
|
125 # operating systems. |
|
47763ac44c48
Added (dummy) expanduser and expandvar methods
Jack Jansen <jack.jansen@cwi.nl>
parents:
2947
diff
changeset
|
126 def expandvars(path): |
|
47763ac44c48
Added (dummy) expanduser and expandvar methods
Jack Jansen <jack.jansen@cwi.nl>
parents:
2947
diff
changeset
|
127 return path |
|
47763ac44c48
Added (dummy) expanduser and expandvar methods
Jack Jansen <jack.jansen@cwi.nl>
parents:
2947
diff
changeset
|
128 |
|
47763ac44c48
Added (dummy) expanduser and expandvar methods
Jack Jansen <jack.jansen@cwi.nl>
parents:
2947
diff
changeset
|
129 # |
|
47763ac44c48
Added (dummy) expanduser and expandvar methods
Jack Jansen <jack.jansen@cwi.nl>
parents:
2947
diff
changeset
|
130 # dummy expanduser to retain interface-compatability with other |
|
47763ac44c48
Added (dummy) expanduser and expandvar methods
Jack Jansen <jack.jansen@cwi.nl>
parents:
2947
diff
changeset
|
131 # operating systems. |
|
47763ac44c48
Added (dummy) expanduser and expandvar methods
Jack Jansen <jack.jansen@cwi.nl>
parents:
2947
diff
changeset
|
132 def expanduser(path): |
|
47763ac44c48
Added (dummy) expanduser and expandvar methods
Jack Jansen <jack.jansen@cwi.nl>
parents:
2947
diff
changeset
|
133 return path |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
134 |
|
2947
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
135 # Normalize a pathname: get rid of '::' sequences by backing up, |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
136 # e.g., 'foo:bar::bletch' becomes 'foo:bletch'. |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
137 # Raise the exception norm_error below if backing up is impossible, |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
138 # e.g., for '::foo'. |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
139 # XXX The Unix version doesn't raise an exception but simply |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
140 # returns an unnormalized path. Should do so here too. |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
141 |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
142 norm_error = 'macpath.norm_error: path cannot be normalized' |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
143 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
144 def normpath(s): |
|
2947
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
145 import string |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
146 if ':' not in s: |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
147 return ':' + s |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
148 f = string.splitfields(s, ':') |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
149 pre = [] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
150 post = [] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
151 if not f[0]: |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
152 pre = f[:1] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
153 f = f[1:] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
154 if not f[len(f)-1]: |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
155 post = f[-1:] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
156 f = f[:-1] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
157 res = [] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
158 for seg in f: |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
159 if seg: |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
160 res.append(seg) |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
161 else: |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
162 if not res: raise norm_error, 'path starts with ::' |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
163 del res[len(res)-1] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
164 if not (pre or res): |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
165 raise norm_error, 'path starts with volume::' |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
166 if pre: res = pre + res |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
167 if post: res = res + post |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
168 s = res[0] |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
169 for seg in res[1:]: |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
170 s = s + ':' + seg |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
171 return s |
|
2910
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
172 |
|
2947
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
173 |
|
2910
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
174 # Directory tree walk. |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
175 # For each directory under top (including top itself), |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
176 # func(arg, dirname, filenames) is called, where |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
177 # dirname is the name of the directory and filenames is the list |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
178 # of files (and subdirectories etc.) in the directory. |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
179 # The func may modify the filenames list, to implement a filter, |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
180 # or to impose a different order of visiting. |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
181 |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
182 def walk(top, func, arg): |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
183 try: |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
184 names = mac.listdir(top) |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
185 except mac.error: |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
186 return |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
187 func(arg, top, names) |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
188 for name in names: |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
189 name = join(top, name) |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
190 if isdir(name): |
|
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
191 walk(name, func, arg) |
