8000 Make requested changes · python/cpython@d4aef86 · GitHub
[go: up one dir, main page]

Skip to content

Commit d4aef86

Browse files
committed
Make requested changes
1 parent e9cc0c9 commit d4aef86

File tree

4 files changed

+48
-44
lines changed

4 files changed

+48
-44
lines changed

Lib/ntpath.py

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,22 @@ def splitdrive(p):
165165
Paths cannot contain both a drive letter and a UNC path.
166166
167167
"""
168+
drive, root, tail = splitroot(p)
169+
return drive, root + tail
170+
171+
172+
def splitroot(p):
173+
"""Split a pathname into drive, root and tail. The drive is defined
174+
exactly as in splitdrive(). On Windows, the root may be a single path
175+
separator or an empty string. The tail contains anything after the root.
176+
For example:
177+
178+
splitroot('//server/share/') == ('//server/share', '/', '')
179+
splitroot('C:/Users/Barney') == ('C:', '/', 'Users/Barney')
180+
splitroot('C:///spam///ham') == ('C:', '/', '//spam///ham')
181+
splitroot('Windows/notepad') == ('', '', 'Windows/notepad')
182+
"""
183+
# Split drive
168184
p = os.fspath(p)
169185
if isinstance(p, bytes):
170186
sep = b'\\'
@@ -180,41 +196,27 @@ def splitdrive(p):
180196
if normp[:1] != sep:
181197
if normp[1:2] == colon:
182198
# Drive-letter drives, e.g. X:
183-
return p[:2], p[2:]
184-
elif normp[1:2] == sep:
199+
drive, p = p[:2], p[2:]
200+
else:
201+
drive = p[:0]
202+
elif normp[1:2] != sep:
203+
drive = p[:0]
204+
else:
185205
# UNC drives, e.g. \\server\share or \\?\UNC\server\share
186206
# Device drives, e.g. \\.\device or \\?\device
187207
start = 8 if normp[:8].upper() == unc_prefix else 2
188208
index = normp.find(sep, start)
189209
if index == -1:
190-
return p, p[:0]
191-
index2 = normp.find(sep, index + 1)
192-
if index2 == -1:
193-
return p, p[:0]
194-
return p[:index2], p[index2:]
195-
return p[:0], p
196-
197-
198-
def splitroot(p):
199-
"""Split a pathname into drive, root and tail. The drive is defined
200-
exactly as in splitdrive(). On Windows, the root may be a single path
201-
separator or an empty string. The tail contains anything after the root.
202-
For example:
210+
drive, p = p, p[:0]
211+
else:
212+
index2 = normp.find(sep, index + 1)
213+
if index2 == -1:
214+
drive, p = p, p[:0]
215+
else:
216+
drive, p = p[:index2], p[index2:]
203217

204-
splitroot('//server/share/') == ('//server/share', '/', '')
205-
splitroot('C:/Users/Barney') == ('C:', '/', 'Users/Barney')
206-
splitroot('C:///spam///ham') == ('C:', '/', '//spam///ham')
207-
splitroot('Windows/notepad') == ('', '', 'Windows/notepad')
208-
"""
209-
drive, p = splitdrive(p)
210-
if isinstance(p, bytes):
211-
sep = b'\\'
212-
altsep = b'/'
213-
else:
214-
sep = ' E864 \\'
215-
altsep = '/'
216-
normp = p.replace(altsep, sep)
217-
if normp[:1] == sep:
218+
# Split root
219+
if normp[len(drive):len(drive)+1] == sep:
218220
# Absolute path, e.g. X:\Windows
219221
return drive, p[:1], p[1:]
220222
# Relative path, e.g. X:Windows

Lib/posixpath.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"samefile","sameopenfile","samestat",
3636
"curdir","pardir","sep","pathsep","defpath","altsep","extsep",
3737
"devnull","realpath","supports_unicode_filenames","relpath",
38-
"commonpath", "isjunction","isreserved","isdevdrive"]
38+
"commonpath", "isjunction","isdevdrive"]
3939

4040

4141
def _get_sep(path):
@@ -168,14 +168,23 @@ def splitroot(p):
168168

169169
def basename(p):
170170
"""Returns the final component of a pathname"""
171-
return split(p)[1]
171+
p = os.fspath(p)
172+
sep = _get_sep(p)
173+
i = p.rfind(sep) + 1
174+
return p[i:]
172175

173176

174177
# Return the head (dirname) part of a path, same as split(path)[0].
175178

176179
def dirname(p):
177180
"""Returns the directory component of a pathname"""
178-
return split(p)[0]
181+
p = os.fspath(p)
182+
sep = _get_sep(p)
183+
i = p.rfind(sep) + 1
184+
head = p[:i]
185+
if head and head != sep*len(head):
186+
head = head.rstrip(sep)
187+
return head
179188

180189

181190
# Is a path a junction?
@@ -187,12 +196,6 @@ def isjunction(path):
187196
return False
188197

189198

190-
def isreserved(path):
191-
"""Return true if the pathname is reserved by the system.
192-
Always returns False on posix"""
193-
return False
194-
195-
196199
def isdevdrive(path):
197200
"""Determines whether the specified path is on a Dev Drive.
198201
Dev Drives are not a part of posix semantics"""

Lib/test/test_ntpath.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,6 @@ def test_splitdrive(self):
124124
tester('ntpath.splitdrive("//?/UNC/server/share/dir")',
125125
("//?/UNC/server/share", "/dir"))
126126

127-
# gh-101363: match GetFullPathNameW() drive letter parsing behaviour
128-
tester('ntpath.splitdrive(" :/foo")', (" :", "/foo"))
129-
tester('ntpath.splitdrive("/:/foo")', ("", "/:/foo"))
130-
131-
132127
def test_splitroot(self):
133128
tester("ntpath.splitroot('')", ('', '', ''))
134129
tester("ntpath.splitroot('foo')", ('', '', 'foo'))
@@ -215,6 +210,10 @@ def test_splitroot(self):
215210
tester('ntpath.splitroot("//x")', ("//x", "", "")) # non-empty server & missing share
216211
tester('ntpath.splitroot("//x/")', ("//x/", "", "")) # non-empty server & empty share
217212

213+
# gh-101363: match GetFullPathNameW() drive letter parsing behaviour
214+
tester('ntpath.splitroot(" :/foo")', (" :", "/", "foo"))
215+
tester('ntpath.splitroot("/:/foo")', ("", "/", ":/foo"))
216+
218217
def test_split(self):
219218
tester('ntpath.split("c:\\foo\\bar")', ('c:\\foo', 'bar'))
220219
tester('ntpath.split("\\\\conky\\mountpoint\\foo\\bar")',
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Added posix implementation for isreserved & isdevdrive.
1+
Added posix implementation for isdevdrive.

0 commit comments

Comments
 (0)
0