Commit 59b5cfdc by willmcgugan@gmail.com

Micro-optimization for normpath. I should probably leave that function alone now.

parent 9ff3da0c
...@@ -22,9 +22,8 @@ _requires_normalization = re.compile(r'/\.\.|\./|^\.$|\.$|//').search ...@@ -22,9 +22,8 @@ _requires_normalization = re.compile(r'/\.\.|\./|^\.$|\.$|//').search
def normpath(path): def normpath(path):
"""Normalizes a path to be in the format expected by FS objects. """Normalizes a path to be in the format expected by FS objects.
This function remove any leading or trailing slashes, collapses This function removes trailing slashes, collapses duplicate slashes,
duplicate slashes, and generally tries very hard to return a new path and generally tries very hard to return a new path in the canonical FS format.
in the canonical FS format.
If the path is invalid, ValueError will be raised. If the path is invalid, ValueError will be raised.
:param path: path to normalize :param path: path to normalize
...@@ -47,7 +46,7 @@ def normpath(path): ...@@ -47,7 +46,7 @@ def normpath(path):
if not _requires_normalization(path): if not _requires_normalization(path):
return path.rstrip('/') return path.rstrip('/')
components = [] components = [''] if path.startswith('/') else []
append = components.append append = components.append
special = ('..', '.', '').__contains__ special = ('..', '.', '').__contains__
try: try:
...@@ -62,9 +61,7 @@ def normpath(path): ...@@ -62,9 +61,7 @@ def normpath(path):
# causing a circular import. # causing a circular import.
from fs.errors import BackReferenceError from fs.errors import BackReferenceError
raise BackReferenceError('Too many backrefs in \'%s\'' % path) raise BackReferenceError('Too many backrefs in \'%s\'' % path)
if path[0] == '/': return u'/'.join(components)
return '/%s' % '/'.join(components)
return '/'.join(components)
if os.sep != '/': if os.sep != '/':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment