[Pythonmac-SIG] os.path.normpath()

Corran Webster cwebster@nevada.edu
Tue, 1 Aug 2000 23:43:23 -0700


--============_-1246923890==_============
Content-Type: text/plain; charset="us-ascii"

Hi,
    there is currently discussion on the distutils list because of some
problems with the current implementation of os.path.normpath() on the
macintosh.  The problem arises because

os.path.normpath("Macintosh HD:Applications:") == "Macintosh HD:Applications:"

and

os.path.normpath("Macintosh HD:Applications") == "Macintosh HD:Applications"

despite the fact that the paths are equivalent.  distutils needs a platform
independent way to check for two paths being equivalent, and this is
failing because of examples like the above.

I've written a replacement (see attachment) which should guarantee that two
equivalent paths get normalised to the same thing, but I'd like to just
check with folks here that I'm not missing anything.

If all looks good, I'll submit a patch for macpath.py to use this new normpath.

Regarsd,
Corran

--============_-1246923890==_============
Content-Type: text/plain; name="normpath.py"; charset="us-ascii"
Content-Disposition: attachment; filename="normpath.py"

import string

def normpath(s):
	"""Normalize a pathname.  Will return the same result for
	equivalent paths."""

	if ":" not in s:
		return ":"+s

	comps = string.splitfields(s, ":")
	i = 1
	while i < len(comps)-1:
		if comps[i] == "" and comps[i-1] != "":
			if i > 1:
				del comps[i-1:i+1]
				i = i-1
			else:
				# best way to handle this is to raise an
exception
				raise norm_error, 'path starts with volume::'
		else:
			i = i + 1
		print i, comps

	s = string.join(comps, ":")

	# remove trailing ":" except for ":" and "Volume:"
	if s[-1] == ":" and len(comps) > 2 and s != ":"*len(s):
		s = s[:-1]
	return s

def _test_normpath():
	pass

if __name__ == "__main__":
	_test_normpath()
	
--============_-1246923890==_============
Content-Type: text/plain; name="test_normpath.py"; charset="us-ascii"
Content-Disposition: attachment; filename="test_normpath.py"

from test_support import *

import normpath

errors = 0

def testit(name, value, expected):
	if value != expected:
		raise TestFailed, '%s returned %f, expected %f'%\
			(name, value, expected)


testit("normpath", normpath.normpath(":"), ":")
testit("normpath", normpath.normpath("::"), "::")
testit("normpath", normpath.normpath(":::"), ":::")

testit("normpath", normpath.normpath("test"), ":test")
testit("normpath", normpath.normpath(":test"), ":test")
testit("normpath", normpath.normpath("test:"), "test:")
testit("normpath", normpath.normpath(":test:test1:"), ":test:test1")
testit("normpath", normpath.normpath("test:test1:"), "test:test1")

testit("normpath", normpath.normpath(":test::"), ":")
testit("normpath", normpath.normpath(":test:::"), "::")
testit("normpath", normpath.normpath(":test:::test1:"), "::test1")
testit("normpath", normpath.normpath(":test:test1::"), ":test")
testit("normpath", normpath.normpath("test:test1::"), "test:")
testit("normpath", normpath.normpath(":test:test1:::"), ":")

if errors:
	print str(errors) + " errors."
else:
	print "No errors.  Thank your lucky stars."


--============_-1246923890==_============--