summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>2008-10-03 21:57:20 (GMT)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>2008-10-03 21:57:20 (GMT)
commitb186f3438419a6c881705a42db1ecc4e52f5241e (patch)
tree6415831b75a16b3a01a975d8cd720d54cf57dd26
parentf60fe81030834800869151fe0414ddc3e7c21939 (diff)
downloadcpython-b186f3438419a6c881705a42db1ecc4e52f5241e.zip
cpython-b186f3438419a6c881705a42db1ecc4e52f5241e.tar.gz
cpython-b186f3438419a6c881705a42db1ecc4e52f5241e.tar.bz2
Issue3187 for Macintosh platform:
macpath.py now accepts both unicode string and bytes as file names. Also add more tests for these functions. Reviewed by Benjamin.
-rw-r--r--Lib/macpath.py62
-rw-r--r--Lib/test/test_macpath.py84
2 files changed, 123 insertions, 23 deletions
diff --git a/Lib/macpath.py b/Lib/macpath.py
index 7d6ae41..c023646 100644
--- a/Lib/macpath.py
+++ b/Lib/macpath.py
@@ -13,6 +13,7 @@ __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
"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 = '.'
@@ -22,6 +23,12 @@ 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):
@@ -35,21 +42,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
@@ -59,18 +68,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):
@@ -80,7 +93,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()
@@ -92,7 +105,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."""
@@ -131,13 +144,15 @@ 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
@@ -147,10 +162,10 @@ def normpath(s):
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
@@ -169,8 +184,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)
path = Carbon.File.FSResolveAliasFile(path, 1)[0].as_pathname()
diff --git a/Lib/test/test_macpath.py b/Lib/test/test_macpath.py
index 38b85e5..368d8f5 100644
--- a/Lib/test/test_macpath.py
+++ b/Lib/test/test_macpath.py
@@ -18,6 +18,14 @@ class MacPathTestCase(unittest.TestCase):
self.failIf(isabs(":foo:bar"))
self.failIf(isabs(":foo:bar:"))
+ self.assert_(isabs(b"xx:yy"))
+ self.assert_(isabs(b"xx:yy:"))
+ self.assert_(isabs(b"xx:"))
+ self.failIf(isabs(b"foo"))
+ self.failIf(isabs(b":foo"))
+ self.failIf(isabs(b":foo:bar"))
+ self.failIf(isabs(b":foo:bar:"))
+
def test_commonprefix(self):
commonprefix = macpath.commonprefix
@@ -28,6 +36,13 @@ class MacPathTestCase(unittest.TestCase):
self.assert_(commonprefix([":home:swen:spam", ":home:swen:spam"])
== ":home:swen:spam")
+ self.assert_(commonprefix([b"home:swenson:spam", b"home:swen:spam"])
+ == b"home:swen")
+ self.assert_(commonprefix([b":home:swen:spam", b":home:swen:eggs"])
+ == b":home:swen:")
+ self.assert_(commonprefix([b":home:swen:spam", b":home:swen:spam"])
+ == b":home:swen:spam")
+
def test_split(self):
split = macpath.split
self.assertEquals(split("foo:bar"),
@@ -39,11 +54,37 @@ class MacPathTestCase(unittest.TestCase):
self.assertEquals(split(":conky:mountpoint:"),
(':conky:mountpoint', ''))
+ self.assertEquals(split(b"foo:bar"),
+ (b'foo:', b'bar'))
+ self.assertEquals(split(b"conky:mountpoint:foo:bar"),
+ (b'conky:mountpoint:foo', b'bar'))
+
+ self.assertEquals(split(b":"), (b'', b''))
+ self.assertEquals(split(b":conky:mountpoint:"),
+ (b':conky:mountpoint', b''))
+
+ def test_join(self):
+ join = macpath.join
+ self.assertEquals(join('a', 'b'), ':a:b')
+ self.assertEquals(join('', 'a:b'), 'a:b')
+ self.assertEquals(join('a:b', 'c'), 'a:b:c')
+ self.assertEquals(join('a:b', ':c'), 'a:b:c')
+ self.assertEquals(join('a', ':b', ':c'), ':a:b:c')
+
+ self.assertEquals(join(b'a', b'b'), b':a:b')
+ self.assertEquals(join(b'', b'a:b'), b'a:b')
+ self.assertEquals(join(b'a:b', b'c'), b'a:b:c')
+ self.assertEquals(join(b'a:b', b':c'), b'a:b:c')
+ self.assertEquals(join(b'a', b':b', b':c'), b':a:b:c')
+
def test_splitdrive(self):
splitdrive = macpath.splitdrive
self.assertEquals(splitdrive("foo:bar"), ('', 'foo:bar'))
self.assertEquals(splitdrive(":foo:bar"), ('', ':foo:bar'))
+ self.assertEquals(splitdrive(b"foo:bar"), (b'', b'foo:bar'))
+ self.assertEquals(splitdrive(b":foo:bar"), (b'', b':foo:bar'))
+
def test_splitext(self):
splitext = macpath.splitext
self.assertEquals(splitext(":foo.ext"), (':foo', '.ext'))
@@ -54,6 +95,49 @@ class MacPathTestCase(unittest.TestCase):
self.assertEquals(splitext(""), ('', ''))
self.assertEquals(splitext("foo.bar.ext"), ('foo.bar', '.ext'))
+ self.assertEquals(splitext(b":foo.ext"), (b':foo', b'.ext'))
+ self.assertEquals(splitext(b"foo:foo.ext"), (b'foo:foo', b'.ext'))
+ self.assertEquals(splitext(b".ext"), (b'.ext', b''))
+ self.assertEquals(splitext(b"foo.ext:foo"), (b'foo.ext:foo', b''))
+ self.assertEquals(splitext(b":foo.ext:"), (b':foo.ext:', b''))
+ self.assertEquals(splitext(b""), (b'', b''))
+ self.assertEquals(splitext(b"foo.bar.ext"), (b'foo.bar', b'.ext'))
+
+ def test_ismount(self):
+ ismount = macpath.ismount
+ self.assertEquals(ismount("a:"), True)
+ self.assertEquals(ismount("a:b"), False)
+ self.assertEquals(ismount("a:b:"), True)
+ self.assertEquals(ismount(""), False)
+ self.assertEquals(ismount(":"), False)
+
+ self.assertEquals(ismount(b"a:"), True)
+ self.assertEquals(ismount(b"a:b"), False)
+ self.assertEquals(ismount(b"a:b:"), True)
+ self.assertEquals(ismount(b""), False)
+ self.assertEquals(ismount(b":"), False)
+
+ def test_normpath(self):
+ normpath = macpath.normpath
+ self.assertEqual(normpath("a:b"), "a:b")
+ self.assertEqual(normpath("a"), ":a")
+ self.assertEqual(normpath("a:b::c"), "a:c")
+ self.assertEqual(normpath("a:b:c:::d"), "a:d")
+ self.assertRaises(macpath.norm_error, normpath, "a::b")
+ self.assertRaises(macpath.norm_error, normpath, "a:b:::c")
+ self.assertEqual(normpath(":"), ":")
+ self.assertEqual(normpath("a:"), "a:")
+ self.assertEqual(normpath("a:b:"), "a:b")
+
+ self.assertEqual(normpath(b"a:b"), b"a:b")
+ self.assertEqual(normpath(b"a"), b":a")
+ self.assertEqual(normpath(b"a:b::c"), b"a:c")
+ self.assertEqual(normpath(b"a:b:c:::d"), b"a:d")
+ self.assertRaises(macpath.norm_error, normpath, b"a::b")
+ self.assertRaises(macpath.norm_error, normpath, b"a:b:::c")
+ self.assertEqual(normpath(b":"), b":")
+ self.assertEqual(normpath(b"a:"), b"a:")
+ self.assertEqual(normpath(b"a:b:"), b"a:b")
def test_main():
support.run_unittest(MacPathTestCase)