annotate Lib/ntpath.py @ 79691:05c8928362ee

Merge __next__ method link fixes with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 12 Oct 2012 13:46:37 +0300
parents d853354e1470
children 47f98a550d42
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
1 # Module 'ntpath' -- common operations on WinNT/Win95 pathnames
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
2 """Common pathname manipulations, WindowsNT/95 version.
10093
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
3
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
4 Instead of importing this module directly, import os and refer to this
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
5 module as os.path.
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
6 """
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 import os
45471
7dd2c1746267 Merged revisions 60990-61002 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44535
diff changeset
9 import sys
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 import stat
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
11 import genericpath
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
12 from genericpath import *
13581
e871bd05c6a7 forgot to change copy.copy(m) to m[:]
Skip Montanaro <skip@pobox.com>
parents: 13577
diff changeset
13
16460
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
14 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
c18ac8c2f036 added several more __all__ lists
Skip Montanaro <skip@pobox.com>
parents: 15870
diff changeset
15 "basename","dirname","commonprefix","getsize","getmtime",
35376
e7ed0e6aed2f Bug #1266283: lexists() is not exported from os.path
Georg Brandl <georg@python.org>
parents: 35308
diff changeset
16 "getatime","getctime", "islink","exists","lexists","isdir","isfile",
46770
8bfe2b06527d Remove os.path.walk
Benjamin Peterson <benjamin@python.org>
parents: 45471
diff changeset
17 "ismount", "expanduser","expandvars","normpath","abspath",
35376
e7ed0e6aed2f Bug #1266283: lexists() is not exported from os.path
Georg Brandl <georg@python.org>
parents: 35308
diff changeset
18 "splitunc","curdir","pardir","sep","pathsep","defpath","altsep",
62874
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
19 "extsep","devnull","realpath","supports_unicode_filenames","relpath",
64495
f6dfd6c563e1 Implement #7566 - os.path.sameopenfile for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 63385
diff changeset
20 "samefile", "sameopenfile",]
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
21
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
22 # strings representing various path-related bits and pieces
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
23 # These are primarily for export; internally, they are hardcoded.
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
24 curdir = '.'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
25 pardir = '..'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
26 extsep = '.'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
27 sep = '\\'
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
28 pathsep = ';'
28427
9d3a25e99075 make nt altsep forward slash - closes bug 709428
Skip Montanaro <skip@pobox.com>
parents: 27908
diff changeset
29 altsep = '/'
27908
47b8cabab7b1 Tweak to Skip's checkin of patch 686397:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 27897
diff changeset
30 defpath = '.;C:\\bin'
27897
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
31 if 'ce' in sys.builtin_module_names:
cf1efca82b18 Migrate definitions of several platform-dependent path-related variables
Skip Montanaro <skip@pobox.com>
parents: 27056
diff changeset
32 defpath = '\\Windows'
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: 40737
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.
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 38453
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)
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
324 except (os.error, AttributeError):
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)
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
334 except (os.error, WindowsError):
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
10523
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
338 # Is a path a mount point? Either a root (with or without drive letter)
474c5b82d7de Withdraw the UNC support from splitdrive(). Instead, a new function
Guido van Rossum <guido@python.org>
parents: 10433
diff changeset
339 # or an UNC path with at most a / or \ after the mount point.
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
340
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 def ismount(path):
7123
5d517ddcbd2e Fix to ismount(). Can't remember who told me this.
Guido van Rossum <guido@python.org>
parents: 6756
diff changeset
342 """Test whether a path is a mount point (defined as root of drive)"""
52403
46a45eccc9f7 Merged revisions 70676 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49412
diff changeset
343 seps = _get_bothseps(path)
53838
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
344 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
345 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
346 return (not rest) or (rest in seps)
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
347 return rest in seps
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
348
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
349
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 # 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
351 # '~' 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
352 # 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
353 # 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
354 # 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
355 # 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
356 # (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
357 # variable expansion.)
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
358
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 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
360 """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
361
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
362 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
363 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
364 tilde = b'~'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
365 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
366 tilde = '~'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
367 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
368 return path
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
369 i, n = 1, len(path)
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
370 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
371 i += 1
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
372
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
373 if 'HOME' in os.environ:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
374 userhome = os.environ['HOME']
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
375 elif 'USERPROFILE' in os.environ:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
376 userhome = os.environ['USERPROFILE']
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
377 elif not 'HOMEPATH' in os.environ:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
378 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
379 else:
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
380 try:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
381 drive = os.environ['HOMEDRIVE']
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
382 except KeyError:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
383 drive = ''
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
384 userhome = join(drive, os.environ['HOMEPATH'])
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
385
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
386 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
387 userhome = userhome.encode(sys.getfilesystemencoding())
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
388
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
389 if i != 1: #~user
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
390 userhome = join(dirname(userhome), path[1:i])
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
391
6756
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
392 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
393
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
394
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 # 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
396 # 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
397 # - no expansion within single quotes
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
398 # - '$$' is translated into '$'
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
399 # - '%%' 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
400 # - ${varname} is accepted.
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
401 # - $varname is accepted.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
402 # - %varname% is accepted.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
403 # - 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
404 # (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
405 # 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
406 # XXX except '^|<>='.
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
407
15870
010d03853546 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
408 def expandvars(path):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
409 """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
410
d8c4378eb87d New splitdrive() that knows about UNC paths (e.g., network paths like
Guido van Rossum <guido@python.org>
parents: 10034
diff changeset
411 Unknown variables are left unchanged."""
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
412 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
413 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
414 return path
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
415 import string
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
416 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
417 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
418 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
419 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
420 dollar = b'$'
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
421 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
422 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
423 return path
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
424 import string
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
425 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
426 quote = '\''
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
427 percent = '%'
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
428 brace = '{'
84ccf91a06cc Issue3187 again: test_ntpath failed when run with the -bb option
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49410
diff changeset
429 dollar = '$'
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
430 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
431 index = 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
432 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
433 while index < pathlen:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
434 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
435 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
436 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
437 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
438 try:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
439 index = path.index(c)
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
440 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
441 except ValueError:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
442 res += path
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
443 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
444 elif c == percent: # variable or '%'
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
445 if path[index + 1:index + 2] == percent:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
446 res += c
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
447 index += 1
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
448 else:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
449 path = path[index+1:]
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
450 pathlen = len(path)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
451 try:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
452 index = path.index(percent)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
453 except ValueError:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
454 res += percent + path
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
455 index = pathlen - 1
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
456 else:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
457 var = path[:index]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
458 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
459 var = var.decode('ascii')
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
460 if var in os.environ:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
461 value = os.environ[var]
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
462 else:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
463 value = '%' + var + '%'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
464 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
465 value = value.encode('ascii')
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
466 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
467 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
468 if path[index + 1:index + 2] == dollar:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
469 res += c
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
470 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
471 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
472 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
473 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
474 try:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
475 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
476 index = path.index(b'}')
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
477 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
478 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
479 var = path[:index]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
480 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
481 var = var.decode('ascii')
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
482 if var in os.environ:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
483 value = os.environ[var]
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
484 else:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
485 value = '${' + var + '}'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
486 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
487 value = value.encode('ascii')
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
488 res += value
14910
6fb2af766892 Avoid import of string module; it is only needed for expandvars().
Fred Drake <fdrake@acm.org>
parents: 14694
diff changeset
489 except ValueError:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
490 if isinstance(path, bytes):
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
491 res += b'${' + path
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
492 else:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
493 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
494 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
495 else:
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
496 var = ''
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
497 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
498 c = path[index:index + 1]
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
499 while c and c in varchars:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
500 if isinstance(path, bytes):
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
501 var += c.decode('ascii')
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
502 else:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
503 var += c
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
504 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
505 c = path[index:index + 1]
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22867
diff changeset
506 if var in os.environ:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
507 value = os.environ[var]
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
508 else:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
509 value = '$' + var
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
510 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
511 value = value.encode('ascii')
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
512 res += value
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
513 if c:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
514 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
515 else:
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
516 res += c
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
517 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
518 return res
1561
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
519
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
520
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
521 # 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
522 # 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
523 # 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
524
8aabe9aae0fa Added ntpath.py (for os.py when used with Windows NT)
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 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
526 """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
527 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
528 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
529 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
530 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
531 # 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
532 # \\.\ -> device names
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
533 # \\?\ -> literal paths
0dcae455ec07 Merged revisions 83352,83355-83358,83362,83366,83368-83369 via svnmerge from
Georg Brandl <georg@python.org>
parents: 60063
diff changeset
534 # 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
535 return path
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
536 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
537 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
538
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
539 # 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
540 if path.startswith(sep):
63488
228a0ef23a7b Merged revisions 83065 via svnmerge from
Georg Brandl <georg@python.org>
parents: 63446
diff changeset
541 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
542 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
543
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
544 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
545 i = 0
e4070dc6ac35 Added doc strings and reindented according to new standard, without tabs.
Guido van Rossum <guido@python.org>
parents: 5976
diff changeset
546 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
547 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
548 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
549 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
550 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
551 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
552 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
553 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
554 del comps[i]
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
555 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
556 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
557 else:
19628
ef2cf9943acd SF bug #456621: normpath on Win32 not collapsing c:\\..
Tim Peters <tim.peters@gmail.com>
parents: 18847
diff changeset
558 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
559 # 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
560 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
561 comps.append(_get_dot(path))
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
562 return prefix + sep.join(comps)
10018
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
563
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
564
d7979f4ed2b6 Add abspath()
Guido van Rossum <guido@python.org>
parents: 8746
diff changeset
565 # Return an absolute path.
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
566 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
567 from nt import _getfullpathname
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
568
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
569 except ImportError: # not running on Windows - mock up something sensible
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
570 def abspath(path):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
571 """Return the absolute version of a path."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
572 if not isabs(path):
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
573 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
574 cwd = os.getcwdb()
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
575 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
576 cwd = os.getcwd()
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
577 path = join(cwd, path)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
578 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
579
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
580 else: # use native Windows method on Windows
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
581 def abspath(path):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
582 """Return the absolute version of a path."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
583
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
584 if path: # Empty path must return current working directory.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
585 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
586 path = _getfullpathname(path)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
587 except WindowsError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
588 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
589 elif isinstance(path, bytes):
e63d4678fca7 Note: only the relevant parts of r79474 are merged.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 53838
diff changeset
590 path = os.getcwdb()
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
591 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
592 path = os.getcwd()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 35376
diff changeset
593 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
594
f2f25a738f7f SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
Guido van Rossum <guido@python.org>
parents: 19628
diff changeset
595 # 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
596 realpath = abspath
25666
4a825b902310 Add os.path.supports_unicode_filenames for all platforms,
Mark Hammond <mhammond@skippinet.com.au>
parents: 23625
diff changeset
597 # 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
598 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
599 sys.getwindowsversion()[3] >= 2)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
600
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
601 def relpath(path, start=curdir):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
602 """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
603 sep = _get_sep(path)
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
604
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
605 if start is curdir:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
606 start = _get_dot(path)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
607
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
608 if not path:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
609 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
610
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
611 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
612 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
613 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
614 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
615 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
616 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
617 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
618 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
619
29025b530046 Issue #5799: ntpath (ie, os.path on Windows) fully supports UNC pathnames.
Mark Hammond <mhammond@skippinet.com.au>
parents: 52403
diff changeset
620 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
621 path_list = [x for x in path_rest.split(sep) if x]
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
622 # 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
623 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
624 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
625 if normcase(e1) != normcase(e2):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
626 break
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
627 i += 1
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
628
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
629 if isinstance(path, bytes):
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
630 pardir = b'..'
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
631 else:
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
632 pardir = '..'
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
633 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
634 if not rel_list:
49410
ef4bb4d5dfde Second part of #3187, for windows:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 46770
diff changeset
635 return _get_dot(path)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40737
diff changeset
636 return join(*rel_list)
62874
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
637
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
638
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
639 # 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
640 try:
64922
aa56ebb8fe94 Fix #9790 again. Rather than handle NotImplementedError at runtime as
Brian Curtin <brian.curtin@gmail.com>
parents: 64918
diff changeset
641 # 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
642 # 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
643 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
644 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
645 else:
aa56ebb8fe94 Fix #9790 again. Rather than handle NotImplementedError at runtime as
Brian Curtin <brian.curtin@gmail.com>
parents: 64918
diff changeset
646 raise ImportError
aa56ebb8fe94 Fix #9790 again. Rather than handle NotImplementedError at runtime as
Brian Curtin <brian.curtin@gmail.com>
parents: 64918
diff changeset
647 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
648 # 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
649 # 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
650 # 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
651 # approximation.
64918
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
652 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
653 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
654
62874
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
655 def samefile(f1, f2):
730b728e5aef Implement #1578269. Patch by Jason R. Coombs.
Brian Curtin <brian.curtin@gmail.com>
parents: 62490
diff changeset
656 "Test whether two pathnames reference the same actual file"
64918
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
657 return _getfinalpathname(f1) == _getfinalpathname(f2)
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
658
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
659
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
660 try:
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
661 from nt import _getfileinformation
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
662 except ImportError:
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
663 # On other operating systems, just return the fd and see that
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
664 # it compares equal in sameopenfile.
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
665 def _getfileinformation(fd):
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
666 return fd
64495
f6dfd6c563e1 Implement #7566 - os.path.sameopenfile for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 63385
diff changeset
667
f6dfd6c563e1 Implement #7566 - os.path.sameopenfile for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 63385
diff changeset
668 def sameopenfile(f1, f2):
f6dfd6c563e1 Implement #7566 - os.path.sameopenfile for Windows.
Brian Curtin <brian.curtin@gmail.com>
parents: 63385
diff changeset
669 """Test whether two file objects reference the same file"""
64918
28d41d34a2e6 Fix #9790: Rework the imports necessary for ntpath.samefile and
Brian Curtin <brian.curtin@gmail.com>
parents: 64498
diff changeset
670 return _getfileinformation(f1) == _getfileinformation(f2)
70713
88e318166eaf Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
Brian Curtin <brian@python.org>
parents: 69876
diff changeset
671
88e318166eaf Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
Brian Curtin <brian@python.org>
parents: 69876
diff changeset
672
88e318166eaf Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
Brian Curtin <brian@python.org>
parents: 69876
diff changeset
673 try:
88e318166eaf Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
Brian Curtin <brian@python.org>
parents: 69876
diff changeset
674 # 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
675 # 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
676 # 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
677 # and check the attribute from there.
70736
d40609dd01e0 Correction to 88e318166eaf - Issue #11583
Brian Curtin <brian@python.org>
parents: 70713
diff changeset
678 from nt import _isdir as isdir
70713
88e318166eaf Fix #11583. Changed os.path.isdir to use GetFileAttributes instead of os.stat.
Brian Curtin <brian@python.org>
parents: 69876
diff changeset
679 except ImportError:
70736
d40609dd01e0 Correction to 88e318166eaf - Issue #11583
Brian Curtin <brian@python.org>
parents: 70713
diff changeset
680 # Use genericpath.isdir as imported above.
d40609dd01e0 Correction to 88e318166eaf - Issue #11583
Brian Curtin <brian@python.org>
parents: 70713
diff changeset
681 pass