annotate Lib/ntpath.py @ 51749:1408feec0084 3.0

Merged revisions 69798 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ................ r69798 | benjamin.peterson | 2009-02-19 22:02:38 -0600 (Thu, 19 Feb 2009) | 13 lines Merged revisions 69769,69776 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r69769 | georg.brandl | 2009-02-19 02:30:06 -0600 (Thu, 19 Feb 2009) | 1 line #5310, #3558: fix operator precedence table. ........ r69776 | georg.brandl | 2009-02-19 10:34:51 -0600 (Thu, 19 Feb 2009) | 2 lines #5317: update IronPython URL. ........ ................
author Benjamin Peterson <benjamin@python.org>
date Fri, 20 Feb 2009 04:05:32 +0000
parents 84ccf91a06cc
children 46a45eccc9f7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
1 # Module 'ntpath' -- common operations on WinNT/Win95 pathnames
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
2 """Common pathname manipulations, WindowsNT/95 version.
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
3
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
4 Instead of importing this module directly, import os and refer to this
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
5 module as os.path.
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
6 """
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 import os
45471
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
9 import sys
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 import stat
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
11 import genericpath
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
12 from genericpath import *
13581
e871bd05c6a7 forgot to change copy.copy(m) to m[:]
Skip Montanaro <skip@pobox.com>
parents: 13577
diff changeset
13
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
14 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
15 "basename","dirname","commonprefix","getsize","getmtime",
35376
e7ed0e6aed2f Bug #1266283: lexists() is not exported from os.path
Georg Brandl <georg@python.org>
parents: 35308
diff changeset
16 "getatime","getctime", "islink","exists","lexists","isdir","isfile",
46770
8bfe2b06527d Remove os.path.walk
Benjamin Peterson <benjamin@python.org>
parents: 45471
diff changeset
17 "ismount", "expanduser","expandvars","normpath","abspath",
35376
e7ed0e6aed2f Bug #1266283: lexists() is not exported from os.path
Georg Brandl <georg@python.org>
parents: 35308
diff changeset
18 "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
19 "extsep","devnull","realpath","supports_unicode_filenames","relpath"]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
21 # strings representing various path-related bits and pieces
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
22 # 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
23 curdir = '.'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
24 pardir = '..'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
25 extsep = '.'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
26 sep = '\\'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
27 pathsep = ';'
28427
9d3a25e99075 make nt altsep forward slash - closes bug 709428
Skip Montanaro <skip@pobox.com>
parents: 27908
diff changeset
28 altsep = '/'
27908
47b8cabab7b1 Tweak to Skip's checkin of patch 686397:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 27897
diff changeset
29 defpath = '.;C:\\bin'
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
30 if 'ce' in sys.builtin_module_names:
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
31 defpath = '\\Windows'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
32 elif 'os2' in sys.builtin_module_names:
27908
47b8cabab7b1 Tweak to Skip's checkin of patch 686397:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 27897
diff changeset
33 # OS/2 w/ VACPP
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
34 altsep = '/'
32302
e196b823e6c5 Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
35 devnull = 'nul'
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
36
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
37 def _get_sep(path):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
38 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
39 return b'\\'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
40 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
41 return '\\'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
42
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
43 def _get_altsep(path):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
44 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
45 return b'/'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
46 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
47 return '/'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
48
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
49 def _get_bothseps(path):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
50 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
51 return b'\\/'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
52 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
53 return '\\/'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
54
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
55 def _get_dot(path):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
56 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
57 return b'.'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
58 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
59 return '.'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
60
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
61 def _get_colon(path):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
62 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
63 return b':'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
64 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
65 return ':'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
66
5976
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
67 # Normalize the case of a pathname and map slashes to backslashes.
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
68 # Other normalizations (such as optimizing '../' away) are not done
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 # (this is done by normpath).
5976
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
70
7351
1baca5d2ba57 (1) Change normpath() to *not* also call normcase().
Guido van Rossum <guido@python.org>
parents: 7123
diff changeset
71 def normcase(s):
1baca5d2ba57 (1) Change normpath() to *not* also call normcase().
Guido van Rossum <guido@python.org>
parents: 7123
diff changeset
72 """Normalize case of pathname.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
74 Makes all characters lowercase and all slashes into backslashes."""
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
75 return s.replace(_get_altsep(s), _get_sep(s)).lower()
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
5703
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
77
11804
57788da16488 Typo in a comment: "wheter" --> "whether"
Fred Drake <fdrake@acm.org>
parents: 11693
diff changeset
78 # Return whether a path is absolute.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 # Trivial in Posix, harder on the Mac or MS-DOS.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 # For DOS it is absolute if it starts with a slash or backslash (current
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
81 # volume), or if a pathname after the volume letter and colon / UNC resource
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
82 # starts with a slash or backslash.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 def isabs(s):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
85 """Test whether a path is absolute"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
86 s = splitdrive(s)[1]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
87 return len(s) > 0 and s[:1] in _get_bothseps(s)
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
88
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
5703
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
90 # Join two (or more) paths.
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
91
5133
666152b77027 join(): join one or more path components
Barry Warsaw <barry@python.org>
parents: 5027
diff changeset
92 def join(a, *p):
42775
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
93 """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
94 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
95 will be discarded."""
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
96 sep = _get_sep(a)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
97 seps = _get_bothseps(a)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
98 colon = _get_colon(a)
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
99 path = a
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
100 for b in p:
18847
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
101 b_wins = 0 # set to 1 iff b makes path irrelevant
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
102 if not path:
18847
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
103 b_wins = 1
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
104
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
105 elif isabs(b):
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
106 # This probably wipes out path so far. However, it's more
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
107 # complicated if path begins with a drive letter:
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
108 # 1. join('c:', '/a') == 'c:/a'
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
109 # 2. join('c:/', '/a') == 'c:/a'
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
110 # But
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
111 # 3. join('c:/a', '/b') == '/b'
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
112 # 4. join('c:', 'd:/') = 'd:/'
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
113 # 5. join('c:/', 'd:/') = 'd:/'
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
114 if path[1:2] != colon or b[1:2] == colon:
18847
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
115 # Path doesn't start with a drive letter, or cases 4 and 5.
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
116 b_wins = 1
18742
920e0361dd6e SF bug #44271: os.path.expanduser problem w/o HOME set.
Tim Peters <tim.peters@gmail.com>
parents: 17836
diff changeset
117
18847
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
118 # Else path has a drive letter, and b doesn't but is absolute.
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
119 elif len(path) > 3 or (len(path) == 3 and
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
120 path[-1:] not in seps):
18847
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
121 # case 3
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
122 b_wins = 1
18742
920e0361dd6e SF bug #44271: os.path.expanduser problem w/o HOME set.
Tim Peters <tim.peters@gmail.com>
parents: 17836
diff changeset
123
18847
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
124 if b_wins:
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
125 path = b
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
126 else:
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
127 # Join, and ensure there's a separator.
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
128 assert len(path) > 0
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
129 if path[-1:] in seps:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
130 if b and b[:1] in seps:
18847
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
131 path += b[1:]
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
132 else:
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
133 path += b
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
134 elif path[-1:] == colon:
18847
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
135 path += b
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
136 elif b:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
137 if b[:1] in seps:
18847
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
138 path += b
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
139 else:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
140 path += sep + b
20887
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
141 else:
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
142 # path is not empty and does not end with a backslash,
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
143 # but b is empty; since, e.g., split('a/') produces
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
144 # ('a', ''), it's best if join() adds a backslash in
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
145 # this case.
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
146 path += sep
18742
920e0361dd6e SF bug #44271: os.path.expanduser problem w/o HOME set.
Tim Peters <tim.peters@gmail.com>
parents: 17836
diff changeset
147
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
148 return path
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 # Split a path in a drive specification (a drive letter followed by a
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
152 # colon) and the path specification.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 # It is always true that drivespec + pathspec == p
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 def splitdrive(p):
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
155 """Split a pathname into drive and path specifiers. Returns a 2-tuple
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
156 "(drive,path)"; either part may be empty"""
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
157 if p[1:2] == _get_colon(p):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
158 return p[0:2], p[2:]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
159 return p[:0], p
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
160
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
161
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
162 # Parse UNC paths
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
163 def splitunc(p):
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
164 """Split a pathname into UNC mount point and relative path specifiers.
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
165
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
166 Return a 2-tuple (unc, rest); either part may be empty.
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
167 If unc is not empty, it has the form '//host/mount' (or similar
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
168 using backslashes). unc+rest is always the input path.
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
169 Paths containing drive letters never have an UNC part.
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
170 """
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
171 sep = _get_sep(p)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
172 if not p[1:2]:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
173 return p[:0], p # Drive letter present
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
174 firstTwo = p[0:2]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
175 if normcase(firstTwo) == sep + sep:
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
176 # is a UNC path:
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
177 # vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
178 # \\machine\mountpoint\directories...
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
179 # directory ^^^^^^^^^^^^^^^
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
180 normp = normcase(p)
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
181 index = normp.find(sep, 2)
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
182 if index == -1:
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
183 ##raise RuntimeError, 'illegal UNC path: "' + p + '"'
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
184 return (p[:0], p)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
185 index = normp.find(sep, index + 1)
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
186 if index == -1:
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
187 index = len(p)
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
188 return p[:index], p[index:]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
189 return p[:0], p
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
190
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 # Split a path in head (everything up to the last '/') and tail (the
10433
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
193 # rest). After the trailing '/' is stripped, the invariant
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 # join(head, tail) == p holds.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 # The resulting head won't end in '/' unless it is the root.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 def split(p):
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
198 """Split a pathname.
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
199
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
200 Return tuple (head, tail) where tail is everything after the final slash.
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
201 Either part may be empty."""
10433
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
202
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
203 seps = _get_bothseps(p)
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
204 d, p = splitdrive(p)
10433
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
205 # set i to index beyond p's last slash
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
206 i = len(p)
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
207 while i and p[i-1] not in seps:
10433
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
208 i = i - 1
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
209 head, tail = p[:i], p[i:] # now tail has no slashes
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
210 # remove trailing slashes from head, unless it's all slashes
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
211 head2 = head
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
212 while head2 and head2[-1:] in seps:
10433
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
213 head2 = head2[:-1]
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
214 head = head2 or head
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
215 return d + head, tail
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
216
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 # Split a path in root and extension.
5027
0ea4ea5b2eb7 Fix splitext() to go up to the last dot, not the first.
Guido van Rossum <guido@python.org>
parents: 4150
diff changeset
219 # The extension is everything starting at the last dot in the last
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 # pathname component; the root is everything before that.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 # It is always true that root + ext == p.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
222
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 def splitext(p):
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
224 return genericpath._splitext(p, _get_sep(p), _get_altsep(p),
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
225 _get_dot(p))
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
226 splitext.__doc__ = genericpath._splitext.__doc__
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
227
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
228
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 # Return the tail (basename) part of a path.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
230
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 def basename(p):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
232 """Returns the final component of a pathname"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
233 return split(p)[1]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
234
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
235
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 # Return the head (dirname) part of a path.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
237
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 def dirname(p):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
239 """Returns the directory component of a pathname"""
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
240 return split(p)[0]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
241
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 # Is a path a symbolic link?
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 # This will always return false on systems where posix.lstat doesn't exist.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 def islink(path):
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
246 """Test for symbolic link.
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
247 On WindowsNT/95 and OS/2 always returns false
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
248 """
22867
e2f2187c3d2c Partial introduction of bools where appropriate.
Guido van Rossum <guido@python.org>
parents: 22830
diff changeset
249 return False
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
250
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
251 # alias exists to lexists
33332
062177de8685 Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents: 32595
diff changeset
252 lexists = exists
062177de8685 Patch #941486: add os.path.lexists(). Also fix bug #940578 by using lexists in glob.glob.
Johannes Gijsbers <jlg@dds.nl>
parents: 32595
diff changeset
253
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
254 # Is a path a mount point? Either a root (with or without drive letter)
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
255 # or an UNC path with at most a / or \ after the mount point.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
256
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 def ismount(path):
7123
5d517ddcbd2e Fix to ismount(). Can't remember who told me this.
Guido van Rossum <guido@python.org>
parents: 6756
diff changeset
258 """Test whether a path is a mount point (defined as root of drive)"""
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
259 unc, rest = splitunc(path)
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
260 seps = _get_bothseps(p)
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
261 if unc:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
262 return rest in p[:0] + seps
7123
5d517ddcbd2e Fix to ismount(). Can't remember who told me this.
Guido van Rossum <guido@python.org>
parents: 6756
diff changeset
263 p = splitdrive(path)[1]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
264 return len(p) == 1 and p[0] in seps
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
265
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
266
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 # Expand paths beginning with '~' or '~user'.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 # '~' means $HOME; '~user' means that user's home directory.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 # If the path doesn't begin with '~', or if the user or $HOME is unknown,
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 # the path is returned unchanged (leaving error reporting to whatever
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 # function is called with the expanded path as argument).
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 # See also module 'glob' for expansion of *, ? and [...] in pathnames.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 # (A function should also be defined to do full *sh-style environment
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 # variable expansion.)
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
275
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 def expanduser(path):
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
277 """Expand ~ and ~user constructs.
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
278
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
279 If user or $HOME is unknown, do nothing."""
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
280 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
281 tilde = b'~'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
282 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
283 tilde = '~'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
284 if not path.startswith(tilde):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
285 return path
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
286 i, n = 1, len(path)
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
287 while i < n and path[i] not in _get_bothseps(path):
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
288 i = i + 1
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
289
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
290 if 'HOME' in os.environ:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
291 userhome = os.environ['HOME']
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
292 elif 'USERPROFILE' in os.environ:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
293 userhome = os.environ['USERPROFILE']
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
294 elif not 'HOMEPATH' in os.environ:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
295 return path
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
296 else:
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
297 try:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
298 drive = os.environ['HOMEDRIVE']
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
299 except KeyError:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
300 drive = ''
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
301 userhome = join(drive, os.environ['HOMEPATH'])
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
302
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
303 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
304 userhome = userhome.encode(sys.getfilesystemencoding())
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
305
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
306 if i != 1: #~user
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
307 userhome = join(dirname(userhome), path[1:i])
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
308
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
309 return userhome + path[i:]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
310
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
311
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 # Expand paths containing shell variable substitutions.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 # The following rules apply:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
314 # - no expansion within single quotes
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
315 # - '$$' is translated into '$'
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
316 # - '%%' is translated into '%' if '%%' are not seen in %var1%%var2%
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
317 # - ${varname} is accepted.
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
318 # - $varname is accepted.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
319 # - %varname% is accepted.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
320 # - varnames can be made out of letters, digits and the characters '_-'
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
321 # (though is not verifed in the ${varname} and %varname% cases)
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 # XXX With COMMAND.COM you can use any characters in a variable name,
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 # XXX except '^|<>='.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
324
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
325 def expandvars(path):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
326 """Expand shell variables of the forms $var, ${var} and %var%.
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
327
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
328 Unknown variables are left unchanged."""
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
329 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
330 if ord('$') not in path and ord('%') not in path:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
331 return path
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
332 import string
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
333 varchars = bytes(string.ascii_letters + string.digits + '_-', 'ascii')
49412
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
334 quote = b'\''
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
335 percent = b'%'
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
336 brace = b'{'
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
337 dollar = b'$'
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
338 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
339 if '$' not in path and '%' not in path:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
340 return path
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
341 import string
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
342 varchars = string.ascii_letters + string.digits + '_-'
49412
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
343 quote = '\''
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
344 percent = '%'
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
345 brace = '{'
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
346 dollar = '$'
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
347 res = path[:0]
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
348 index = 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
349 pathlen = len(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
350 while index < pathlen:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
351 c = path[index:index+1]
49412
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
352 if c == quote: # no expansion within single quotes
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
353 path = path[index + 1:]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
354 pathlen = len(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
355 try:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
356 index = path.index(c)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
357 res = res + c + path[:index + 1]
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
358 except ValueError:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
359 res = res + path
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
360 index = pathlen - 1
49412
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
361 elif c == percent: # variable or '%'
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
362 if path[index + 1:index + 2] == percent:
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
363 res = res + c
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
364 index = index + 1
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
365 else:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
366 path = path[index+1:]
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
367 pathlen = len(path)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
368 try:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
369 index = path.index(percent)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
370 except ValueError:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
371 res = res + percent + path
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
372 index = pathlen - 1
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
373 else:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
374 var = path[:index]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
375 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
376 var = var.decode('ascii')
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
377 if var in os.environ:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
378 value = os.environ[var]
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
379 else:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
380 value = '%' + var + '%'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
381 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
382 value = value.encode('ascii')
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
383 res = res + value
49412
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
384 elif c == dollar: # variable or '$$'
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
385 if path[index + 1:index + 2] == dollar:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
386 res = res + c
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
387 index = index + 1
49412
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
388 elif path[index + 1:index + 2] == brace:
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
389 path = path[index+2:]
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
390 pathlen = len(path)
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
391 try:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
392 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
393 index = path.index(b'}')
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
394 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
395 index = path.index('}')
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
396 var = path[:index]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
397 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
398 var = var.decode('ascii')
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
399 if var in os.environ:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
400 value = os.environ[var]
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
401 else:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
402 value = '${' + var + '}'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
403 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
404 value = value.encode('ascii')
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
405 res = res + value
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
406 except ValueError:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
407 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
408 res = res + b'${' + path
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
409 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
410 res = res + '${' + path
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
411 index = pathlen - 1
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
412 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
413 var = ''
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
414 index = index + 1
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
415 c = path[index:index + 1]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
416 while c and c in varchars:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
417 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
418 var = var + c.decode('ascii')
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
419 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
420 var = var + c
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
421 index = index + 1
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
422 c = path[index:index + 1]
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
423 if var in os.environ:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
424 value = os.environ[var]
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
425 else:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
426 value = '$' + var
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
427 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
428 value = value.encode('ascii')
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
429 res = res + value
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
430 if c:
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
431 index = index - 1
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
432 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
433 res = res + c
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
434 index = index + 1
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
435 return res
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
436
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
437
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
438 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A\B.
4150
8a0d43b13125 Don't do truncation to 8+3 format -- this is used on NT file systems!
Guido van Rossum <guido@python.org>
parents: 3953
diff changeset
439 # Previously, this function also truncated pathnames to 8+3 format,
8a0d43b13125 Don't do truncation to 8+3 format -- this is used on NT file systems!
Guido van Rossum <guido@python.org>
parents: 3953
diff changeset
440 # but as this module is called "ntpath", that's obviously wrong!
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
441
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 def normpath(path):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
443 """Normalize path, eliminating double slashes, etc."""
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
444 sep = _get_sep(path)
49412
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
445 dotdot = _get_dot(path) * 2
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
446 path = path.replace(_get_altsep(path), sep)
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
447 prefix, path = splitdrive(path)
32595
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
448 # We need to be careful here. If the prefix is empty, and the path starts
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
449 # with a backslash, it could either be an absolute path on the current
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
450 # drive (\dir1\dir2\file) or a UNC filename (\\server\mount\dir1\file). It
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
451 # is therefore imperative NOT to collapse multiple backslashes blindly in
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
452 # that case.
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
453 # The code below preserves multiple backslashes when there is no drive
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
454 # letter. This means that the invalid filename \\\a\b is preserved
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
455 # unchanged, where a\\\b is normalised to a\b. It's not clear that there
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
456 # is any better behaviour for such edge cases.
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
457 if not prefix:
32595
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
458 # No drive letter - preserve initial backslashes
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
459 while path[:1] == sep:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
460 prefix = prefix + sep
32595
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
461 path = path[1:]
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
462 else:
b527cb1b2b9e Make ntpath compress multiple slashes between drive letter and the rest of the
Brett Cannon <bcannon@gmail.com>
parents: 32302
diff changeset
463 # We have a drive letter - collapse initial backslashes
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
464 if path.startswith(sep):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
465 prefix = prefix + sep
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
466 path = path.lstrip(sep)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
467 comps = path.split(sep)
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
468 i = 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
469 while i < len(comps):
49412
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
470 if not comps[i] or comps[i] == _get_dot(path):
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
471 del comps[i]
49412
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
472 elif comps[i] == dotdot:
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
473 if i > 0 and comps[i-1] != dotdot:
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
474 del comps[i-1:i+1]
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
475 i -= 1
49412
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
476 elif i == 0 and prefix.endswith(_get_sep(path)):
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
477 del comps[i]
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
478 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
479 i += 1
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
480 else:
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
481 i += 1
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
482 # If the path is now empty, substitute '.'
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
483 if not prefix and not comps:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
484 comps.append(_get_dot(path))
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
485 return prefix + sep.join(comps)
10018
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
486
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
487
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
488 # Return an absolute path.
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
489 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
490 from nt import _getfullpathname
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
491
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
492 except ImportError: # not running on Windows - mock up something sensible
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
493 def abspath(path):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
494 """Return the absolute version of a path."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
495 if not isabs(path):
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
496 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
497 cwd = os.getcwdb()
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
498 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
499 cwd = os.getcwd()
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
500 path = join(cwd, path)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
501 return normpath(path)
21743
34f85a0a7faa Allow abspath to still do something sensisble if the nt module can not be imported.
Mark Hammond <mhammond@skippinet.com.au>
parents: 20887
diff changeset
502
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
503 else: # use native Windows method on Windows
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
504 def abspath(path):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
505 """Return the absolute version of a path."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
506
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
507 if path: # Empty path must return current working directory.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
508 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
509 path = _getfullpathname(path)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
510 except WindowsError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
511 pass # Bad path - return unchanged.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
512 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
513 path = os.getcwd()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
514 return normpath(path)
19934
f2f25a738f7f SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents: 19628
diff changeset
515
f2f25a738f7f SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents: 19628
diff changeset
516 # realpath is a no-op on systems without islink support
f2f25a738f7f SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents: 19628
diff changeset
517 realpath = abspath
25666
4a825b902310 Add os.path.supports_unicode_filenames for all platforms,
Mark Hammond <mhammond@skippinet.com.au>
parents: 23625
diff changeset
518 # Win9x family and earlier have no Unicode filename support.
25676
bac5a4932905 Don't try to access sys.getwindowsversion unless it exists (ntpath is
Tim Peters <tim.peters@gmail.com>
parents: 25666
diff changeset
519 supports_unicode_filenames = (hasattr(sys, "getwindowsversion") and
bac5a4932905 Don't try to access sys.getwindowsversion unless it exists (ntpath is
Tim Peters <tim.peters@gmail.com>
parents: 25666
diff changeset
520 sys.getwindowsversion()[3] >= 2)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
521
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
522 def relpath(path, start=curdir):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
523 """Return a relative version of a path"""
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
524 sep = _get_sep(path)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
525
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
526 if start is curdir:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
527 start = _get_dot(path)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
528
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
529 if not path:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
530 raise ValueError("no path specified")
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
531 start_list = abspath(start).split(sep)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
532 path_list = abspath(path).split(sep)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
533 if start_list[0].lower() != path_list[0].lower():
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
534 unc_path, rest = splitunc(path)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
535 unc_start, rest = splitunc(start)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
536 if bool(unc_path) ^ bool(unc_start):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
537 raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)"
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
538 % (path, start))
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
539 else:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
540 raise ValueError("path is on drive %s, start on drive %s"
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
541 % (path_list[0], start_list[0]))
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
542 # 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: 40737
diff changeset
543 for i in range(min(len(start_list), len(path_list))):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
544 if start_list[i].lower() != path_list[i].lower():
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
545 break
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
546 else:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
547 i += 1
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
548
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
549 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
550 pardir = b'..'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
551 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
552 pardir = '..'
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
553 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
554 if not rel_list:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
555 return _get_dot(path)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
556 return join(*rel_list)