annotate Lib/ntpath.py @ 70817:dd775b1561ba

Fix markup.
author Georg Brandl <georg@python.org>
date Tue, 14 Jun 2011 21:09:55 +0200
parents d40609dd01e0
children d853354e1470
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",
62874
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
19 "extsep","devnull","realpath","supports_unicode_filenames","relpath",
64495
f6dfd6c563e1 Implement #7566 - os.path.sameopenfile for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 63385
diff changeset
20 "samefile", "sameopenfile",]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
22 # 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
23 # 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
24 curdir = '.'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
25 pardir = '..'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
26 extsep = '.'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
27 sep = '\\'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
28 pathsep = ';'
28427
9d3a25e99075 make nt altsep forward slash - closes bug 709428
Skip Montanaro <skip@pobox.com>
parents: 27908
diff changeset
29 altsep = '/'
27908
47b8cabab7b1 Tweak to Skip's checkin of patch 686397:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 27897
diff changeset
30 defpath = '.;C:\\bin'
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
31 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
32 defpath = '\\Windows'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
33 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
34 # OS/2 w/ VACPP
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
35 altsep = '/'
32302
e196b823e6c5 Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
36 devnull = 'nul'
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
37
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
38 def _get_empty(path):
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
39 if isinstance(path, bytes):
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
40 return b''
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
41 else:
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
42 return ''
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
43
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
44 def _get_sep(path):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
45 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
46 return b'\\'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
47 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
48 return '\\'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
49
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
50 def _get_altsep(path):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
51 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
52 return b'/'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
53 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
54 return '/'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
55
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
56 def _get_bothseps(path):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
57 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
58 return b'\\/'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
59 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
60 return '\\/'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
61
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
62 def _get_dot(path):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
63 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
64 return b'.'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
65 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
66 return '.'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
67
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
68 def _get_colon(path):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
69 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
70 return b':'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
71 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
72 return ':'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
73
63446
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
74 def _get_special(path):
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
75 if isinstance(path, bytes):
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
76 return (b'\\\\.\\', b'\\\\?\\')
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
77 else:
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
78 return ('\\\\.\\', '\\\\?\\')
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
79
5976
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
80 # 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
81 # 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
82 # (this is done by normpath).
5976
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
83
7351
1baca5d2ba57 (1) Change normpath() to *not* also call normcase().
Guido van Rossum <guido@python.org>
parents: 7123
diff changeset
84 def normcase(s):
1baca5d2ba57 (1) Change normpath() to *not* also call normcase().
Guido van Rossum <guido@python.org>
parents: 7123
diff changeset
85 """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
86
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
87 Makes all characters lowercase and all slashes into backslashes."""
62490
4e099e5004a1 #9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59473
diff changeset
88 if not isinstance(s, (bytes, str)):
4e099e5004a1 #9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59473
diff changeset
89 raise TypeError("normcase() argument must be str or bytes, "
4e099e5004a1 #9018: os.path.normcase() now raises a TypeError if the argument is not str or bytes.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 59473
diff changeset
90 "not '{}'".format(s.__class__.__name__))
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
91 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
92
5703
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
93
11804
57788da16488 Typo in a comment: "wheter" --> "whether"
Fred Drake <fdrake@acm.org>
parents: 11693
diff changeset
94 # Return whether a path is absolute.
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
95 # Trivial in Posix, harder on Windows.
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
96 # For Windows it is absolute if it starts with a slash or backslash (current
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
97 # volume), or if a pathname after the volume-letter-and-colon or UNC-resource
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
98 # 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
99
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 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
101 """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
102 s = splitdrive(s)[1]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
103 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
104
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
5703
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
106 # Join two (or more) paths.
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
107
5133
666152b77027 join(): join one or more path components
Barry Warsaw <barry@python.org>
parents: 5027
diff changeset
108 def join(a, *p):
42775
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
109 """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
110 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
111 will be discarded."""
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
112 sep = _get_sep(a)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
113 seps = _get_bothseps(a)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
114 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
115 path = a
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
116 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
117 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
118 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
119 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
120
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 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
122 # This probably wipes out path so far. However, it's more
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
123 # complicated if path begins with a drive letter. You get a+b
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
124 # (minus redundant slashes) in these four cases:
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
125 # 1. join('c:', '/a') == 'c:/a'
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
126 # 2. join('//computer/share', '/a') == '//computer/share/a'
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
127 # 3. join('c:/', '/a') == 'c:/a'
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
128 # 4. join('//computer/share/', '/a') == '//computer/share/a'
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
129 # But b wins in all of these cases:
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
130 # 5. join('c:/a', '/b') == '/b'
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
131 # 6. join('//computer/share/a', '/b') == '/b'
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
132 # 7. join('c:', 'd:/') == 'd:/'
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
133 # 8. join('c:', '//computer/share/') == '//computer/share/'
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
134 # 9. join('//computer/share', 'd:/') == 'd:/'
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
135 # 10. join('//computer/share', '//computer/share/') == '//computer/share/'
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
136 # 11. join('c:/', 'd:/') == 'd:/'
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
137 # 12. join('c:/', '//computer/share/') == '//computer/share/'
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
138 # 13. join('//computer/share/', 'd:/') == 'd:/'
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
139 # 14. join('//computer/share/', '//computer/share/') == '//computer/share/'
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
140 b_prefix, b_rest = splitdrive(b)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
141
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
142 # if b has a prefix, it always wins.
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
143 if b_prefix:
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
144 b_wins = 1
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
145 else:
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
146 # b doesn't have a prefix.
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
147 # but isabs(b) returned true.
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
148 # and therefore b_rest[0] must be a slash.
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
149 # (but let's check that.)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
150 assert(b_rest and b_rest[0] in seps)
18742
920e0361dd6e SF bug #44271: os.path.expanduser problem w/o HOME set.
Tim Peters <tim.peters@gmail.com>
parents: 17836
diff changeset
151
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
152 # so, b still wins if path has a rest that's more than a sep.
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
153 # you get a+b if path_rest is empty or only has a sep.
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
154 # (see cases 1-4 for times when b loses.)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
155 path_rest = splitdrive(path)[1]
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
156 b_wins = path_rest and path_rest not in seps
18742
920e0361dd6e SF bug #44271: os.path.expanduser problem w/o HOME set.
Tim Peters <tim.peters@gmail.com>
parents: 17836
diff changeset
157
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
158 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
159 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
160 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
161 # 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
162 assert len(path) > 0
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
163 if path[-1:] in seps:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
164 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
165 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
166 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
167 path += b
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
168 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
169 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
170 elif b:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
171 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
172 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
173 else:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
174 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
175 else:
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
176 # 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
177 # 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
178 # ('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
179 # this case.
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
180 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
181
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
182 return path
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
184
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 # 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
186 # 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
187 # 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
188 def splitdrive(p):
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
189 """Split a pathname into drive/UNC sharepoint and relative path specifiers.
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
190 Returns a 2-tuple (drive_or_unc, path); either part may be empty.
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
191
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
192 If you assign
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
193 result = splitdrive(p)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
194 It is always true that:
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
195 result[0] + result[1] == p
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
196
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
197 If the path contained a drive letter, drive_or_unc will contain everything
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
198 up to and including the colon. e.g. splitdrive("c:/dir") returns ("c:", "/dir")
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
199
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
200 If the path contained a UNC path, the drive_or_unc will contain the host name
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
201 and share up to but not including the fourth directory separator character.
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
202 e.g. splitdrive("//host/computer/dir") returns ("//host/computer", "/dir")
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
203
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
204 Paths cannot contain both a drive letter and a UNC path.
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
205
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
206 """
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
207 empty = _get_empty(p)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
208 if len(p) > 1:
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
209 sep = _get_sep(p)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
210 normp = normcase(p)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
211 if (normp[0:2] == sep*2) and (normp[2:3] != sep):
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
212 # is a UNC path:
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
213 # vvvvvvvvvvvvvvvvvvvv drive letter or UNC path
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
214 # \\machine\mountpoint\directory\etc\...
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
215 # directory ^^^^^^^^^^^^^^^
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
216 index = normp.find(sep, 2)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
217 if index == -1:
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
218 return empty, p
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
219 index2 = normp.find(sep, index + 1)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
220 # a UNC path can't have two slashes in a row
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
221 # (after the initial two)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
222 if index2 == index + 1:
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
223 return empty, p
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
224 if index2 == -1:
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
225 index2 = len(p)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
226 return p[:index2], p[index2:]
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
227 if normp[1:2] == _get_colon(p):
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
228 return p[:2], p[2:]
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
229 return empty, p
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
230
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
231
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
232 # Parse UNC paths
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
233 def splitunc(p):
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
234 """Deprecated since Python 3.1. Please use splitdrive() instead;
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
235 it now handles UNC paths.
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
236
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
237 Split a pathname into UNC mount point and relative path specifiers.
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
238
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
239 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
240 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
241 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
242 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
243 """
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
244 import warnings
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
245 warnings.warn("ntpath.splitunc is deprecated, use ntpath.splitdrive instead",
54989
d3d6ccc9abb4 PendingDeprecationWarning -> DeprecationWarning. Both of these were
Gregory P. Smith <greg@mad-scientist.com>
parents: 53838
diff changeset
246 DeprecationWarning)
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
247 sep = _get_sep(p)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
248 if not p[1:2]:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
249 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
250 firstTwo = p[0:2]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
251 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
252 # 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
253 # 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
254 # \\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
255 # directory ^^^^^^^^^^^^^^^
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
256 normp = normcase(p)
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
257 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
258 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
259 ##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
260 return (p[:0], p)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
261 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
262 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
263 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
264 return p[:index], p[index:]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
265 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
266
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
267
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 # 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
269 # 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
270 # 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
271 # 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
272
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 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
274 """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
275
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
276 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
277 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
278
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
279 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
280 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
281 # 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
282 i = len(p)
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
283 while i and p[i-1] not in seps:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
284 i -= 1
10433
2ab0ec847184 New code for split() by Tim Peters, behaves more like posixpath.split().
Guido van Rossum <guido@python.org>
parents: 10093
diff changeset
285 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
286 # 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
287 head2 = head
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
288 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
289 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
290 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
291 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
292
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
293
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 # 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
295 # 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
296 # 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
297 # 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
298
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 def splitext(p):
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
300 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
301 _get_dot(p))
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
302 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
303
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
304
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 # 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
306
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 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
308 """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
309 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
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 # 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
313
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 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
315 """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
316 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
317
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 # Is a path a symbolic link?
62874
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
319 # This will always return false on systems where os.lstat doesn't exist.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
320
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 def islink(path):
62874
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
322 """Test whether a path is a symbolic link.
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
323 This will always return false for Windows prior to 6.0
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
324 and for OS/2.
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
325 """
62874
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
326 try:
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
327 st = os.lstat(path)
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
328 except (os.error, AttributeError):
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
329 return False
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
330 return stat.S_ISLNK(st.st_mode)
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
331
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
332 # Being true for dangling symbolic links is also useful.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
333
62874
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
334 def lexists(path):
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
335 """Test whether a path exists. Returns True for broken symbolic links"""
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
336 try:
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
337 st = os.lstat(path)
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
338 except (os.error, WindowsError):
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
339 return False
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
340 return True
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
341
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
342 # 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
343 # 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
344
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 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
346 """Test whether a path is a mount point (defined as root of drive)"""
52403
46a45eccc9f7 Merged revisions 70676 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49412
diff changeset
347 seps = _get_bothseps(path)
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
348 root, rest = splitdrive(path)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
349 if root and root[0] in seps:
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
350 return (not rest) or (rest in seps)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
351 return rest in seps
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
352
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
353
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 # 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
355 # '~' 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
356 # 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
357 # 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
358 # 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
359 # 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
360 # (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
361 # variable expansion.)
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
362
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 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
364 """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
365
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
366 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
367 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
368 tilde = b'~'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
369 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
370 tilde = '~'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
371 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
372 return path
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
373 i, n = 1, len(path)
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
374 while i < n and path[i] not in _get_bothseps(path):
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
375 i += 1
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
376
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
377 if 'HOME' in os.environ:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
378 userhome = os.environ['HOME']
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
379 elif 'USERPROFILE' in os.environ:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
380 userhome = os.environ['USERPROFILE']
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
381 elif not 'HOMEPATH' in os.environ:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
382 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
383 else:
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
384 try:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
385 drive = os.environ['HOMEDRIVE']
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
386 except KeyError:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
387 drive = ''
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
388 userhome = join(drive, os.environ['HOMEPATH'])
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
389
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
390 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
391 userhome = userhome.encode(sys.getfilesystemencoding())
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
392
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
393 if i != 1: #~user
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
394 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
395
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
396 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
397
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
398
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 # 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
400 # 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
401 # - no expansion within single quotes
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
402 # - '$$' is translated into '$'
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
403 # - '%%' 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
404 # - ${varname} is accepted.
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
405 # - $varname is accepted.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
406 # - %varname% is accepted.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
407 # - varnames can be made out of letters, digits and the characters '_-'
68585
bf94b6a73fc8 #11565: Fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 65523
diff changeset
408 # (though is not verified 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
409 # 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
410 # XXX except '^|<>='.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
411
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
412 def expandvars(path):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
413 """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
414
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
415 Unknown variables are left unchanged."""
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
416 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
417 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
418 return path
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
419 import string
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
420 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
421 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
422 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
423 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
424 dollar = b'$'
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
425 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
426 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
427 return path
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
428 import string
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
429 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
430 quote = '\''
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
431 percent = '%'
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
432 brace = '{'
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
433 dollar = '$'
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
434 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
435 index = 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
436 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
437 while index < pathlen:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
438 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
439 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
440 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
441 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
442 try:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
443 index = path.index(c)
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
444 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
445 except ValueError:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
446 res += path
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
447 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
448 elif c == percent: # variable or '%'
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
449 if path[index + 1:index + 2] == percent:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
450 res += c
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
451 index += 1
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
452 else:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
453 path = path[index+1:]
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
454 pathlen = len(path)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
455 try:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
456 index = path.index(percent)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
457 except ValueError:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
458 res += percent + path
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
459 index = pathlen - 1
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
460 else:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
461 var = path[:index]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
462 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
463 var = var.decode('ascii')
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
464 if var in os.environ:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
465 value = os.environ[var]
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
466 else:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
467 value = '%' + var + '%'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
468 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
469 value = value.encode('ascii')
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
470 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
471 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
472 if path[index + 1:index + 2] == dollar:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
473 res += c
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
474 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
475 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
476 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
477 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
478 try:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
479 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
480 index = path.index(b'}')
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
481 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
482 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
483 var = path[:index]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
484 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
485 var = var.decode('ascii')
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
486 if var in os.environ:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
487 value = os.environ[var]
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
488 else:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
489 value = '${' + var + '}'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
490 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
491 value = value.encode('ascii')
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
492 res += value
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
493 except ValueError:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
494 if isinstance(path, bytes):
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
495 res += b'${' + path
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
496 else:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
497 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
498 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
499 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
500 var = ''
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
501 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
502 c = path[index:index + 1]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
503 while c and c in varchars:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
504 if isinstance(path, bytes):
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
505 var += c.decode('ascii')
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
506 else:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
507 var += c
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
508 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
509 c = path[index:index + 1]
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
510 if var in os.environ:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
511 value = os.environ[var]
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
512 else:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
513 value = '$' + var
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
514 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
515 value = value.encode('ascii')
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
516 res += value
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
517 if c:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
518 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
519 else:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
520 res += c
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
521 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
522 return res
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
523
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
524
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
525 # 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
526 # 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
527 # 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
528
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 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
530 """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
531 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
532 dotdot = _get_dot(path) * 2
63446
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
533 special_prefixes = _get_special(path)
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
534 if path.startswith(special_prefixes):
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
535 # in the case of paths with these prefixes:
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
536 # \\.\ -> device names
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
537 # \\?\ -> literal paths
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
538 # do not do any normalization, but return the path unchanged
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
539 return path
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
540 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
541 prefix, path = splitdrive(path)
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
542
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
543 # collapse initial backslashes
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
544 if path.startswith(sep):
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
545 prefix += sep
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
546 path = path.lstrip(sep)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
547
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
548 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
549 i = 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
550 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
551 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
552 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
553 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
554 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
555 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
556 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
557 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
558 del comps[i]
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
559 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
560 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
561 else:
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
562 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
563 # 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
564 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
565 comps.append(_get_dot(path))
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
566 return prefix + sep.join(comps)
10018
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
567
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
568
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
569 # 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
570 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
571 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
572
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
573 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
574 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
575 """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
576 if not isabs(path):
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
577 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
578 cwd = os.getcwdb()
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
579 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
580 cwd = os.getcwd()
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
581 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
582 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
583
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
584 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
585 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
586 """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
587
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
588 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
589 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
590 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
591 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
592 pass # Bad path - return unchanged.
60063
e63d4678fca7 Note: only the relevant parts of r79474 are merged.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 53838
diff changeset
593 elif isinstance(path, bytes):
e63d4678fca7 Note: only the relevant parts of r79474 are merged.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 53838
diff changeset
594 path = os.getcwdb()
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
595 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
596 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
597 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
598
f2f25a738f7f SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents: 19628
diff changeset
599 # 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
600 realpath = abspath
25666
4a825b902310 Add os.path.supports_unicode_filenames for all platforms,
Mark Hammond <mhammond@skippinet.com.au>
parents: 23625
diff changeset
601 # 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
602 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
603 sys.getwindowsversion()[3] >= 2)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
604
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
605 def relpath(path, start=curdir):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
606 """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
607 sep = _get_sep(path)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
608
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
609 if start is curdir:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
610 start = _get_dot(path)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
611
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
612 if not path:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
613 raise ValueError("no path specified")
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
614
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
615 start_abs = abspath(normpath(start))
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
616 path_abs = abspath(normpath(path))
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
617 start_drive, start_rest = splitdrive(start_abs)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
618 path_drive, path_rest = splitdrive(path_abs)
65523
b0ba7eeb98a5 Merged revisions 85689 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 63488
diff changeset
619 if normcase(start_drive) != normcase(path_drive):
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
620 error = "path is on mount '{0}', start on mount '{1}'".format(
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
621 path_drive, start_drive)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
622 raise ValueError(error)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
623
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
624 start_list = [x for x in start_rest.split(sep) if x]
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
625 path_list = [x for x in path_rest.split(sep) if x]
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
626 # Work out how much of the filepath is shared by start and path.
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
627 i = 0
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
628 for e1, e2 in zip(start_list, path_list):
65523
b0ba7eeb98a5 Merged revisions 85689 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 63488
diff changeset
629 if normcase(e1) != normcase(e2):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
630 break
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
631 i += 1
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
632
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
633 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
634 pardir = b'..'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
635 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
636 pardir = '..'
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
637 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
638 if not rel_list:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
639 return _get_dot(path)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
640 return join(*rel_list)
62874
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
641
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
642
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
643 # determine if two files are in fact the same file
64918
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
644 try:
64922
aa56ebb8fe94 Fix #9790 again. Rather than handle NotImplementedError at runtime as
Brian Curtin <brian.curtin@gmail.com>
parents: 64918
diff changeset
645 # GetFinalPathNameByHandle is available starting with Windows 6.0.
aa56ebb8fe94 Fix #9790 again. Rather than handle NotImplementedError at runtime as
Brian Curtin <brian.curtin@gmail.com>
parents: 64918
diff changeset
646 # Windows XP and non-Windows OS'es will mock _getfinalpathname.
aa56ebb8fe94 Fix #9790 again. Rather than handle NotImplementedError at runtime as
Brian Curtin <brian.curtin@gmail.com>
parents: 64918
diff changeset
647 if sys.getwindowsversion()[:2] >= (6, 0):
aa56ebb8fe94 Fix #9790 again. Rather than handle NotImplementedError at runtime as
Brian Curtin <brian.curtin@gmail.com>
parents: 64918
diff changeset
648 from nt import _getfinalpathname
aa56ebb8fe94 Fix #9790 again. Rather than handle NotImplementedError at runtime as
Brian Curtin <brian.curtin@gmail.com>
parents: 64918
diff changeset
649 else:
aa56ebb8fe94 Fix #9790 again. Rather than handle NotImplementedError at runtime as
Brian Curtin <brian.curtin@gmail.com>
parents: 64918
diff changeset
650 raise ImportError
aa56ebb8fe94 Fix #9790 again. Rather than handle NotImplementedError at runtime as
Brian Curtin <brian.curtin@gmail.com>
parents: 64918
diff changeset
651 except (AttributeError, ImportError):
64918
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
652 # On Windows XP and earlier, two files are the same if their absolute
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
653 # pathnames are the same.
64922
aa56ebb8fe94 Fix #9790 again. Rather than handle NotImplementedError at runtime as
Brian Curtin <brian.curtin@gmail.com>
parents: 64918
diff changeset
654 # Non-Windows operating systems fake this method with an XP
aa56ebb8fe94 Fix #9790 again. Rather than handle NotImplementedError at runtime as
Brian Curtin <brian.curtin@gmail.com>
parents: 64918
diff changeset
655 # approximation.
64918
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
656 def _getfinalpathname(f):
69876
011e4bb8b933 ntpath.samefile fails to detect that "A.TXT" and "a.txt" refer to the same file on Windows XP.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 68586
diff changeset
657 return normcase(abspath(f))
64918
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
658
62874
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
659 def samefile(f1, f2):
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
660 "Test whether two pathnames reference the same actual file"
64918
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
661 return _getfinalpathname(f1) == _getfinalpathname(f2)
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
662
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
663
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
664 try:
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
665 from nt import _getfileinformation
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
666 except ImportError:
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
667 # On other operating systems, just return the fd and see that
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
668 # it compares equal in sameopenfile.
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
669 def _getfileinformation(fd):
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
670 return fd
64495
f6dfd6c563e1 Implement #7566 - os.path.sameopenfile for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 63385
diff changeset
671
f6dfd6c563e1 Implement #7566 - os.path.sameopenfile for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 63385
diff changeset
672 def sameopenfile(f1, f2):
f6dfd6c563e1 Implement #7566 - os.path.sameopenfile for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 63385
diff changeset
673 """Test whether two file objects reference the same file"""
64918
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
674 return _getfileinformation(f1) == _getfileinformation(f2)
70713
88e318166eaf Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
Brian Curtin <brian@python.org>
parents: 69876
diff changeset
675
88e318166eaf Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
Brian Curtin <brian@python.org>
parents: 69876
diff changeset
676
88e318166eaf Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
Brian Curtin <brian@python.org>
parents: 69876
diff changeset
677 try:
88e318166eaf Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
Brian Curtin <brian@python.org>
parents: 69876
diff changeset
678 # The genericpath.isdir implementation uses os.stat and checks the mode
88e318166eaf Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
Brian Curtin <brian@python.org>
parents: 69876
diff changeset
679 # attribute to tell whether or not the path is a directory.
88e318166eaf Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
Brian Curtin <brian@python.org>
parents: 69876
diff changeset
680 # This is overkill on Windows - just pass the path to GetFileAttributes
88e318166eaf Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
Brian Curtin <brian@python.org>
parents: 69876
diff changeset
681 # and check the attribute from there.
70736
d40609dd01e0 Correction to 88e318166eaf - Issue #11583
Brian Curtin <brian@python.org>
parents: 70713
diff changeset
682 from nt import _isdir as isdir
70713
88e318166eaf Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
Brian Curtin <brian@python.org>
parents: 69876
diff changeset
683 except ImportError:
70736
d40609dd01e0 Correction to 88e318166eaf - Issue #11583
Brian Curtin <brian@python.org>
parents: 70713
diff changeset
684 # Use genericpath.isdir as imported above.
d40609dd01e0 Correction to 88e318166eaf - Issue #11583
Brian Curtin <brian@python.org>
parents: 70713
diff changeset
685 pass