[Jython-checkins] jython: Add relpath, fixes test_py_compile.

frank.wierzbicki jython-checkins at python.org
Fri May 25 00:44:46 CEST 2012


http://hg.python.org/jython/rev/d3fd048e40bf
changeset:   6665:d3fd048e40bf
user:        Frank Wierzbicki <fwierzbicki at gmail.com>
date:        Thu May 24 15:44:40 2012 -0700
summary:
  Add relpath, fixes test_py_compile.

files:
  Lib/posixpath.py |  17 ++++++++++++++++-
  1 files changed, 16 insertions(+), 1 deletions(-)


diff --git a/Lib/posixpath.py b/Lib/posixpath.py
--- a/Lib/posixpath.py
+++ b/Lib/posixpath.py
@@ -19,7 +19,7 @@
            "walk","expanduser","expandvars","normpath","abspath",
            "samefile",
            "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
-           "devnull","realpath","supports_unicode_filenames"]
+           "devnull","realpath","supports_unicode_filenames", "relpath"]
 
 # strings representing various path-related bits and pieces
 curdir = '.'
@@ -475,7 +475,22 @@
                 path = normpath(resolved)
         return path
 
+def relpath(path, start=curdir):
+    """Return a relative version of a path"""
 
+    if not path:
+        raise ValueError("no path specified")
+
+    start_list = [x for x in abspath(start).split(sep) if x]
+    path_list = [x for x in abspath(path).split(sep) if x]
+
+    # Work out how much of the filepath is shared by start and path.
+    i = len(commonprefix([start_list, path_list]))
+
+    rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
+    if not rel_list:
+        return curdir
+    return join(*rel_list)
 def _ensure_str(obj):
     """Ensure obj is a string, otherwise raise a TypeError"""
     if isinstance(obj, basestring):

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list