Mercurial > cpython-lru_cache-weakref
annotate Lib/posixpath.py @ 79691:05c8928362ee
Merge __next__ method link fixes with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 12 Oct 2012 13:46:37 +0300 |
| parents | 30d4d1528b58 |
| children | bfe9526606e2 47f98a550d42 |
| rev | line source |
|---|---|
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
1 """Common operations on Posix pathnames. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
2 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
3 Instead of importing this module directly, import os and refer to |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
4 this module as os.path. The "os.path" name is an alias for this |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
5 module on Posix systems; on other systems (e.g. Mac, Windows), |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
6 os.path provides the same operations in a manner specific to that |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
7 platform, and is an alias to another module (e.g. macpath, ntpath). |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
8 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
9 Some of this can actually be useful on non-Posix systems too, e.g. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
10 for manipulation of the pathname component of URLs. |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
11 """ |
| 5 | 12 |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
13 import os |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
14 import sys |
|
21
961696ef3ab7
Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
15 import stat |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
16 import genericpath |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
35377
diff
changeset
|
17 from genericpath import * |
| 5 | 18 |
|
16562
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
16535
diff
changeset
|
19 __all__ = ["normcase","isabs","join","splitdrive","split","splitext", |
|
623456576d6a
__all__ for several more modules
Skip Montanaro <skip@pobox.com>
parents:
16535
diff
changeset
|
20 "basename","dirname","commonprefix","getsize","getmtime", |
|
35377
448c6fbff641
backport bug #1266283: add lexists to os.path.__all__
Georg Brandl <georg@python.org>
parents:
35007
diff
changeset
|
21 "getatime","getctime","islink","exists","lexists","isdir","isfile", |
|
46770
8bfe2b06527d
Remove os.path.walk
Benjamin Peterson <benjamin@python.org>
parents:
44535
diff
changeset
|
22 "ismount", "expanduser","expandvars","normpath","abspath", |
|
27056
94b9cf4274de
Fix SF #659228, 'realpath' function missing from os.path
Neal Norwitz <nnorwitz@gmail.com>
parents:
26946
diff
changeset
|
23 "samefile","sameopenfile","samestat", |
|
27897
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
24 "curdir","pardir","sep","pathsep","defpath","altsep","extsep", |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
25 "devnull","realpath","supports_unicode_filenames","relpath"] |
| 5 | 26 |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
27 # Strings representing various path-related bits and pieces. |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
28 # 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:
27056
diff
changeset
|
29 curdir = '.' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
30 pardir = '..' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
31 extsep = '.' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
32 sep = '/' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
33 pathsep = ':' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
34 defpath = ':/bin:/usr/bin' |
|
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
35 altsep = None |
|
32302
e196b823e6c5
Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents:
32077
diff
changeset
|
36 devnull = '/dev/null' |
|
27897
cf1efca82b18
Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents:
27056
diff
changeset
|
37 |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
38 def _get_sep(path): |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
39 if isinstance(path, bytes): |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
40 return b'/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
41 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
42 return '/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
43 |
| 582 | 44 # Normalize the case of a pathname. Trivial in Posix, string.lower on Mac. |
| 45 # On MS-DOS this may also turn slashes into backslashes; however, other | |
| 46 # normalizations (such as optimizing '../' away) are not allowed | |
| 47 # (another function should be defined to do that). | |
| 48 | |
| 49 def normcase(s): | |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
50 """Normalize case of pathname. Has no effect under Posix""" |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
51 # TODO: on Mac OS X, this should really return s.lower(). |
|
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:
59613
diff
changeset
|
52 if not isinstance(s, (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:
59613
diff
changeset
|
53 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:
59613
diff
changeset
|
54 "not '{}'".format(s.__class__.__name__)) |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
55 return s |
| 582 | 56 |
| 57 | |
|
13050
a195962e389c
typos fixed by Rob Hooft
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11881
diff
changeset
|
58 # Return whether a path is absolute. |
| 582 | 59 # Trivial in Posix, harder on the Mac or MS-DOS. |
| 60 | |
| 61 def isabs(s): | |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
62 """Test whether a path is absolute""" |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
63 sep = _get_sep(s) |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
64 return s.startswith(sep) |
| 582 | 65 |
| 66 | |
|
5133
666152b77027
join(): join one or more path components
Barry Warsaw <barry@python.org>
parents:
4074
diff
changeset
|
67 # Join pathnames. |
|
666152b77027
join(): join one or more path components
Barry Warsaw <barry@python.org>
parents:
4074
diff
changeset
|
68 # Ignore the previous parts if a part is absolute. |
| 361 | 69 # Insert a '/' unless the first part is empty or already ends in '/'. |
| 582 | 70 |
|
5133
666152b77027
join(): join one or more path components
Barry Warsaw <barry@python.org>
parents:
4074
diff
changeset
|
71 def join(a, *p): |
|
42775
d7517c306791
Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
72 """Join two or more pathname components, inserting '/' as needed. |
|
d7517c306791
Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
73 If any component is an absolute path, all previous path components |
|
78223
61d0e3526a51
#15342: Add clarifying sentence to posixpath.join docstring.
R David Murray <rdmurray@bitdance.com>
parents:
78150
diff
changeset
|
74 will be discarded. An empty last part will result in a path that |
|
61d0e3526a51
#15342: Add clarifying sentence to posixpath.join docstring.
R David Murray <rdmurray@bitdance.com>
parents:
78150
diff
changeset
|
75 ends with a separator.""" |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
76 sep = _get_sep(a) |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
77 path = a |
|
78118
cf1ac0c9e753
#15180: Clarify posixpath.join() error message when mixing str & bytes
Hynek Schlawack <hs@ox.cx>
parents:
76852
diff
changeset
|
78 try: |
|
cf1ac0c9e753
#15180: Clarify posixpath.join() error message when mixing str & bytes
Hynek Schlawack <hs@ox.cx>
parents:
76852
diff
changeset
|
79 for b in p: |
|
cf1ac0c9e753
#15180: Clarify posixpath.join() error message when mixing str & bytes
Hynek Schlawack <hs@ox.cx>
parents:
76852
diff
changeset
|
80 if b.startswith(sep): |
|
cf1ac0c9e753
#15180: Clarify posixpath.join() error message when mixing str & bytes
Hynek Schlawack <hs@ox.cx>
parents:
76852
diff
changeset
|
81 path = b |
|
cf1ac0c9e753
#15180: Clarify posixpath.join() error message when mixing str & bytes
Hynek Schlawack <hs@ox.cx>
parents:
76852
diff
changeset
|
82 elif not path or path.endswith(sep): |
|
cf1ac0c9e753
#15180: Clarify posixpath.join() error message when mixing str & bytes
Hynek Schlawack <hs@ox.cx>
parents:
76852
diff
changeset
|
83 path += b |
|
cf1ac0c9e753
#15180: Clarify posixpath.join() error message when mixing str & bytes
Hynek Schlawack <hs@ox.cx>
parents:
76852
diff
changeset
|
84 else: |
|
cf1ac0c9e753
#15180: Clarify posixpath.join() error message when mixing str & bytes
Hynek Schlawack <hs@ox.cx>
parents:
76852
diff
changeset
|
85 path += sep + b |
|
cf1ac0c9e753
#15180: Clarify posixpath.join() error message when mixing str & bytes
Hynek Schlawack <hs@ox.cx>
parents:
76852
diff
changeset
|
86 except TypeError: |
|
78150
5553a53a230a
#15377: Make posixpath.join() more strict when checking for str/bytes mix
Hynek Schlawack <hs@ox.cx>
parents:
78118
diff
changeset
|
87 valid_types = all(isinstance(s, (str, bytes, bytearray)) |
|
5553a53a230a
#15377: Make posixpath.join() more strict when checking for str/bytes mix
Hynek Schlawack <hs@ox.cx>
parents:
78118
diff
changeset
|
88 for s in (a, ) + p) |
|
5553a53a230a
#15377: Make posixpath.join() more strict when checking for str/bytes mix
Hynek Schlawack <hs@ox.cx>
parents:
78118
diff
changeset
|
89 if valid_types: |
|
5553a53a230a
#15377: Make posixpath.join() more strict when checking for str/bytes mix
Hynek Schlawack <hs@ox.cx>
parents:
78118
diff
changeset
|
90 # Must have a mixture of text and binary data |
|
78119
1462b963e5ce
#15180: Clarify posixpath.join() error message when mixing str & bytes
Hynek Schlawack <hs@ox.cx>
parents:
78118
diff
changeset
|
91 raise TypeError("Can't mix strings and bytes in path " |
|
1462b963e5ce
#15180: Clarify posixpath.join() error message when mixing str & bytes
Hynek Schlawack <hs@ox.cx>
parents:
78118
diff
changeset
|
92 "components.") from None |
|
78150
5553a53a230a
#15377: Make posixpath.join() more strict when checking for str/bytes mix
Hynek Schlawack <hs@ox.cx>
parents:
78118
diff
changeset
|
93 raise |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
94 return path |
| 5 | 95 |
| 96 | |
|
769
f8421da4efa7
Renamed to posixpath; changed def'n of split().
Guido van Rossum <guido@python.org>
parents:
582
diff
changeset
|
97 # Split a path in head (everything up to the last '/') and tail (the |
|
3064
df7dfac30c3e
rationalized os.path.split() so split "/a/" yields "/a", ""
Guido van Rossum <guido@python.org>
parents:
2925
diff
changeset
|
98 # rest). If the path ends in '/', tail will be empty. If there is no |
|
df7dfac30c3e
rationalized os.path.split() so split "/a/" yields "/a", ""
Guido van Rossum <guido@python.org>
parents:
2925
diff
changeset
|
99 # '/' in the path, head will be empty. |
|
df7dfac30c3e
rationalized os.path.split() so split "/a/" yields "/a", ""
Guido van Rossum <guido@python.org>
parents:
2925
diff
changeset
|
100 # Trailing '/'es are stripped from head unless it is the root. |
| 582 | 101 |
| 5 | 102 def split(p): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
103 """Split a pathname. Returns tuple "(head, tail)" where "tail" is |
| 14909 | 104 everything after the final slash. Either part may be empty.""" |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
105 sep = _get_sep(p) |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
106 i = p.rfind(sep) + 1 |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
107 head, tail = p[:i], p[i:] |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
108 if head and head != sep*len(head): |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
109 head = head.rstrip(sep) |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
110 return head, tail |
| 5 | 111 |
| 112 | |
| 361 | 113 # Split a path in root and extension. |
|
4074
7b6eefac6e49
Correct description of splitext().
Guido van Rossum <guido@python.org>
parents:
3795
diff
changeset
|
114 # The extension is everything starting at the last dot in the last |
| 361 | 115 # pathname component; the root is everything before that. |
| 582 | 116 # It is always true that root + ext == p. |
| 117 | |
| 361 | 118 def splitext(p): |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
119 if isinstance(p, bytes): |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
120 sep = b'/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
121 extsep = b'.' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
122 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
123 sep = '/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
124 extsep = '.' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
125 return genericpath._splitext(p, sep, None, extsep) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
126 splitext.__doc__ = genericpath._splitext.__doc__ |
| 361 | 127 |
| 2925 | 128 # Split a pathname into a drive specification and the rest of the |
| 129 # path. Useful on DOS/Windows/NT; on Unix, the drive is always empty. | |
| 130 | |
| 131 def splitdrive(p): | |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
132 """Split a pathname into drive and path. On Posix, drive is always |
| 14909 | 133 empty.""" |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
134 return p[:0], p |
| 2925 | 135 |
| 136 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
35377
diff
changeset
|
137 # Return the tail (basename) part of a path, same as split(path)[1]. |
| 582 | 138 |
| 5 | 139 def basename(p): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
140 """Returns the final component of a pathname""" |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
141 sep = _get_sep(p) |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
142 i = p.rfind(sep) + 1 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
35377
diff
changeset
|
143 return p[i:] |
| 5 | 144 |
| 145 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
35377
diff
changeset
|
146 # Return the head (dirname) part of a path, same as split(path)[0]. |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
147 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
148 def dirname(p): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
149 """Returns the directory component of a pathname""" |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
150 sep = _get_sep(p) |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
151 i = p.rfind(sep) + 1 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
35377
diff
changeset
|
152 head = p[:i] |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
153 if head and head != sep*len(head): |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
154 head = head.rstrip(sep) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
35377
diff
changeset
|
155 return head |
| 5 | 156 |
|
8746
ca1b906149e6
Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents:
6757
diff
changeset
|
157 |
| 582 | 158 # Is a path a symbolic link? |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
159 # This will always return false on systems where os.lstat doesn't exist. |
| 582 | 160 |
| 161 def islink(path): | |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
162 """Test whether a path is a symbolic link""" |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
163 try: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
164 st = os.lstat(path) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
165 except (os.error, AttributeError): |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
166 return False |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
167 return stat.S_ISLNK(st.st_mode) |
| 582 | 168 |
|
33332
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
169 # Being true for dangling symbolic links is also useful. |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
170 |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
171 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:
33204
diff
changeset
|
172 """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:
33204
diff
changeset
|
173 try: |
|
59613
77c71f258df7
Merged revisions 78018,78035-78040,78042-78043,78046,78048-78052,78054,78059,78075-78080 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53072
diff
changeset
|
174 os.lstat(path) |
|
33332
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
175 except os.error: |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
176 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:
33204
diff
changeset
|
177 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:
33204
diff
changeset
|
178 |
|
062177de8685
Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents:
33204
diff
changeset
|
179 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
180 # Are two filenames really pointing to the same file? |
| 582 | 181 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
182 def samefile(f1, f2): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
183 """Test whether two pathnames reference the same actual file""" |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
184 s1 = os.stat(f1) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
185 s2 = os.stat(f2) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
186 return samestat(s1, s2) |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
187 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
188 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
189 # Are two open files really referencing the same file? |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
190 # (Not necessarily the same file descriptor!) |
| 582 | 191 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
192 def sameopenfile(fp1, fp2): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
193 """Test whether two open file objects reference the same file""" |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
194 s1 = os.fstat(fp1) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
195 s2 = os.fstat(fp2) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
196 return samestat(s1, s2) |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
197 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
198 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
199 # Are two stat buffers (obtained from stat, fstat or lstat) |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
200 # describing the same file? |
| 582 | 201 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
202 def samestat(s1, s2): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
203 """Test whether two stat buffers reference the same file""" |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
204 return s1.st_ino == s2.st_ino and \ |
|
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
205 s1.st_dev == s2.st_dev |
| 5 | 206 |
| 207 | |
| 208 # Is a path a mount point? | |
|
3795
1f20f51b700e
Renamed all occurrences of posix to os.
Guido van Rossum <guido@python.org>
parents:
3483
diff
changeset
|
209 # (Does this work for all UNIXes? Is it even guaranteed to work by Posix?) |
| 582 | 210 |
| 5 | 211 def ismount(path): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
212 """Test whether a path is a mount point""" |
|
63438
9ee196a7f9a2
Merged revisions 83393,83396,83398,83404-83405,83408 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53072
diff
changeset
|
213 if islink(path): |
|
9ee196a7f9a2
Merged revisions 83393,83396,83398,83404-83405,83408 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53072
diff
changeset
|
214 # A symlink can never be a mount point |
|
9ee196a7f9a2
Merged revisions 83393,83396,83398,83404-83405,83408 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53072
diff
changeset
|
215 return False |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
216 try: |
|
44535
420fe6d7d5f4
Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
42775
diff
changeset
|
217 s1 = os.lstat(path) |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
218 if isinstance(path, bytes): |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
219 parent = join(path, b'..') |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
220 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
221 parent = join(path, '..') |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
222 s2 = os.lstat(parent) |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
223 except os.error: |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20361
diff
changeset
|
224 return False # It doesn't exist -- so not a mount point :-) |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
225 dev1 = s1.st_dev |
|
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
226 dev2 = s2.st_dev |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
227 if dev1 != dev2: |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20361
diff
changeset
|
228 return True # path/.. on a different device as path |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
229 ino1 = s1.st_ino |
|
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
230 ino2 = s2.st_ino |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
231 if ino1 == ino2: |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20361
diff
changeset
|
232 return True # path/.. is the same i-node as path |
|
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
20361
diff
changeset
|
233 return False |
| 5 | 234 |
| 235 | |
| 582 | 236 # Expand paths beginning with '~' or '~user'. |
| 237 # '~' means $HOME; '~user' means that user's home directory. | |
| 238 # If the path doesn't begin with '~', or if the user or $HOME is unknown, | |
| 239 # the path is returned unchanged (leaving error reporting to whatever | |
| 240 # function is called with the expanded path as argument). | |
| 241 # See also module 'glob' for expansion of *, ? and [...] in pathnames. | |
| 242 # (A function should also be defined to do full *sh-style environment | |
| 243 # variable expansion.) | |
| 244 | |
| 245 def expanduser(path): | |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
246 """Expand ~ and ~user constructions. If user or $HOME is unknown, |
| 14909 | 247 do nothing.""" |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
248 if isinstance(path, bytes): |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
249 tilde = b'~' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
250 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
251 tilde = '~' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
252 if not path.startswith(tilde): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
253 return path |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
254 sep = _get_sep(path) |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
255 i = path.find(sep, 1) |
|
29540
c3bc7ed9d315
Use find() instead of looping over the string in expanduser().
Walter Dörwald <walter@livinglogic.de>
parents:
29512
diff
changeset
|
256 if i < 0: |
|
c3bc7ed9d315
Use find() instead of looping over the string in expanduser().
Walter Dörwald <walter@livinglogic.de>
parents:
29512
diff
changeset
|
257 i = len(path) |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
258 if i == 1: |
|
29540
c3bc7ed9d315
Use find() instead of looping over the string in expanduser().
Walter Dörwald <walter@livinglogic.de>
parents:
29512
diff
changeset
|
259 if 'HOME' not in os.environ: |
|
25126
56f9f5ab6ec1
SF # 555779, import user doesn't work with CGIs
Neal Norwitz <nnorwitz@gmail.com>
parents:
23709
diff
changeset
|
260 import pwd |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
261 userhome = pwd.getpwuid(os.getuid()).pw_dir |
|
25126
56f9f5ab6ec1
SF # 555779, import user doesn't work with CGIs
Neal Norwitz <nnorwitz@gmail.com>
parents:
23709
diff
changeset
|
262 else: |
|
56f9f5ab6ec1
SF # 555779, import user doesn't work with CGIs
Neal Norwitz <nnorwitz@gmail.com>
parents:
23709
diff
changeset
|
263 userhome = os.environ['HOME'] |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
264 else: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
265 import pwd |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
266 name = path[1:i] |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
267 if isinstance(name, bytes): |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
268 name = str(name, 'ASCII') |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
269 try: |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
270 pwent = pwd.getpwnam(name) |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
271 except KeyError: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
272 return path |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
273 userhome = pwent.pw_dir |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
274 if isinstance(path, bytes): |
|
65022
1add8e5f3198
Use os.fsencode() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64796
diff
changeset
|
275 userhome = os.fsencode(userhome) |
|
53072
7e978ca42930
Merged revisions 70980,71059,71225,71234,71241,71243,71249,71251,71255,71266,71299,71329,71397-71398,71486 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
51194
diff
changeset
|
276 root = b'/' |
|
7e978ca42930
Merged revisions 70980,71059,71225,71234,71241,71243,71249,71251,71255,71266,71299,71329,71397-71398,71486 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
51194
diff
changeset
|
277 else: |
|
7e978ca42930
Merged revisions 70980,71059,71225,71234,71241,71243,71249,71251,71255,71266,71299,71329,71397-71398,71486 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
51194
diff
changeset
|
278 root = '/' |
|
76852
e472481b6d73
Closes #14768: os.path.expanduser('~/a') doesn't works correctly when HOME is '/'
Jesus Cea <jcea@jcea.es>
parents:
68257
diff
changeset
|
279 userhome = userhome.rstrip(root) |
|
e472481b6d73
Closes #14768: os.path.expanduser('~/a') doesn't works correctly when HOME is '/'
Jesus Cea <jcea@jcea.es>
parents:
68257
diff
changeset
|
280 return (userhome + path[i:]) or root |
|
932
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
281 |
|
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
282 |
|
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
283 # Expand paths containing shell variable substitutions. |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
284 # This expands the forms $variable and ${variable} only. |
|
13050
a195962e389c
typos fixed by Rob Hooft
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11881
diff
changeset
|
285 # Non-existent variables are left unchanged. |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
286 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
287 _varprog = None |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
288 _varprogb = None |
|
932
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
289 |
|
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
290 def expandvars(path): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
291 """Expand shell variables of form $var and ${var}. Unknown variables |
| 14909 | 292 are left unchanged.""" |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
293 global _varprog, _varprogb |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
294 if isinstance(path, bytes): |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
295 if b'$' not in path: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
296 return path |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
297 if not _varprogb: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
298 import re |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
299 _varprogb = re.compile(br'\$(\w+|\{[^}]*\})', re.ASCII) |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
300 search = _varprogb.search |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
301 start = b'{' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
302 end = b'}' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
303 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
304 if '$' not in path: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
305 return path |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
306 if not _varprog: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
307 import re |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
308 _varprog = re.compile(r'\$(\w+|\{[^}]*\})', re.ASCII) |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
309 search = _varprog.search |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
310 start = '{' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
311 end = '}' |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
312 i = 0 |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
313 while True: |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
314 m = search(path, i) |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
315 if not m: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
316 break |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
317 i, j = m.span(0) |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
318 name = m.group(1) |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
319 if name.startswith(start) and name.endswith(end): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
320 name = name[1:-1] |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
321 if isinstance(name, bytes): |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
322 name = str(name, 'ASCII') |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
22867
diff
changeset
|
323 if name in os.environ: |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
324 tail = path[j:] |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
325 value = os.environ[name] |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
326 if isinstance(path, bytes): |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
327 value = value.encode('ASCII') |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
328 path = path[:i] + value |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
329 i = len(path) |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
330 path += tail |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
331 else: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
332 i = j |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
333 return path |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
334 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
335 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
336 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B. |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
337 # It should be understood that this may change the meaning of the path |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
338 # if it contains symbolic links! |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
339 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
340 def normpath(path): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
341 """Normalize path, eliminating double slashes, etc.""" |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
342 if isinstance(path, bytes): |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
343 sep = b'/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
344 empty = b'' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
345 dot = b'.' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
346 dotdot = b'..' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
347 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
348 sep = '/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
349 empty = '' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
350 dot = '.' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
351 dotdot = '..' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
352 if path == empty: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
353 return dot |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
354 initial_slashes = path.startswith(sep) |
|
16333
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
355 # POSIX allows one or two initial slashes, but treats three or more |
|
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
356 # as single slash. |
|
16535
084b3dc9b6ff
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16333
diff
changeset
|
357 if (initial_slashes and |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
358 path.startswith(sep*2) and not path.startswith(sep*3)): |
|
16333
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
359 initial_slashes = 2 |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
360 comps = path.split(sep) |
|
13646
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
361 new_comps = [] |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
362 for comp in comps: |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
363 if comp in (empty, dot): |
|
13646
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
364 continue |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
365 if (comp != dotdot or (not initial_slashes and not new_comps) or |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
366 (new_comps and new_comps[-1] == dotdot)): |
|
13646
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
367 new_comps.append(comp) |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
368 elif new_comps: |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
369 new_comps.pop() |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
370 comps = new_comps |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
371 path = sep.join(comps) |
|
16333
01d5b80a3c03
Fixed posixpath.normpath() to respect two leading slashes, but
Marc-André Lemburg <mal@egenix.com>
parents:
15870
diff
changeset
|
372 if initial_slashes: |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
373 path = sep*initial_slashes + path |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
374 return path or dot |
| 10018 | 375 |
| 376 | |
| 377 def abspath(path): | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
378 """Return an absolute path.""" |
| 10018 | 379 if not isabs(path): |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
380 if isinstance(path, bytes): |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
381 cwd = os.getcwdb() |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
382 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
383 cwd = os.getcwd() |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
384 path = join(cwd, path) |
| 10018 | 385 return normpath(path) |
|
19934
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
386 |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
387 |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
388 # Return a canonical path (i.e. the absolute location of a file on the |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
389 # filesystem). |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
390 |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
391 def realpath(filename): |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
392 """Return the canonical path of the specified filename, eliminating any |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
393 symbolic links encountered in the path.""" |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
394 if isinstance(filename, bytes): |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
395 sep = b'/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
396 empty = b'' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
397 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
398 sep = '/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
399 empty = '' |
|
33129
ae9b47a8d648
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents:
32901
diff
changeset
|
400 if isabs(filename): |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
401 bits = [sep] + filename.split(sep)[1:] |
|
33129
ae9b47a8d648
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents:
32901
diff
changeset
|
402 else: |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
403 bits = [empty] + filename.split(sep) |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33129
diff
changeset
|
404 |
|
19934
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
405 for i in range(2, len(bits)+1): |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
406 component = join(*bits[0:i]) |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
407 # Resolve symbolic links. |
|
32621
6aba6d075899
Remove tabs introduced in last commit.
Brett Cannon <bcannon@gmail.com>
parents:
32607
diff
changeset
|
408 if islink(component): |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
409 resolved = _resolve_link(component) |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
410 if resolved is None: |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
411 # Infinite loop -- return original component + rest of the path |
|
33129
ae9b47a8d648
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents:
32901
diff
changeset
|
412 return abspath(join(*([component] + bits[i:]))) |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
413 else: |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
414 newpath = join(*([resolved] + bits[i:])) |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33129
diff
changeset
|
415 return realpath(newpath) |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19934
diff
changeset
|
416 |
|
33129
ae9b47a8d648
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents:
32901
diff
changeset
|
417 return abspath(filename) |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33129
diff
changeset
|
418 |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
419 |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
420 def _resolve_link(path): |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
421 """Internal helper function. Takes a path and follows symlinks |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
32621
diff
changeset
|
422 until we either arrive at something that isn't a symlink, or |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
423 encounter a path we've seen before (meaning that there's a loop). |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
424 """ |
|
51194
61787533395c
Merged revisions 69118 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49380
diff
changeset
|
425 paths_seen = set() |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
426 while islink(path): |
|
32621
6aba6d075899
Remove tabs introduced in last commit.
Brett Cannon <bcannon@gmail.com>
parents:
32607
diff
changeset
|
427 if path in paths_seen: |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
428 # Already seen this path, so we must have a symlink loop |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
429 return None |
|
51194
61787533395c
Merged revisions 69118 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49380
diff
changeset
|
430 paths_seen.add(path) |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
431 # Resolve where the link points to |
|
32621
6aba6d075899
Remove tabs introduced in last commit.
Brett Cannon <bcannon@gmail.com>
parents:
32607
diff
changeset
|
432 resolved = os.readlink(path) |
|
32901
30d86e356bf2
Use isabs() in conditional, not abspath
Andrew M. Kuchling <amk@amk.ca>
parents:
32736
diff
changeset
|
433 if not isabs(resolved): |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
434 dir = dirname(path) |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
435 path = normpath(join(dir, resolved)) |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
436 else: |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
437 path = normpath(resolved) |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
438 return path |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
439 |
|
64796
f77cb8d76e99
Issue #767645: Set os.path.supports_unicode_filenames to True in posixpath
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63424
diff
changeset
|
440 supports_unicode_filenames = (sys.platform == 'darwin') |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
441 |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
442 def relpath(path, start=None): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
443 """Return a relative version of a path""" |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
444 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
445 if not path: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
446 raise ValueError("no path specified") |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
447 |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
448 if isinstance(path, bytes): |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
449 curdir = b'.' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
450 sep = b'/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
451 pardir = b'..' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
452 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
453 curdir = '.' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
454 sep = '/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
455 pardir = '..' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
456 |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
457 if start is None: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
458 start = curdir |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
459 |
|
65523
b0ba7eeb98a5
Merged revisions 85689 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
63438
diff
changeset
|
460 start_list = [x for x in abspath(start).split(sep) if x] |
|
b0ba7eeb98a5
Merged revisions 85689 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
63438
diff
changeset
|
461 path_list = [x for x in abspath(path).split(sep) if x] |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
462 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
463 # Work out how much of the filepath is shared by start and path. |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
464 i = len(commonprefix([start_list, path_list])) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
465 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
466 rel_list = [pardir] * (len(start_list)-i) + path_list[i:] |
|
44535
420fe6d7d5f4
Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
42775
diff
changeset
|
467 if not rel_list: |
|
420fe6d7d5f4
Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
42775
diff
changeset
|
468 return curdir |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
469 return join(*rel_list) |
