Mercurial > cpython-lru_cache-weakref
annotate Lib/posixpath.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | e472481b6d73 |
| children | cf1ac0c9e753 |
| 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 |
|
d7517c306791
Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
74 will be discarded.""" |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
75 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
|
76 path = a |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
77 for b in p: |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
78 if b.startswith(sep): |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
79 path = b |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
80 elif not path or path.endswith(sep): |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
81 path += b |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
82 else: |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
83 path += sep + b |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
84 return path |
| 5 | 85 |
| 86 | |
|
769
f8421da4efa7
Renamed to posixpath; changed def'n of split().
Guido van Rossum <guido@python.org>
parents:
582
diff
changeset
|
87 # 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
|
88 # 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
|
89 # '/' 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
|
90 # Trailing '/'es are stripped from head unless it is the root. |
| 582 | 91 |
| 5 | 92 def split(p): |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
93 """Split a pathname. Returns tuple "(head, tail)" where "tail" is |
| 14909 | 94 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
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 return head, tail |
| 5 | 101 |
| 102 | |
| 361 | 103 # Split a path in root and extension. |
|
4074
7b6eefac6e49
Correct description of splitext().
Guido van Rossum <guido@python.org>
parents:
3795
diff
changeset
|
104 # The extension is everything starting at the last dot in the last |
| 361 | 105 # pathname component; the root is everything before that. |
| 582 | 106 # It is always true that root + ext == p. |
| 107 | |
| 361 | 108 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
|
109 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
|
110 sep = b'/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
111 extsep = b'.' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
112 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
113 sep = '/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
114 extsep = '.' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
115 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
|
116 splitext.__doc__ = genericpath._splitext.__doc__ |
| 361 | 117 |
| 2925 | 118 # Split a pathname into a drive specification and the rest of the |
| 119 # path. Useful on DOS/Windows/NT; on Unix, the drive is always empty. | |
| 120 | |
| 121 def splitdrive(p): | |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
122 """Split a pathname into drive and path. On Posix, drive is always |
| 14909 | 123 empty.""" |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
124 return p[:0], p |
| 2925 | 125 |
| 126 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
35377
diff
changeset
|
127 # Return the tail (basename) part of a path, same as split(path)[1]. |
| 582 | 128 |
| 5 | 129 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
|
130 """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
|
131 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
|
132 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
|
133 return p[i:] |
| 5 | 134 |
| 135 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
35377
diff
changeset
|
136 # 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
|
137 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
138 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
|
139 """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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 head = head.rstrip(sep) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
35377
diff
changeset
|
145 return head |
| 5 | 146 |
|
8746
ca1b906149e6
Added getsize(), getmtime(), getatime()
Guido van Rossum <guido@python.org>
parents:
6757
diff
changeset
|
147 |
| 582 | 148 # 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
|
149 # This will always return false on systems where os.lstat doesn't exist. |
| 582 | 150 |
| 151 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
|
152 """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
|
153 try: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
154 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
|
155 except (os.error, AttributeError): |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
156 return False |
|
23625
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
157 return stat.S_ISLNK(st.st_mode) |
| 582 | 158 |
|
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
|
159 # 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
|
160 |
|
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
|
161 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
|
162 """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
|
163 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
|
164 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
|
165 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
|
166 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
|
167 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
|
168 |
|
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 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
170 # Are two filenames really pointing to the same file? |
| 582 | 171 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
172 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
|
173 """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
|
174 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
|
175 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
|
176 return samestat(s1, s2) |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
177 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
178 |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
179 # Are two open files really referencing the same file? |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
180 # (Not necessarily the same file descriptor!) |
| 582 | 181 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
182 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
|
183 """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
|
184 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
|
185 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
|
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 stat buffers (obtained from stat, fstat or lstat) |
|
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
190 # describing the same file? |
| 582 | 191 |
|
428
3c7b7caa9da5
Added samefile() function.
Guido van Rossum <guido@python.org>
parents:
361
diff
changeset
|
192 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
|
193 """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
|
194 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
|
195 s1.st_dev == s2.st_dev |
| 5 | 196 |
| 197 | |
| 198 # 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
|
199 # (Does this work for all UNIXes? Is it even guaranteed to work by Posix?) |
| 582 | 200 |
| 5 | 201 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
|
202 """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
|
203 if islink(path): |
|
9ee196a7f9a2
Merged revisions 83393,83396,83398,83404-83405,83408 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53072
diff
changeset
|
204 # 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
|
205 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
|
206 try: |
|
44535
420fe6d7d5f4
Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
42775
diff
changeset
|
207 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
|
208 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
|
209 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
|
210 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
211 parent = join(path, '..') |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
212 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
|
213 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
|
214 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
|
215 dev1 = s1.st_dev |
|
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
216 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
|
217 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
|
218 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
|
219 ino1 = s1.st_ino |
|
65cf478950ff
Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents:
23620
diff
changeset
|
220 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
|
221 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
|
222 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
|
223 return False |
| 5 | 224 |
| 225 | |
| 582 | 226 # Expand paths beginning with '~' or '~user'. |
| 227 # '~' means $HOME; '~user' means that user's home directory. | |
| 228 # If the path doesn't begin with '~', or if the user or $HOME is unknown, | |
| 229 # the path is returned unchanged (leaving error reporting to whatever | |
| 230 # function is called with the expanded path as argument). | |
| 231 # See also module 'glob' for expansion of *, ? and [...] in pathnames. | |
| 232 # (A function should also be defined to do full *sh-style environment | |
| 233 # variable expansion.) | |
| 234 | |
| 235 def expanduser(path): | |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
236 """Expand ~ and ~user constructions. If user or $HOME is unknown, |
| 14909 | 237 do nothing.""" |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
238 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
|
239 tilde = b'~' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
240 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
241 tilde = '~' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
242 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
|
243 return path |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
244 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
|
245 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
|
246 if i < 0: |
|
c3bc7ed9d315
Use find() instead of looping over the string in expanduser().
Walter Dörwald <walter@livinglogic.de>
parents:
29512
diff
changeset
|
247 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
|
248 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
|
249 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
|
250 import pwd |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
251 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
|
252 else: |
|
56f9f5ab6ec1
SF # 555779, import user doesn't work with CGIs
Neal Norwitz <nnorwitz@gmail.com>
parents:
23709
diff
changeset
|
253 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
|
254 else: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
255 import pwd |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
256 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
|
257 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
|
258 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
|
259 try: |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
260 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
|
261 except KeyError: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
262 return path |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
263 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
|
264 if isinstance(path, bytes): |
|
65022
1add8e5f3198
Use os.fsencode() to support surrogates
Victor Stinner <victor.stinner@haypocalc.com>
parents:
64796
diff
changeset
|
265 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
|
266 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
|
267 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
|
268 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
|
269 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
|
270 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
|
271 |
|
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
272 |
|
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
273 # 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
|
274 # 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
|
275 # 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
|
276 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1320
diff
changeset
|
277 _varprog = None |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
278 _varprogb = None |
|
932
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
279 |
|
615983f1a3ff
Added emacs.py (for misc/py-connect.el).
Guido van Rossum <guido@python.org>
parents:
850
diff
changeset
|
280 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
|
281 """Expand shell variables of form $var and ${var}. Unknown variables |
| 14909 | 282 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
|
283 global _varprog, _varprogb |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
284 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
|
285 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
|
286 return path |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
287 if not _varprogb: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
288 import re |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
289 _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
|
290 search = _varprogb.search |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
291 start = b'{' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
292 end = b'}' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
293 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
294 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
|
295 return path |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
296 if not _varprog: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
297 import re |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
298 _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
|
299 search = _varprog.search |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
300 start = '{' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
301 end = '}' |
|
6757
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
302 i = 0 |
|
22867
e2f2187c3d2c
Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents:
22830
diff
changeset
|
303 while True: |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
304 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
|
305 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
|
306 break |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
307 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
|
308 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
|
309 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
|
310 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
|
311 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
|
312 name = str(name, 'ASCII') |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
22867
diff
changeset
|
313 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
|
314 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
|
315 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
|
316 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
|
317 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
|
318 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
|
319 i = len(path) |
|
29512
6444ea3dc83c
Modernize Lib/posixpath.py: Use startswith(), endswith(), rstrip(),
Walter Dörwald <walter@livinglogic.de>
parents:
27897
diff
changeset
|
320 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
|
321 else: |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
322 i = j |
|
aff6ff52d5e7
Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents:
6519
diff
changeset
|
323 return path |
|
1086
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
324 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
325 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
326 # 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
|
327 # 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
|
328 # 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
|
329 |
|
db817f3a1c65
* change default line numbers for 'list' in pdb.py
Guido van Rossum <guido@python.org>
parents:
932
diff
changeset
|
330 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
|
331 """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
|
332 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
|
333 sep = b'/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
334 empty = b'' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
335 dot = b'.' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
336 dotdot = b'..' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
337 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
338 sep = '/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
339 empty = '' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
340 dot = '.' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
341 dotdot = '..' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
342 if path == empty: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
343 return dot |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
344 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
|
345 # 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
|
346 # as single slash. |
|
16535
084b3dc9b6ff
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16333
diff
changeset
|
347 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
|
348 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
|
349 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
|
350 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
|
351 new_comps = [] |
|
52595b70a568
added rewritten normpath from Moshe Zadka that does the right thing with
Skip Montanaro <skip@pobox.com>
parents:
13622
diff
changeset
|
352 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
|
353 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
|
354 continue |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
355 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
|
356 (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
|
357 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
|
358 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
|
359 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
|
360 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
|
361 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
|
362 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
|
363 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
|
364 return path or dot |
| 10018 | 365 |
| 366 | |
| 367 def abspath(path): | |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11428
diff
changeset
|
368 """Return an absolute path.""" |
| 10018 | 369 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
|
370 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
|
371 cwd = os.getcwdb() |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
372 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
373 cwd = os.getcwd() |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
374 path = join(cwd, path) |
| 10018 | 375 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
|
376 |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
377 |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
378 # 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
|
379 # filesystem). |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
380 |
|
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
381 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
|
382 """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
|
383 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
|
384 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
|
385 sep = b'/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
386 empty = b'' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
387 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
388 sep = '/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
389 empty = '' |
|
33129
ae9b47a8d648
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents:
32901
diff
changeset
|
390 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
|
391 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
|
392 else: |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
393 bits = [empty] + filename.split(sep) |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33129
diff
changeset
|
394 |
|
19934
f2f25a738f7f
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents:
17549
diff
changeset
|
395 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
|
396 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
|
397 # Resolve symbolic links. |
|
32621
6aba6d075899
Remove tabs introduced in last commit.
Brett Cannon <bcannon@gmail.com>
parents:
32607
diff
changeset
|
398 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
|
399 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
|
400 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
|
401 # 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
|
402 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
|
403 else: |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
404 newpath = join(*([resolved] + bits[i:])) |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33129
diff
changeset
|
405 return realpath(newpath) |
|
19953
0f9df563b65d
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
19934
diff
changeset
|
406 |
|
33129
ae9b47a8d648
bug #990669: os.path.realpath() will resolve symlinks before normalizing the
Johannes Gijsbers <jlg@dds.nl>
parents:
32901
diff
changeset
|
407 return abspath(filename) |
|
33204
c18aca7e81cb
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
33129
diff
changeset
|
408 |
|
32607
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
409 |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
410 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
|
411 """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
|
412 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
|
413 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
|
414 """ |
|
51194
61787533395c
Merged revisions 69118 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49380
diff
changeset
|
415 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
|
416 while islink(path): |
|
32621
6aba6d075899
Remove tabs introduced in last commit.
Brett Cannon <bcannon@gmail.com>
parents:
32607
diff
changeset
|
417 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
|
418 # 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
|
419 return None |
|
51194
61787533395c
Merged revisions 69118 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
49380
diff
changeset
|
420 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
|
421 # Resolve where the link points to |
|
32621
6aba6d075899
Remove tabs introduced in last commit.
Brett Cannon <bcannon@gmail.com>
parents:
32607
diff
changeset
|
422 resolved = os.readlink(path) |
|
32901
30d86e356bf2
Use isabs() in conditional, not abspath
Andrew M. Kuchling <amk@amk.ca>
parents:
32736
diff
changeset
|
423 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
|
424 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
|
425 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
|
426 else: |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
427 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
|
428 return path |
|
c4316df2fed4
posixpath.realpath() now detects symlink loops and returns the path just before
Brett Cannon <bcannon@gmail.com>
parents:
32302
diff
changeset
|
429 |
|
64796
f77cb8d76e99
Issue #767645: Set os.path.supports_unicode_filenames to True in posixpath
Victor Stinner <victor.stinner@haypocalc.com>
parents:
63424
diff
changeset
|
430 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
|
431 |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
432 def relpath(path, start=None): |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
433 """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
|
434 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
435 if not path: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
436 raise ValueError("no path specified") |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
437 |
|
49380
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
438 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
|
439 curdir = b'.' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
440 sep = b'/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
441 pardir = b'..' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
442 else: |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
443 curdir = '.' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
444 sep = '/' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
445 pardir = '..' |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
446 |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
447 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
|
448 start = curdir |
|
8a7c930abab6
Issue #3187: Better support for "undecodable" filenames. Code by Victor
Guido van Rossum <guido@python.org>
parents:
48769
diff
changeset
|
449 |
|
65523
b0ba7eeb98a5
Merged revisions 85689 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents:
63438
diff
changeset
|
450 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
|
451 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
|
452 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
453 # 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
|
454 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
|
455 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
456 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
|
457 if not rel_list: |
|
420fe6d7d5f4
Merged revisions 59703-59773 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
42775
diff
changeset
|
458 return curdir |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
459 return join(*rel_list) |
