From 4ff6c7fb5456f22cdb615228a8b70d11687e2e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20v=2E=20L=C3=B6wis?= Date: Wed, 7 Mar 2007 11:04:33 +0000 Subject: Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', ''). --- Lib/genericpath.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'Lib/genericpath.py') diff --git a/Lib/genericpath.py b/Lib/genericpath.py index 1574cef0c8..6d11ec03b0 100644 --- a/Lib/genericpath.py +++ b/Lib/genericpath.py @@ -75,3 +75,32 @@ def commonprefix(m): if s1[i] != s2[i]: return s1[:i] return s1[:n] + +# Split a path in root and extension. +# The extension is everything starting at the last dot in the last +# pathname component; the root is everything before that. +# It is always true that root + ext == p. + +# Generic implementation of splitext, to be parametrized with +# the separators +def _splitext(p, sep, altsep, extsep): + """Split the extension from a pathname. + + Extension is everything from the last dot to the end, ignoring + leading dots. Returns "(root, ext)"; ext may be empty.""" + + sepIndex = p.rfind(sep) + if altsep: + altsepIndex = p.rfind(altsep) + sepIndex = max(sepIndex, altsepIndex) + + dotIndex = p.rfind(extsep) + if dotIndex > sepIndex: + # skip all leading dots + filenameIndex = sepIndex + 1 + while filenameIndex < dotIndex: + if p[filenameIndex] != extsep: + return p[:dotIndex], p[dotIndex:] + filenameIndex += 1 + + return p, '' -- cgit v1.2.1