Mercurial > cpython-lru_cache-weakref
annotate Lib/macpath.py @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 7769c4d72806 |
| children |
| rev | line source |
|---|---|
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
1 """Pathname and path-related operations for the Macintosh.""" |
| 103 | 2 |
|
7525
88992696b9e8
Instead of 'import mac', use 'import os' -- this way, the path syntax
Guido van Rossum <guido@python.org>
parents:
5576
diff
changeset
|
3 import os |
|
579
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
4 from stat import * |
|
41057
98bbc93bd55b
Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents:
39689
diff
changeset
|
5 import genericpath |
|
39689
d0ed7dbaad56
- Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents:
35376
diff
changeset
|
6 from genericpath import * |
| 103 | 7 |
|
16198
32755b456369
a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents:
15543
diff
changeset
|
8 __all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
|
32755b456369
a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents:
15543
diff
changeset
|
9 "basename","dirname","commonprefix","getsize","getmtime", |
|
35376
e7ed0e6aed2f
Bug #1266283: lexists() is not exported from os.path
Georg Brandl <georg@python.org>
parents:
35308
diff
changeset
|
10 "getatime","getctime", "islink","exists","lexists","isdir","isfile", |
|
46770
8bfe2b06527d
Remove os.path.walk
Benjamin Peterson <benjamin@python.org>
parents:
43020
diff
changeset
|
11 "expanduser","expandvars","normpath","abspath", |
|
27897
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27507
diff
changeset
|
12 "curdir","pardir","sep","pathsep","defpath","altsep","extsep", |
|
32302
e196b823e6c5
Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents:
28098
diff
changeset
|
13 "devnull","realpath","supports_unicode_filenames"] |
|
579
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
14 |
|
27897
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27507
diff
changeset
|
15 # strings representing various path-related bits and pieces |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
16 # These are primarily for export; internally, they are hardcoded. |
|
27897
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27507
diff
changeset
|
17 curdir = ':' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27507
diff
changeset
|
18 pardir = '::' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27507
diff
changeset
|
19 extsep = '.' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27507
diff
changeset
|
20 sep = ':' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27507
diff
changeset
|
21 pathsep = '\n' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27507
diff
changeset
|
22 defpath = ':' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27507
diff
changeset
|
23 altsep = None |
|
32302
e196b823e6c5
Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents:
28098
diff
changeset
|
24 devnull = 'Dev:Null' |
|
27897
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27507
diff
changeset
|
25 |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
26 def _get_colon(path): |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
27 if isinstance(path, bytes): |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
28 return b':' |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
29 else: |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
30 return ':' |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
31 |
|
14910
6fb2af766892
Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents:
14128
diff
changeset
|
32 # Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here. |
|
579
6884346c04a0
Added 'normcase' function.
Guido van Rossum <guido@python.org>
parents:
559
diff
changeset
|
33 |
|
14910
6fb2af766892
Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents:
14128
diff
changeset
|
34 def normcase(path): |
|
62490
4e099e5004a1
#9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59814
diff
changeset
|
35 if not isinstance(path, (bytes, str)): |
|
4e099e5004a1
#9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59814
diff
changeset
|
36 raise TypeError("normcase() argument must be str or bytes, " |
|
4e099e5004a1
#9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
59814
diff
changeset
|
37 "not '{}'".format(path.__class__.__name__)) |
|
14910
6fb2af766892
Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents:
14128
diff
changeset
|
38 return path.lower() |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
39 |
| 103 | 40 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
41 def isabs(s): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
42 """Return true if a path is absolute. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
43 On the Mac, relative paths begin with a colon, |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
44 but as a special case, paths with no colons at all are also relative. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
45 Anything else is absolute (the string up to the first colon is the |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
46 volume name).""" |
| 103 | 47 |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
48 colon = _get_colon(s) |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
49 return colon in s and s[:1] != colon |
| 103 | 50 |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
51 |
|
5133
666152b77027
join(): join one or more path components
Barry Warsaw <barry@python.org>
parents:
3793
diff
changeset
|
52 def join(s, *p): |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
53 colon = _get_colon(s) |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
54 path = s |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
55 for t in p: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
56 if (not s) or isabs(t): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
57 path = t |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
58 continue |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
59 if t[:1] == colon: |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
60 t = t[1:] |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
61 if colon not in path: |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
62 path = colon + path |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
63 if path[-1:] != colon: |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
64 path = path + colon |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
65 path = path + t |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
66 return path |
| 103 | 67 |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
68 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
69 def split(s): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
70 """Split a pathname into two parts: the directory leading up to the final |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
71 bit, and the basename (the filename, without colons, in that directory). |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
72 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
|
73 |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
74 colon = _get_colon(s) |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
75 if colon not in s: return s[:0], s |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
76 col = 0 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
77 for i in range(len(s)): |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
78 if s[i:i+1] == colon: col = i + 1 |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
79 path, file = s[:col-1], s[col:] |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
80 if path and not colon in path: |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
81 path = path + colon |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
82 return path, file |
|
105
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
83 |
|
b4d5fdcbf83c
Added explanatory comments.
Guido van Rossum <guido@python.org>
parents:
103
diff
changeset
|
84 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
85 def splitext(p): |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
86 if isinstance(p, bytes): |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
87 return genericpath._splitext(p, b':', altsep, b'.') |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
88 else: |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
89 return genericpath._splitext(p, sep, altsep, extsep) |
|
41057
98bbc93bd55b
Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents:
39689
diff
changeset
|
90 splitext.__doc__ = genericpath._splitext.__doc__ |
| 3793 | 91 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
92 def splitdrive(p): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
93 """Split a pathname into a drive specification and the rest of the |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
94 path. Useful on DOS/Windows/NT; on the Mac, the drive is always |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
95 empty (don't use the volume name -- it doesn't have the same |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
96 syntactic and semantic oddities as DOS drive letters, such as there |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
97 being a separate current directory per drive).""" |
|
2947
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
98 |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
99 return p[:0], p |
|
2947
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
100 |
|
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
101 |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
102 # Short interfaces to split() |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
103 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
104 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
|
105 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
|
106 |
| 27257 | 107 def ismount(s): |
|
27507
d9d57f5f98cd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27257
diff
changeset
|
108 if not isabs(s): |
|
d9d57f5f98cd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27257
diff
changeset
|
109 return False |
|
d9d57f5f98cd
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
27257
diff
changeset
|
110 components = split(s) |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
111 return len(components) == 2 and not components[1] |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
112 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
113 def islink(s): |
|
23220
bd08c6955e9b
Fixes based on ideas from Christopher Smith:
Jack Jansen <jack.jansen@cwi.nl>
parents:
22867
diff
changeset
|
114 """Return true if the pathname refers to a symbolic link.""" |
| 2260 | 115 |
|
23220
bd08c6955e9b
Fixes based on ideas from Christopher Smith:
Jack Jansen <jack.jansen@cwi.nl>
parents:
22867
diff
changeset
|
116 try: |
| 28098 | 117 import Carbon.File |
| 118 return Carbon.File.ResolveAliasFile(s, 0)[2] | |
|
23220
bd08c6955e9b
Fixes based on ideas from Christopher Smith:
Jack Jansen <jack.jansen@cwi.nl>
parents:
22867
diff
changeset
|
119 except: |
|
bd08c6955e9b
Fixes based on ideas from Christopher Smith:
Jack Jansen <jack.jansen@cwi.nl>
parents:
22867
diff
changeset
|
120 return False |
| 2260 | 121 |
|
33332
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
32302
diff
changeset
|
122 # Is `stat`/`lstat` a meaningful difference on the Mac? This is safe in any |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
32302
diff
changeset
|
123 # case. |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
32302
diff
changeset
|
124 |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
32302
diff
changeset
|
125 def lexists(path): |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
32302
diff
changeset
|
126 """Test whether a path exists. Returns True for broken symbolic links""" |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
32302
diff
changeset
|
127 |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
32302
diff
changeset
|
128 try: |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
32302
diff
changeset
|
129 st = os.lstat(path) |
|
80932
47f98a550d42
Issue #16706: get rid of os.error
Andrew Svetlov <andrew.svetlov@gmail.com>
parents:
68257
diff
changeset
|
130 except OSError: |
|
33332
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
32302
diff
changeset
|
131 return False |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
32302
diff
changeset
|
132 return True |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
32302
diff
changeset
|
133 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
134 def expandvars(path): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
135 """Dummy to retain interface-compatibility with other operating systems.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
136 return path |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
137 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
138 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
139 def expanduser(path): |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
140 """Dummy to retain interface-compatibility with other operating systems.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
141 return path |
|
2947
a980d076052d
added normpath() and splitdrive()
Guido van Rossum <guido@python.org>
parents:
2910
diff
changeset
|
142 |
|
22704
18dccdf2c46d
Use docstrings for exception classes
Neal Norwitz <nnorwitz@gmail.com>
parents:
22702
diff
changeset
|
143 class norm_error(Exception): |
|
18dccdf2c46d
Use docstrings for exception classes
Neal Norwitz <nnorwitz@gmail.com>
parents:
22702
diff
changeset
|
144 """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
|
145 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
770
diff
changeset
|
146 def normpath(s): |
|
13876
1c00fea9ae40
Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents:
13577
diff
changeset
|
147 """Normalize a pathname. Will return the same result for |
|
1c00fea9ae40
Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents:
13577
diff
changeset
|
148 equivalent paths.""" |
|
1c00fea9ae40
Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents:
13577
diff
changeset
|
149 |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
150 colon = _get_colon(s) |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
151 |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
152 if colon not in s: |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
153 return colon + s |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
154 |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
155 comps = s.split(colon) |
|
13876
1c00fea9ae40
Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents:
13577
diff
changeset
|
156 i = 1 |
|
1c00fea9ae40
Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents:
13577
diff
changeset
|
157 while i < len(comps)-1: |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
158 if not comps[i] and comps[i-1]: |
|
13876
1c00fea9ae40
Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents:
13577
diff
changeset
|
159 if i > 1: |
|
1c00fea9ae40
Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents:
13577
diff
changeset
|
160 del comps[i-1:i+1] |
|
14910
6fb2af766892
Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents:
14128
diff
changeset
|
161 i = i - 1 |
|
13876
1c00fea9ae40
Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents:
13577
diff
changeset
|
162 else: |
|
1c00fea9ae40
Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents:
13577
diff
changeset
|
163 # best way to handle this is to raise an exception |
|
43020
907a45df1295
Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents:
41057
diff
changeset
|
164 raise norm_error('Cannot use :: immediately after volume name') |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
165 else: |
|
13876
1c00fea9ae40
Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents:
13577
diff
changeset
|
166 i = i + 1 |
|
1c00fea9ae40
Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents:
13577
diff
changeset
|
167 |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
168 s = colon.join(comps) |
|
13876
1c00fea9ae40
Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents:
13577
diff
changeset
|
169 |
|
1c00fea9ae40
Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents:
13577
diff
changeset
|
170 # remove trailing ":" except for ":" and "Volume:" |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
171 if s[-1:] == colon and len(comps) > 2 and s != colon*len(s): |
|
13876
1c00fea9ae40
Rewrite of normpath() by Corran Webster, so trailing :s are removed
Jack Jansen <jack.jansen@cwi.nl>
parents:
13577
diff
changeset
|
172 s = s[:-1] |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
173 return s |
|
2910
5a2612fb6c0e
Added missing walk() function
Jack Jansen <jack.jansen@cwi.nl>
parents:
2260
diff
changeset
|
174 |
| 10018 | 175 def abspath(path): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11137
diff
changeset
|
176 """Return an absolute path.""" |
| 10018 | 177 if not isabs(path): |
|
60063
e63d4678fca7
Note: only the relevant parts of r79474 are merged.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
49414
diff
changeset
|
178 if isinstance(path, bytes): |
|
e63d4678fca7
Note: only the relevant parts of r79474 are merged.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
49414
diff
changeset
|
179 cwd = os.getcwdb() |
|
e63d4678fca7
Note: only the relevant parts of r79474 are merged.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
49414
diff
changeset
|
180 else: |
|
e63d4678fca7
Note: only the relevant parts of r79474 are merged.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
49414
diff
changeset
|
181 cwd = os.getcwd() |
|
e63d4678fca7
Note: only the relevant parts of r79474 are merged.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
49414
diff
changeset
|
182 path = join(cwd, path) |
| 10018 | 183 return normpath(path) |
|
19934
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
19080
diff
changeset
|
184 |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
19080
diff
changeset
|
185 # realpath is a no-op on systems without islink support |
|
23220
bd08c6955e9b
Fixes based on ideas from Christopher Smith:
Jack Jansen <jack.jansen@cwi.nl>
parents:
22867
diff
changeset
|
186 def realpath(path): |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23237
diff
changeset
|
187 path = abspath(path) |
|
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23237
diff
changeset
|
188 try: |
| 28098 | 189 import Carbon.File |
|
84442
7769c4d72806
Issue #18200: Back out usage of ModuleNotFoundError (8d28d44f3a9a)
Brett Cannon <brett@python.org>
parents:
84114
diff
changeset
|
190 except ImportError: |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23237
diff
changeset
|
191 return path |
|
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23237
diff
changeset
|
192 if not path: |
|
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23237
diff
changeset
|
193 return path |
|
49414
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
194 colon = _get_colon(path) |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
195 components = path.split(colon) |
|
16a194da77de
Issue3187 for Macintosh platform:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46770
diff
changeset
|
196 path = components[0] + colon |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23237
diff
changeset
|
197 for c in components[1:]: |
|
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23237
diff
changeset
|
198 path = join(path, c) |
|
60063
e63d4678fca7
Note: only the relevant parts of r79474 are merged.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
49414
diff
changeset
|
199 try: |
|
e63d4678fca7
Note: only the relevant parts of r79474 are merged.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
49414
diff
changeset
|
200 path = Carbon.File.FSResolveAliasFile(path, 1)[0].as_pathname() |
|
e63d4678fca7
Note: only the relevant parts of r79474 are merged.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
49414
diff
changeset
|
201 except Carbon.File.Error: |
|
e63d4678fca7
Note: only the relevant parts of r79474 are merged.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
49414
diff
changeset
|
202 pass |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23237
diff
changeset
|
203 return path |
|
25666
4a825b902310
Add os.path.supports_unicode_filenames for all platforms,
Mark Hammond <mhammond@skippinet.com.au>
parents:
23625
diff
changeset
|
204 |
|
64718
557355e46ba6
Issue #767645: Set os.path.supports_unicode_filenames to True on Mac OS X
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62490
diff
changeset
|
205 supports_unicode_filenames = True |
