annotate Lib/ntpath.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents f283589cb71e
children
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: 40649
diff changeset
11 import genericpath
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents: 37908
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",
81065
cc3f7ef1072e Fix #16788. Add samestat to Lib/ntpath.py
Brian Curtin <brian@python.org>
parents: 81062
diff changeset
20 "samefile", "sameopenfile", "samestat",]
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'
32302
e196b823e6c5 Feature request #935915: Add os.path.devnull.
Martin v. Löwis <martin@v.loewis.de>
parents: 30723
diff changeset
33 devnull = 'nul'
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
34
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
35 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
36 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
37 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
38 else:
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
39 return ''
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
40
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
41 def _get_sep(path):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
42 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
43 return b'\\'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
44 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
45 return '\\'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
46
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
47 def _get_altsep(path):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
48 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
49 return b'/'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
50 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
51 return '/'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
52
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
53 def _get_bothseps(path):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
54 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
55 return b'\\/'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
56 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
57 return '\\/'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
58
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
59 def _get_dot(path):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
60 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
61 return b'.'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
62 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
63 return '.'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
64
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
65 def _get_colon(path):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
66 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
67 return b':'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
68 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
69 return ':'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
70
63446
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
71 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
72 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
73 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
74 else:
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
75 return ('\\\\.\\', '\\\\?\\')
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
76
5976
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
77 # 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
78 # 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
79 # (this is done by normpath).
5976
5b2c595428fa Rewrite normcase() using string.translate...
Guido van Rossum <guido@python.org>
parents: 5703
diff changeset
80
7351
1baca5d2ba57 (1) Change normpath() to *not* also call normcase().
Guido van Rossum <guido@python.org>
parents: 7123
diff changeset
81 def normcase(s):
1baca5d2ba57 (1) Change normpath() to *not* also call normcase().
Guido van Rossum <guido@python.org>
parents: 7123
diff changeset
82 """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
83
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
84 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
85 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
86 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
87 "not '{}'".format(s.__class__.__name__))
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
88 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
89
5703
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
90
11804
57788da16488 Typo in a comment: "wheter" --> "whether"
Fred Drake <fdrake@acm.org>
parents: 11693
diff changeset
91 # 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
92 # 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
93 # 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
94 # 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
95 # 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
96
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 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
98 """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
99 s = splitdrive(s)[1]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
100 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
101
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
5703
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
103 # Join two (or more) paths.
03dd1ded5cb3 Support $HOME in expanduser().
Guido van Rossum <guido@python.org>
parents: 5136
diff changeset
104
5133
666152b77027 join(): join one or more path components
Barry Warsaw <barry@python.org>
parents: 5027
diff changeset
105 def join(a, *p):
42775
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41554
diff changeset
106 """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
107 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
108 will be discarded."""
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
109 sep = _get_sep(a)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
110 seps = _get_bothseps(a)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
111 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
112 path = a
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
113 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
114 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
115 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
116 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
117
e1464143c463 One more crack at join(): stop trying to pretend this isn't a mass of
Tim Peters <tim.peters@gmail.com>
parents: 18846
diff changeset
118 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
119 # 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
120 # 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
121 # (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
122 # 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
123 # 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
124 # 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
125 # 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
126 # 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
127 # 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
128 # 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
129 # 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
130 # 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
131 # 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
132 # 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
133 # 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
134 # 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
135 # 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
136 # 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
137 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
138
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
139 # 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
140 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
141 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
142 else:
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
143 # 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
144 # 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
145 # 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
146 # (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
147 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
148
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
149 # 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
150 # 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
151 # (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
152 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
153 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
154
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
155 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
156 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
157 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
158 # 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
159 assert len(path) > 0
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
160 if path[-1:] in seps:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
161 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
162 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
163 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
164 path += b
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
165 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
166 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
167 elif b:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
168 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
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 else:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
171 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
172 else:
0c17515b05db SF bug 478425: Change in os.path.join (ntpath.py)
Tim Peters <tim.peters@gmail.com>
parents: 20361
diff changeset
173 # 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
174 # 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
175 # ('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
176 # this case.
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
177 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
178
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
179 return path
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
180
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 # 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
183 # 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
184 # 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
185 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
186 """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
187 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
188
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
189 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
190 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
191 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
192 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
193
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
194 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
195 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
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 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
198 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
199 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
200
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
201 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
202
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 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
205 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
206 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
207 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
208 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
209 # 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
210 # 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
211 # \\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
212 # directory ^^^^^^^^^^^^^^^
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
213 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
214 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
215 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
216 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
217 # 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
218 # (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
219 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
220 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
221 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
222 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
223 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
224 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
225 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
226 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
227
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
228
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
229 # Parse UNC paths
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
230 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
231 """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
232 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
233
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
234 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
235
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
236 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
237 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
238 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
239 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
240 """
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
241 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
242 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
243 DeprecationWarning)
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
244 sep = _get_sep(p)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
245 if not p[1:2]:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
246 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
247 firstTwo = p[0:2]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
248 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
249 # 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
250 # 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
251 # \\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
252 # directory ^^^^^^^^^^^^^^^
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
253 normp = normcase(p)
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
254 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
255 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
256 ##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
257 return (p[:0], p)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
258 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
259 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
260 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
261 return p[:index], p[index:]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
262 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
263
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
264
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 # 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
266 # 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
267 # 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
268 # 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
269
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 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
271 """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
272
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
273 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
274 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
275
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
276 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
277 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
278 # 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
279 i = len(p)
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
280 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
281 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
282 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
283 # 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
284 head2 = head
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
285 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
286 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
287 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
288 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
289
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
290
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 # 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
292 # 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
293 # 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
294 # 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
295
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 def splitext(p):
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
297 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
298 _get_dot(p))
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
299 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
300
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
301
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 # 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
303
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 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
305 """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
306 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
307
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
308
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 # 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
310
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 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
312 """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
313 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
314
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 # 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
316 # 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
317
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 def islink(path):
62874
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
319 """Test whether a path is a symbolic link.
79479
d853354e1470 #16135: Removal of OS/2 support (Remove OS2 and OS/2 references)
Jesus Cea <jcea@jcea.es>
parents: 70736
diff changeset
320 This will always return false for Windows prior to 6.0.
39689
d0ed7dbaad56 - Move functions common to all path modules into genericpath.py and have the
Jack Diederich <jackdied@gmail.com>
parents: 37908
diff changeset
321 """
62874
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
322 try:
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
323 st = os.lstat(path)
80932
47f98a550d42 Issue #16706: get rid of os.error
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 79479
diff changeset
324 except (OSError, AttributeError):
62874
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
325 return False
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
326 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
327
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
328 # 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
329
62874
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
330 def lexists(path):
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
331 """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
332 try:
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
333 st = os.lstat(path)
80944
bb94f6222fef Issue #16719: Get rid of WindowsError. Use OSError instead
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80932
diff changeset
334 except OSError:
62874
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
335 return False
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
336 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
337
84945
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
338 # Is a path a mount point?
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
339 # Any drive letter root (eg c:\)
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
340 # Any share UNC (eg \\server\share)
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
341 # Any volume mounted on a filesystem folder
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
342 #
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
343 # No one method detects all three situations. Historically we've lexically
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
344 # detected drive letter roots and share UNCs. The canonical approach to
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
345 # detecting mounted volumes (querying the reparse tag) fails for the most
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
346 # common case: drive letter roots. The alternative which uses GetVolumePathName
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
347 # fails if the drive letter is the result of a SUBST.
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
348 try:
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
349 from nt import _getvolumepathname
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
350 except ImportError:
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
351 _getvolumepathname = None
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 def ismount(path):
84945
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
353 """Test whether a path is a mount point (a drive root, the root of a
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
354 share, or a mounted volume)"""
52403
46a45eccc9f7 Merged revisions 70676 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49412
diff changeset
355 seps = _get_bothseps(path)
84945
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
356 path = abspath(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
357 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
358 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
359 return (not rest) or (rest in seps)
84945
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
360 if rest in seps:
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
361 return True
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
362
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
363 if _getvolumepathname:
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
364 return path.rstrip(seps) == _getvolumepathname(path).rstrip(seps)
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
365 else:
f283589cb71e Issue #9035: os.path.ismount now recognises volumes mounted below
Tim Golden <mail@timgolden.me.uk>
parents: 84442
diff changeset
366 return False
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
367
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
368
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 # 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
370 # '~' 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
371 # 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
372 # 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
373 # 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
374 # 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
375 # (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
376 # variable expansion.)
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
377
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 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
379 """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
380
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
381 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
382 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
383 tilde = b'~'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
384 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
385 tilde = '~'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
386 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
387 return path
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
388 i, n = 1, len(path)
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
389 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
390 i += 1
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
391
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
392 if 'HOME' in os.environ:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
393 userhome = os.environ['HOME']
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
394 elif 'USERPROFILE' in os.environ:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
395 userhome = os.environ['USERPROFILE']
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
396 elif not 'HOMEPATH' in os.environ:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
397 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
398 else:
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
399 try:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
400 drive = os.environ['HOMEDRIVE']
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
401 except KeyError:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
402 drive = ''
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
403 userhome = join(drive, os.environ['HOMEPATH'])
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
404
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
405 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
406 userhome = userhome.encode(sys.getfilesystemencoding())
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
407
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
408 if i != 1: #~user
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
409 userhome = join(dirname(userhome), path[1:i])
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
410
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
411 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
412
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
413
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 # 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
415 # 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
416 # - no expansion within single quotes
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
417 # - '$$' is translated into '$'
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
418 # - '%%' 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
419 # - ${varname} is accepted.
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
420 # - $varname is accepted.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
421 # - %varname% is accepted.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
422 # - 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
423 # (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
424 # 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
425 # XXX except '^|<>='.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
426
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
427 def expandvars(path):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
428 """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
429
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
430 Unknown variables are left unchanged."""
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
431 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
432 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
433 return path
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
434 import string
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
435 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
436 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
437 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
438 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
439 dollar = b'$'
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
440 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
441 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
442 return path
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
443 import string
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
444 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
445 quote = '\''
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
446 percent = '%'
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
447 brace = '{'
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
448 dollar = '$'
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
449 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
450 index = 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
451 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
452 while index < pathlen:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
453 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
454 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
455 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
456 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
457 try:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
458 index = path.index(c)
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
459 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
460 except ValueError:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
461 res += path
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
462 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
463 elif c == percent: # variable or '%'
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
464 if path[index + 1:index + 2] == percent:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
465 res += c
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
466 index += 1
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
467 else:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
468 path = path[index+1:]
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
469 pathlen = len(path)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
470 try:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
471 index = path.index(percent)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
472 except ValueError:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
473 res += percent + path
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
474 index = pathlen - 1
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
475 else:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
476 var = path[:index]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
477 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
478 var = var.decode('ascii')
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
479 if var in os.environ:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
480 value = os.environ[var]
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
481 else:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
482 value = '%' + var + '%'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
483 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
484 value = value.encode('ascii')
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
485 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
486 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
487 if path[index + 1:index + 2] == dollar:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
488 res += c
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
489 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
490 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
491 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
492 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
493 try:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
494 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
495 index = path.index(b'}')
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
496 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
497 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
498 var = path[:index]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
499 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
500 var = var.decode('ascii')
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
501 if var in os.environ:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
502 value = os.environ[var]
40649
a4cdb9c8af25 Fixed ntpath.expandvars to not replace references to non-existing
Sjoerd Mullender <sjoerd@acm.org>
parents: 39689
diff changeset
503 else:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
504 value = '${' + var + '}'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
505 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
506 value = value.encode('ascii')
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
507 res += value
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
508 except ValueError:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
509 if isinstance(path, bytes):
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
510 res += b'${' + path
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
511 else:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
512 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
513 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
514 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
515 var = ''
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
516 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
517 c = path[index:index + 1]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
518 while c and c in varchars:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
519 if isinstance(path, bytes):
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
520 var += c.decode('ascii')
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
521 else:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
522 var += c
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
523 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
524 c = path[index:index + 1]
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
525 if var in os.environ:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
526 value = os.environ[var]
40649
a4cdb9c8af25 Fixed ntpath.expandvars to not replace references to non-existing
Sjoerd Mullender <sjoerd@acm.org>
parents: 39689
diff changeset
527 else:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
528 value = '$' + var
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
529 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
530 value = value.encode('ascii')
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
531 res += value
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
532 if c:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
533 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
534 else:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
535 res += c
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
536 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
537 return res
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
538
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
539
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
540 # 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
541 # 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
542 # 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
543
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 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
545 """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
546 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
547 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
548 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
549 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
550 # 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
551 # \\.\ -> device names
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
552 # \\?\ -> literal paths
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
553 # 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
554 return path
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
555 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
556 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
557
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
558 # 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
559 if path.startswith(sep):
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
560 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
561 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
562
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
563 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
564 i = 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
565 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
566 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
567 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
568 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
569 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
570 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
571 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
572 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
573 del comps[i]
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
574 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
575 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
576 else:
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
577 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
578 # 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
579 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
580 comps.append(_get_dot(path))
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
581 return prefix + sep.join(comps)
10018
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
582
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
583
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
584 # Return an absolute path.
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
585 try:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
586 from nt import _getfullpathname
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
587
84442
7769c4d72806 Issue #18200: Back out usage of ModuleNotFoundError (8d28d44f3a9a)
Brett Cannon <brett@python.org>
parents: 84114
diff changeset
588 except ImportError: # not running on Windows - mock up something sensible
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
589 def abspath(path):
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
590 """Return the absolute version of a path."""
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
591 if not isabs(path):
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
592 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
593 cwd = os.getcwdb()
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
594 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
595 cwd = os.getcwd()
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
596 path = join(cwd, path)
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
597 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
598
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
599 else: # use native Windows method on Windows
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
600 def abspath(path):
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
601 """Return the absolute version of a path."""
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
602
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
603 if path: # Empty path must return current working directory.
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
604 try:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
605 path = _getfullpathname(path)
80944
bb94f6222fef Issue #16719: Get rid of WindowsError. Use OSError instead
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80932
diff changeset
606 except OSError:
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
607 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
608 elif isinstance(path, bytes):
e63d4678fca7 Note: only the relevant parts of r79474 are merged.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 53838
diff changeset
609 path = os.getcwdb()
37908
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
610 else:
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
611 path = os.getcwd()
c7ef92b9045c SF bug #1473760 TempFile can hang on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 35376
diff changeset
612 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
613
f2f25a738f7f SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents: 19628
diff changeset
614 # 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
615 realpath = abspath
25666
4a825b902310 Add os.path.supports_unicode_filenames for all platforms,
Mark Hammond <mhammond@skippinet.com.au>
parents: 23625
diff changeset
616 # 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
617 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
618 sys.getwindowsversion()[3] >= 2)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
619
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
620 def relpath(path, start=curdir):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
621 """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
622 sep = _get_sep(path)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
623
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
624 if start is curdir:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
625 start = _get_dot(path)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
626
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
627 if not path:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
628 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
629
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
630 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
631 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
632 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
633 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
634 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
635 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
636 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
637 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
638
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
639 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
640 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: 40649
diff changeset
641 # 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
642 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
643 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
644 if normcase(e1) != normcase(e2):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
645 break
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
646 i += 1
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
647
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
648 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
649 pardir = b'..'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
650 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
651 pardir = '..'
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
652 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
653 if not rel_list:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
654 return _get_dot(path)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40649
diff changeset
655 return join(*rel_list)
62874
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
656
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
657
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
658 # 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
659 try:
64922
aa56ebb8fe94 Fix #9790 again. Rather than handle NotImplementedError at runtime as
Brian Curtin <brian.curtin@gmail.com>
parents: 64918
diff changeset
660 # 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
661 # 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
662 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
663 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
664 else:
aa56ebb8fe94 Fix #9790 again. Rather than handle NotImplementedError at runtime as
Brian Curtin <brian.curtin@gmail.com>
parents: 64918
diff changeset
665 raise ImportError
aa56ebb8fe94 Fix #9790 again. Rather than handle NotImplementedError at runtime as
Brian Curtin <brian.curtin@gmail.com>
parents: 64918
diff changeset
666 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
667 # 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
668 # 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
669 # 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
670 # approximation.
64918
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
671 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
672 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
673
70713
88e318166eaf Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
Brian Curtin <brian@python.org>
parents: 69876
diff changeset
674
88e318166eaf Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
Brian Curtin <brian@python.org>
parents: 69876
diff changeset
675 try:
88e318166eaf Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
Brian Curtin <brian@python.org>
parents: 69876
diff changeset
676 # 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
677 # 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
678 # 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
679 # and check the attribute from there.
70736
d40609dd01e0 Correction to 88e318166eaf - Issue #11583
Brian Curtin <brian@python.org>
parents: 70713
diff changeset
680 from nt import _isdir as isdir
84442
7769c4d72806 Issue #18200: Back out usage of ModuleNotFoundError (8d28d44f3a9a)
Brett Cannon <brett@python.org>
parents: 84114
diff changeset
681 except ImportError:
70736
d40609dd01e0 Correction to 88e318166eaf - Issue #11583
Brian Curtin <brian@python.org>
parents: 70713
diff changeset
682 # Use genericpath.isdir as imported above.
d40609dd01e0 Correction to 88e318166eaf - Issue #11583
Brian Curtin <brian@python.org>
parents: 70713
diff changeset
683 pass