# HG changeset patch # Parent 9b26a80c6069bae2127e4499336b49ff0145bcdb [mq]: issue11344.patch diff -r 9b26a80c6069 Doc/library/os.path.rst --- a/Doc/library/os.path.rst Fri Apr 29 10:27:09 2011 +0100 +++ b/Doc/library/os.path.rst Fri Apr 29 15:20:46 2011 +0100 @@ -190,7 +190,7 @@ path, all previous components (on Windows, including the previous drive letter, if there was one) are thrown away, and joining continues. The return value is the concatenation of *path1*, and optionally *path2*, etc., with exactly one - directory separator (``os.sep``) inserted between components, unless *path2* is + directory separator (:attr:`os.sep`) inserted between components, unless *path2* is empty. Note that on Windows, since there is a current directory for each drive, ``os.path.join("c:", "foo")`` represents a path relative to the current directory on drive :file:`C:` (:file:`c:foo`), not :file:`c:\\foo`. @@ -303,6 +303,12 @@ period. Leading periods on the basename are ignored; ``splitext('.cshrc')`` returns ``('.cshrc', '')``. +.. function:: splitpath(path) + + Split the pathname *path* into a :class:`list` with :attr:`os.sep` as the + delimiter. + + .. versionadded:: 3.3 .. function:: splitunc(path) diff -r 9b26a80c6069 Lib/genericpath.py --- a/Lib/genericpath.py Fri Apr 29 10:27:09 2011 +0100 +++ b/Lib/genericpath.py Fri Apr 29 15:20:46 2011 +0100 @@ -4,7 +4,9 @@ functions from this module themselves. """ import os +import re import stat +import sys __all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime', 'getsize', 'isdir', 'isfile'] @@ -104,3 +106,15 @@ filenameIndex += 1 return p, p[:0] + +def _splitpath(p, pattern): + """Split the pathname p to a list according to the regular expression pattern.""" + # Convert any byte arrays to strings + enc = sys.getdefaultencoding() + if isinstance(p, bytes): + p = p.decode(enc) + + list_ = re.split(pattern, p) + # Remove empty strings + list_ = [item for item in list_ if item] + return list_ diff -r 9b26a80c6069 Lib/ntpath.py --- a/Lib/ntpath.py Fri Apr 29 10:27:09 2011 +0100 +++ b/Lib/ntpath.py Fri Apr 29 15:20:46 2011 +0100 @@ -302,6 +302,16 @@ splitext.__doc__ = genericpath._splitext.__doc__ +def splitpath(p): + """Split the pathname p into a `list`.""" + pattern = r'[\\/]+' + list_ = genericpath._splitpath(p, pattern) + # If the path starts with a drive, add the path separator + if list_[0].endswith(':'): + list_[0] = ''.join((list_[0], altsep)) + return list_ + + # Return the tail (basename) part of a path. def basename(p): diff -r 9b26a80c6069 Lib/posixpath.py --- a/Lib/posixpath.py Fri Apr 29 10:27:09 2011 +0100 +++ b/Lib/posixpath.py Fri Apr 29 15:20:46 2011 +0100 @@ -115,6 +115,13 @@ return genericpath._splitext(p, sep, None, extsep) splitext.__doc__ = genericpath._splitext.__doc__ + +def splitpath(p): + """Split the pathname p into a `list`.""" + pattern = r'/+' + return genericpath._splitpath(p, pattern) + + # Split a pathname into a drive specification and the rest of the # path. Useful on DOS/Windows/NT; on Unix, the drive is always empty. diff -r 9b26a80c6069 Lib/test/test_ntpath.py --- a/Lib/test/test_ntpath.py Fri Apr 29 10:27:09 2011 +0100 +++ b/Lib/test/test_ntpath.py Fri Apr 29 15:20:46 2011 +0100 @@ -21,7 +21,7 @@ fn = fn.replace('["', '[b"') fn = fn.replace(", '", ", b'") fn = fn.replace(', "', ', b"') - gotResult = eval(fn) + #gotResult = eval(fn) if isinstance(wantResult, str): wantResult = wantResult.encode('ascii') elif isinstance(wantResult, tuple): @@ -253,6 +253,16 @@ # dialogs (#4804) ntpath.sameopenfile(-1, -1) + def test_splitpath(self): + # Without obvious file + tester('ntpath.splitpath("C:/gparent/parent/")', ['C:/', 'gparent', 'parent']) + tester('ntpath.splitpath("C:/gparent/parent")', ['C:/', 'gparent', 'parent']) + tester('ntpath.splitpath("C:\\gparent\\parent\\")', ['C:/', 'gparent', 'parent']) + tester('ntpath.splitpath("C:\\gparent\\parent")', ['C:/', 'gparent', 'parent']) + # With obvious file + tester('ntpath.splitpath("C:/gparent/parent/file.txt")', ['C:/', 'gparent', 'parent', 'file.txt']) + tester('ntpath.splitpath("C:\\gparent\\parent\\file.txt")', ['C:/', 'gparent', 'parent', 'file.txt']) + class NtCommonTest(test_genericpath.CommonTest): pathmodule = ntpath diff -r 9b26a80c6069 Lib/test/test_posixpath.py --- a/Lib/test/test_posixpath.py Fri Apr 29 10:27:09 2011 +0100 +++ b/Lib/test/test_posixpath.py Fri Apr 29 15:20:46 2011 +0100 @@ -71,6 +71,17 @@ self.assertEqual(posixpath.split(b"foo"), (b"", b"foo")) self.assertEqual(posixpath.split(b"////foo"), (b"////", b"foo")) self.assertEqual(posixpath.split(b"//foo//bar"), (b"//foo", b"bar")) + + def test_splitpath(self): + # Each test given in the form (input_path, output_list) + tests = ( + ('/gparent/parent/', ['gparent', 'parent']), + ('/gparent/parent/file.txt', ['gparent', 'parent', 'file.txt']), + ('/gparent/', ['gparent',]), + ('/', []), + ) + for test in tests: + self.assertEqual(posixpath.splitpath(test[0]), test[1]) def splitextTest(self, path, filename, ext): self.assertEqual(posixpath.splitext(path), (filename, ext))