On 6 Nov, 2012, at 16:27, Serhiy Storchaka <stor...@gmail.com> wrote:/var/log
> See http://bugs.python.org/issue10395.
>
> os.path.commonpath() should be a function which returns right longest common sub-path for specified paths (os.path.commonprefix() is completely useless for this).
>
> There are some open questions about details of *right* behavior.
>
>
>
> What should be a common prefix of '/var/log/apache2' and
> '/var//log/mysql'?
/usr
> What should be a common prefix of '/usr' and '//usr'?
/usr/local
> What should be a common prefix of '/usr/local/' and '/usr/local/'?
/usr/local
> What should be a common prefix of '/usr/local/' and '/usr/local/bin'?
/usr/bin
> What should be a common prefix of '/usr/bin/..' and '/usr/bin'?
In all cases the path is first split into its elements, then calculate the largest common prefix of the two sets of elements, then join the elements back up again.
/var/log
On 6 Nov, 2012, at 16:27, Serhiy Storchaka <stor...@gmail.com> wrote:
> What should be a common prefix of '/var/log/apache2' and '/var//log/mysql'?
/usr
> What should be a common prefix of '/usr' and '//usr'?
/usr/local
> What should be a common prefix of '/usr/local/' and '/usr/local/'?
> What should be a common prefix of '/usr/local/' and '/usr/local/bin'?/usr/local
/usr/bin
> What should be a common prefix of '/usr/bin/..' and '/usr/bin'?
* Relative paths that don't share a prefix should raise an exception
* On windows two paths that don't have the same drive should raise an exception
The alternative is to return some arbitrary value (like None) that you have to test for, which would IMHO make it too easy to accidently pass an useless value to some other API and get a confusing exeption later on.
Bruce Leban wrote:But then the common prefix of "/a/b" and "/a/c" would be "/a/",
If you change the semantics so that it either (1) it always always includes a trailing / or (2) it includes a trailing slash if the two paths have it in common, then you don't have the weirdness that in this case it returns a slash and in others it doesn't. I am slightly inclined to (1) at this point.
which would be very unexpected -- usually the dirname of a path is
not considered to include a trailing slash.
The special treatment of the root directory is no weirder than it
is anywhere else. It's already special, since in unix it's the
only case where a trailing slash is semantically significant.
(To the kernel, at least -- a few command line utilities break this
rule, but they're screwy.)
It would be nice if in conjunction with this os.path.commonprefix is renamed as string.commonprefix with the os.path.commonprefix kept for backwards compatibility (and deprecated).more inlineOn Tue, Nov 6, 2012 at 7:49 AM, Ronald Oussoren <ronaldo...@mac.com> wrote:/var/log
On 6 Nov, 2012, at 16:27, Serhiy Storchaka <stor...@gmail.com> wrote:
> What should be a common prefix of '/var/log/apache2' and '/var//log/mysql'?
/usr
> What should be a common prefix of '/usr' and '//usr'?
/usr/local
> What should be a common prefix of '/usr/local/' and '/usr/local/'?
It appears that you want the result to never include a trailing /. However, you've left out one key test case:What is commonpath('/usr', '/var')?It seems to me that the only reasonable value is '/'.
If you change the semantics so that it either (1) it always always includes a trailing / or (2) it includes a trailing slash if the two paths have it in common, then you don't have the weirdness that in this case it returns a slash and in others it doesn't. I am slightly inclined to (1) at this point.
It would also be a bit surprising that there are cases where commonpath(a,a) != a.
> What should be a common prefix of '/usr/local/' and '/usr/local/bin'?/usr/local
/usr/bin
> What should be a common prefix of '/usr/bin/..' and '/usr/bin'?
seems better than the alternative of interpreting the '..'.
* Relative paths that don't share a prefix should raise an exceptionWhy? Why is an empty path not a reasonable result?
* On windows two paths that don't have the same drive should raise an exceptionI disagree. On unix systems, should two paths that don't have the same drive also raise an exception? What if I'm using this function on windows to compare two http paths or two paths to a remote unix system? Raising an exception in either case would be wrong.
The alternative is to return some arbitrary value (like None) that you have to test for, which would IMHO make it too easy to accidently pass an useless value to some other API and get a confusing exeption later on.Yes, don't return a useless value. An empty string is useful in the relative path case and '/' is useful in the non-relative but paths don't have common prefix at all case.
--- Bruce