8000 Remove start- & endswith · python/cpython@b853d4d · GitHub
[go: up one dir, main page]

Skip to content

Commit b853d4d

Browse files
committed
Remove start- & endswith
1 parent a4d9fcb commit b853d4d

File tree

2 files changed

+51
-48
lines changed

2 files changed

+51
-48
lines changed

Lib/ntpath.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def isreserved(path):
340340
def _isreservedname(name):
341341
"""Return true if the filename is reserved by the system."""
342342
# Trailing dots and spaces are reserved.
343-
if name.endswith(('.', ' ')) and name not in ('.', '..'):
343+
if name[-1:] in ('.', ' ') and name not in ('.', '..'):
344344
return True
345345
# Wildcards, separators, colon, and pipe (*?"<>/\:|) are reserved.
346346
# ASCII control characters (0-31) are reserved.
@@ -373,7 +373,7 @@ def expanduser(path):
373373
tilde = b'~'
374374
else:
375375
tilde = '~'
376-
if not path.startswith(tilde):
376+
if path[:1] != tilde:
377377
return path
378378
i, n = 1, len(path)
379379
while i < n and path[i] not in _get_bothseps(path):

Lib/posixpath.py

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def isabs(s):
6161
"""Test whether a path is absolute"""
6262
s = os.fspath(s)
6363
sep = _get_sep(s)
64-
return s.startswith(sep)
64+
return s[:1] == sep
6565

6666

6767
# Join pathnames.
@@ -80,9 +80,9 @@ def join(a, *p):
8080
if not p:
8181
path[:0] + sep #23780: Ensure compatible data type even if p is null.
8282
for b in map(os.fspath, p):
83-
if b.startswith(sep):
83+
if b[:1] == sep:
8484
path = b
85-
elif not path or path.endswith(sep):
85+
elif not path or path[-1:] == sep:
8686
path += b
8787
else:
8888
path += sep + b
@@ -229,7 +229,7 @@ def expanduser(path):
229229
tilde = b'~'
230230
else:
231231
tilde = '~'
232-
if not path.startswith(tilde):
232+
if path[:1] != tilde:
233233
return path
234234
sep = _get_sep(path)
235235
i = path.find(sep, 1)
@@ -311,7 +311,7 @@ def expandvars(path):
311311
break
312312
i, j = m.span(0)
313313
name = m.group(1)
314-
if name.startswith(start) and name.endswith(end):
314+
if name[:1] == start and name[-1:] == end:
315315
name = name[1:-1]
316316
try:
317317
if environ is None:
@@ -331,6 +331,34 @@ def expandvars(path):
331331
# It should be understood that this may change the meaning of the path
332332
# if it contains symbolic links!
333333

334+
def _normpath_fallback(path):
335+
"""Normalize path, eliminating double slashes, etc."""
336+
path = os.fspath(path)
337+
if isinstance(path, bytes):
338+
sep = b'/'
339+
curdir = b'.'
340+
pardir = b'..'
341+
else:
342+
sep = '/'
343+
curdir = '.'
344+
pardir = '..'
345+
if not path:
346+
return curdir
347+
_, root, tail = splitroot(path)
348+
comps = []
349+
for comp in tail.split(sep):
350+
if not comp or comp == curdir:
351+
continue
352+
if (
353+
comp != pardir
354+
or (not root and not comps)
355+
or (comps and comps[-1] == pardir)
356+
):
357+
comps.append(comp)
358+
elif comps:
359+
comps.pop()
360+
return (root + sep.join(comps)) or curdir
361+
334362
try:
335363
from posix import _path_normpath
336364
def normpath(path):
@@ -340,33 +368,7 @@ def normpath(path):
340368
return os.fsencode(_path_normpath(os.fsdecode(path))) or b"."
341369
return _path_normpath(path) or "."
342370
except ImportError:
343-
def normpath(path):
344-
"""Normalize path, eliminating double slashes, etc."""
345-
path = os.fspath(path)
346-
if isinstance(path, bytes):
347-
sep = b'/'
348-
curdir = b'.'
349-
pardir = b'..'
350-
else:
351-
sep = '/'
352-
curdir = '.'
353-
pardir = '..'
354-
if not path:
355-
return curdir
356-
_, root, tail = splitroot(path)
357-
comps = []
358-
for comp in tail.split(sep):
359-
if not comp or comp == curdir:
360-
continue
361-
if (
362-
comp != pardir
363-
or (not root and not comps)
364-
or (comps and comps[-1] == pardir)
365-
):
366-
comps.append(comp)
367-
elif comps:
368-
comps.pop()
369-
return (root + sep.join(comps)) or curdir
371+
normpath = _normpath_fallback
370372

371373

372374
def abspath(path):
@@ -388,11 +390,11 @@ def realpath(filename, *, strict=False):
388390
"""Return the canonical path of the specified filename, eliminating any
389391
symbolic links encountered in the path."""
390392
filename = os.fspath(filename)
391-
path, ok = _joinrealpath(filename[:0], filename, strict, {})
393+
path, _ = _joinrealpath(filename[:0], filename, strict, {})
392394
return abspath(path)
393395

394-
# Join two paths, normalizing and eliminating any symbolic links
395-
# encountered in the second path.
396+
# Join two paths, normalizing and eliminating any symbolic links encountered in
397+
# the second path. Two leading slashes are replaced by a single slash.
396398
def _joinrealpath(path, rest, strict, seen):
397399
if isinstance(path, bytes):
398400
sep = b'/'
@@ -414,22 +416,24 @@ def _joinrealpath(path, rest, strict, seen):
414416
continue
415417
if name == pardir:
416418
# parent dir
417-
if path:
418-
path, name = split(path)
419-
if name == pardir:
420-
path = join(path, pardir, pardir)
421-
else:
419+
if not path:
420+
# ..
422421
path = pardir
422+
elif basename(path) == pardir:
423+
# ../..
424+
path = join(path, pardir)
425+
else:
426+
# foo/bar/.. -> foo
427+
path = dirname(path)
423428
continue
424429
newpath = join(path, name)
425430
try:
426431
st = os.lstat(newpath)
432+
is_link = stat.S_ISLNK(st.st_mode)
427433
except OSError:
428434
if strict:
429435
raise
430436
is_link = False
431-
else:
432-
is_link = stat.S_ISLNK(st.st_mode)
433437
if not is_link:
434438
path = newpath
435439
continue
@@ -441,12 +445,11 @@ def _joinrealpath(path, rest, strict, seen):
441445
# use cached value
442446
continue
443447
# The symlink is not resolved, so we must have a symlink loop.
444-
if strict:
445-
# Raise OSError(errno.ELOOP)
446-
os.stat(newpath)
447-
else:
448+
if not strict:
448449
# Return already resolved part + rest of the path unchanged.
449450
return join(newpath, rest), False
451+
# Raise OSError(errno.ELOOP)
452+
os.stat(newpath)
450453
seen[newpath] = None # not resolved symlink
451454
path, ok = _joinrealpath(path, os.readlink(newpath), strict, seen)
452455
if not ok:

0 commit comments

Comments
 (0)
0