summaryrefslogtreecommitdiff
path: root/Lib/genericpath.py
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2007-03-07 11:04:33 +0000
committerMartin v. Löwis <martin@v.loewis.de>2007-03-07 11:04:33 +0000
commit4ff6c7fb5456f22cdb615228a8b70d11687e2e94 (patch)
tree046ffb0ac1835ef62ebef4fff2dbf4032793596d /Lib/genericpath.py
parent030ef7450d0dd939d1c089d5a8f4dadde570521e (diff)
downloadcpython-4ff6c7fb5456f22cdb615228a8b70d11687e2e94.tar.gz
Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Diffstat (limited to 'Lib/genericpath.py')
-rw-r--r--Lib/genericpath.py29
1 files changed, 29 insertions, 0 deletions
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, ''