commonprefix - the beast just won't die...
I reverted the changes to {posix,nt,dos}path.commonprefix this morning, updated the tests (still to be checked in) and was starting to work on documentation changes, when I realized that something Guido said about using dirname to trim to the common directory prefix is probably not correct. Here's an example. The common prefix of ["/usr/local", "/usr/local/bin"] is "/usr/local". If you blindly apply dirname to that (which is what I think Guido suggested as the way to make commonprefix do what I wanted, you wind up with "/usr", which isn't going to be correct on most Unix flavors. Instead, you need to check that the prefix doesn't exist or isn't a directory before applying dirname. (And of course, that only works on the machine containing the paths in question. You should be able to import posixpath on a Mac and feed it Unix-style paths, which you won't be able to check for existence.) Based on this problem, I'm against documenting using dirname to trim the commonprefix output to a directory prefix. I'm going to submit a patch with the test case and minimal documentation changes and leave it at that for now. Skip
On Tue, Aug 22, 2000 at 09:45:27AM -0500, Skip Montanaro wrote:
I reverted the changes to {posix,nt,dos}path.commonprefix this morning, updated the tests (still to be checked in) and was starting to work on documentation changes, when I realized that something Guido said about using dirname to trim to the common directory prefix is probably not correct. Here's an example. The common prefix of ["/usr/local", "/usr/local/bin"] is "/usr/local". If you blindly apply dirname to that (which is what I think Guido suggested as the way to make commonprefix do what I wanted, you wind up with "/usr", which isn't going to be correct on most Unix flavors. Instead, you need to check that the prefix doesn't exist or isn't a directory before applying dirname.
And even that won't work, in a case like this: /home/swenson/ /home/swen/ (common prefix would be /home/swen, which is a directory) or cases like this: /home/swenson/ /home/swenniker/ where another directory called /home/swen exists. -- Thomas Wouters <thomas@xs4all.net> Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
[Skip Montanaro]
I reverted the changes to {posix,nt,dos}path.commonprefix this morning, updated the tests (still to be checked in) and was starting to work on documentation changes, when I realized that something Guido said about using dirname to trim to the common directory prefix is probably not correct. Here's an example. The common prefix of ["/usr/local", "/usr/local/bin"] is "/usr/local" If you blindly apply dirname to that (which is what I think Guido suggested as the way to make commonprefix do what I wanted, you wind up with "/usr" which isn't going to be correct on most Unix flavors. Instead, you need to check that the prefix doesn't exist or isn't a directory before applying dirname.
[Thomas Wouters]
And even that won't work, in a case like this:
/home/swenson/ /home/swen/
(common prefix would be /home/swen, which is a directory)
Note that Guido's suggestion does work for that, though.
or cases like this:
/home/swenson/ /home/swenniker/
where another directory called /home/swen
exists.
Ditto. This isn't coincidence: Guido's suggestion works as-is *provided that* each dirname in the original collection ends with a path separator. Change Skip's example to ["/usr/local/", "/usr/local/bin/"] ^ stuck in slashes ^ and Guido's suggestion works fine too. But these are purely string-crunching functions, and "/usr/local" *screams* "directory" to people thanks to its specific name. Let's make the test case absurdly "simple": ["/x/y", "/x/y"] What's the "common (directory) prefix" here? Well, there's simply no way to know at this level. It's /x/y if y is a directory, or /x if y's just a regular file. Guido's suggestion returns /x in this case, or /x/y if you add trailing slashes to both. If you don't tell a string gimmick which inputs are and aren't directories, you can't expect it to guess. I'll say again, if you want a new function, press for one! Just leave commonprefix alone.
participants (3)
-
Skip Montanaro -
Thomas Wouters -
Tim Peters