[Python-checkins] CVS: python/dist/src/Lib posixpath.py,1.33,1.34

Skip Montanaro python-dev@python.org
Wed, 19 Jul 2000 10:09:55 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory slayer.i.sourceforge.net:/tmp/cvs-serv17257

Modified Files:
	posixpath.py 
Log Message:
added rewritten normpath from Moshe Zadka that does the right thing with
paths containing ..


Index: posixpath.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/posixpath.py,v
retrieving revision 1.33
retrieving revision 1.34
diff -C2 -r1.33 -r1.34
*** posixpath.py	2000/07/16 16:52:45	1.33
--- posixpath.py	2000/07/19 17:09:51	1.34
***************
*** 347,374 ****
  def normpath(path):
      """Normalize path, eliminating double slashes, etc."""
      import string
!     # Treat initial slashes specially
!     slashes = ''
!     while path[:1] == '/':
!         slashes = slashes + '/'
!         path = path[1:]
!     comps = string.splitfields(path, '/')
!     i = 0
!     while i < len(comps):
!         if comps[i] == '.':
!             del comps[i]
!             while i < len(comps) and comps[i] == '':
!                 del comps[i]
!         elif comps[i] == '..' and i > 0 and comps[i-1] not in ('', '..'):
!             del comps[i-1:i+1]
!             i = i-1
!         elif comps[i] == '' and i > 0 and comps[i-1] <> '':
!             del comps[i]
!         else:
!             i = i+1
!     # If the path is now empty, substitute '.'
!     if not comps and not slashes:
!         comps.append('.')
!     return slashes + string.joinfields(comps, '/')
  
  
--- 347,369 ----
  def normpath(path):
      """Normalize path, eliminating double slashes, etc."""
+     if path == '':
+         return '.'
      import string
!     initial_slash = (path[0] == '/')
!     comps = string.split(path, '/')
!     new_comps = []
!     for comp in comps:
!         if comp in ('', '.'):
!             continue
!         if (comp != '..' or (not initial_slash and not new_comps) or 
!              (new_comps and new_comps[-1] == '..')):
!             new_comps.append(comp)
!         elif new_comps:
!             new_comps.pop()
!     comps = new_comps
!     path = string.join(comps, '/')
!     if initial_slash:
!         path = '/' + path
!     return path or '.'