aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/macpath.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/macpath.py')
-rw-r--r--Lib/macpath.py102
1 files changed, 46 insertions, 56 deletions
diff --git a/Lib/macpath.py b/Lib/macpath.py
index cd4cb8581af..1615d9122a1 100644
--- a/Lib/macpath.py
+++ b/Lib/macpath.py
@@ -1,7 +1,6 @@
"""Pathname and path-related operations for the Macintosh."""
import os
-import warnings
from stat import *
import genericpath
from genericpath import *
@@ -9,11 +8,12 @@ from genericpath import *
__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
"basename","dirname","commonprefix","getsize","getmtime",
"getatime","getctime", "islink","exists","lexists","isdir","isfile",
- "walk","expanduser","expandvars","normpath","abspath",
+ "expanduser","expandvars","normpath","abspath",
"curdir","pardir","sep","pathsep","defpath","altsep","extsep",
"devnull","realpath","supports_unicode_filenames"]
# strings representing various path-related bits and pieces
+# These are primarily for export; internally, they are hardcoded.
curdir = ':'
pardir = '::'
extsep = '.'
@@ -23,9 +23,18 @@ defpath = ':'
altsep = None
devnull = 'Dev:Null'
+def _get_colon(path):
+ if isinstance(path, bytes):
+ return b':'
+ else:
+ return ':'
+
# Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here.
def normcase(path):
+ if not isinstance(path, (bytes, str)):
+ raise TypeError("normcase() argument must be str or bytes, "
+ "not '{}'".format(path.__class__.__name__))
return path.lower()
@@ -36,21 +45,23 @@ def isabs(s):
Anything else is absolute (the string up to the first colon is the
volume name)."""
- return ':' in s and s[0] != ':'
+ colon = _get_colon(s)
+ return colon in s and s[:1] != colon
def join(s, *p):
+ colon = _get_colon(s)
path = s
for t in p:
if (not s) or isabs(t):
path = t
continue
- if t[:1] == ':':
+ if t[:1] == colon:
t = t[1:]
- if ':' not in path:
- path = ':' + path
- if path[-1:] != ':':
- path = path + ':'
+ if colon not in path:
+ path = colon + path
+ if path[-1:] != colon:
+ path = path + colon
path = path + t
return path
@@ -60,18 +71,22 @@ def split(s):
bit, and the basename (the filename, without colons, in that directory).
The result (s, t) is such that join(s, t) yields the original argument."""
- if ':' not in s: return '', s
- colon = 0
+ colon = _get_colon(s)
+ if colon not in s: return s[:0], s
+ col = 0
for i in range(len(s)):
- if s[i] == ':': colon = i + 1
- path, file = s[:colon-1], s[colon:]
- if path and not ':' in path:
- path = path + ':'
+ if s[i:i+1] == colon: col = i + 1
+ path, file = s[:col-1], s[col:]
+ if path and not colon in path:
+ path = path + colon
return path, file
def splitext(p):
- return genericpath._splitext(p, sep, altsep, extsep)
+ if isinstance(p, bytes):
+ return genericpath._splitext(p, b':', altsep, b'.')
+ else:
+ return genericpath._splitext(p, sep, altsep, extsep)
splitext.__doc__ = genericpath._splitext.__doc__
def splitdrive(p):
@@ -81,7 +96,7 @@ def splitdrive(p):
syntactic and semantic oddities as DOS drive letters, such as there
being a separate current directory per drive)."""
- return '', p
+ return p[:0], p
# Short interfaces to split()
@@ -93,7 +108,7 @@ def ismount(s):
if not isabs(s):
return False
components = split(s)
- return len(components) == 2 and components[1] == ''
+ return len(components) == 2 and not components[1]
def islink(s):
"""Return true if the pathname refers to a symbolic link."""
@@ -132,62 +147,36 @@ def normpath(s):
"""Normalize a pathname. Will return the same result for
equivalent paths."""
- if ":" not in s:
- return ":"+s
+ colon = _get_colon(s)
+
+ if colon not in s:
+ return colon + s
- comps = s.split(":")
+ comps = s.split(colon)
i = 1
while i < len(comps)-1:
- if comps[i] == "" and comps[i-1] != "":
+ if not comps[i] and comps[i-1]:
if i > 1:
del comps[i-1:i+1]
i = i - 1
else:
# best way to handle this is to raise an exception
- raise norm_error, 'Cannot use :: immediately after volume name'
+ raise norm_error('Cannot use :: immediately after volume name')
else:
i = i + 1
- s = ":".join(comps)
+ s = colon.join(comps)
# remove trailing ":" except for ":" and "Volume:"
- if s[-1] == ":" and len(comps) > 2 and s != ":"*len(s):
+ if s[-1:] == colon and len(comps) > 2 and s != colon*len(s):
s = s[:-1]
return s
-
-def walk(top, func, arg):
- """Directory tree walk with callback function.
-
- For each directory in the directory tree rooted at top (including top
- itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
- dirname is the name of the directory, and fnames a list of the names of
- the files and subdirectories in dirname (excluding '.' and '..'). func
- may modify the fnames list in-place (e.g. via del or slice assignment),
- and walk will only recurse into the subdirectories whose names remain in
- fnames; this can be used to implement a filter, or to impose a specific
- order of visiting. No semantics are defined for, or required of, arg,
- beyond that arg is always passed to func. It can be used, e.g., to pass
- a filename pattern, or a mutable object designed to accumulate
- statistics. Passing None for arg is common."""
- warnings.warnpy3k("In 3.x, os.path.walk is removed in favor of os.walk.",
- stacklevel=2)
- try:
- names = os.listdir(top)
- except os.error:
- return
- func(arg, top, names)
- for name in names:
- name = join(top, name)
- if isdir(name) and not islink(name):
- walk(name, func, arg)
-
-
def abspath(path):
"""Return an absolute path."""
if not isabs(path):
- if isinstance(path, unicode):
- cwd = os.getcwdu()
+ if isinstance(path, bytes):
+ cwd = os.getcwdb()
else:
cwd = os.getcwd()
path = join(cwd, path)
@@ -202,8 +191,9 @@ def realpath(path):
return path
if not path:
return path
- components = path.split(':')
- path = components[0] + ':'
+ colon = _get_colon(path)
+ components = path.split(colon)
+ path = components[0] + colon
for c in components[1:]:
path = join(path, c)
try: