annotate Lib/genericpath.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents cc367c077245
children d234dd21374a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
39744
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
1 """
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
2 Path operations common to more than one OS
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
3 Do not use directly. The OS specific modules import the appropriate
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
4 functions from this module themselves.
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
5 """
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
6 import os
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
7 import stat
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
8
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
9 __all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
10 'getsize', 'isdir', 'isfile']
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
11
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
12
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
13 # Does a path exist?
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
14 # This is false for dangling symbolic links on systems that support them.
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
15 def exists(path):
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
16 """Test whether a path exists. Returns False for broken symbolic links"""
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
17 try:
58761
cc367c077245 No need to assign the results of expressions used only for side effects.
Georg Brandl <georg@python.org>
parents: 41714
diff changeset
18 os.stat(path)
39744
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
19 except os.error:
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
20 return False
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
21 return True
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
22
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
23
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
24 # This follows symbolic links, so both islink() and isdir() can be true
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
25 # for the same path ono systems that support symlinks
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
26 def isfile(path):
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
27 """Test whether a path is a regular file"""
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
28 try:
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
29 st = os.stat(path)
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
30 except os.error:
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
31 return False
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
32 return stat.S_ISREG(st.st_mode)
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
33
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
34
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
35 # Is a path a directory?
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
36 # This follows symbolic links, so both islink() and isdir()
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
37 # can be true for the same path on systems that support symlinks
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
38 def isdir(s):
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
39 """Return true if the pathname refers to an existing directory."""
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
40 try:
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
41 st = os.stat(s)
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
42 except os.error:
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
43 return False
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
44 return stat.S_ISDIR(st.st_mode)
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
45
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
46
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
47 def getsize(filename):
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
48 """Return the size of a file, reported by os.stat()."""
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
49 return os.stat(filename).st_size
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
50
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
51
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
52 def getmtime(filename):
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
53 """Return the last modification time of a file, reported by os.stat()."""
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
54 return os.stat(filename).st_mtime
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
55
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
56
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
57 def getatime(filename):
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
58 """Return the last access time of a file, reported by os.stat()."""
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
59 return os.stat(filename).st_atime
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
60
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
61
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
62 def getctime(filename):
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
63 """Return the metadata change time of a file, reported by os.stat()."""
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
64 return os.stat(filename).st_ctime
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
65
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
66
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
67 # Return the longest prefix of all list elements.
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
68 def commonprefix(m):
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
69 "Given a list of pathnames, returns the longest common leading component"
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
70 if not m: return ''
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
71 s1 = min(m)
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
72 s2 = max(m)
41714
c4d57dc60b01 Small speedup.
Raymond Hettinger <python@rcn.com>
parents: 41133
diff changeset
73 for i, c in enumerate(s1):
c4d57dc60b01 Small speedup.
Raymond Hettinger <python@rcn.com>
parents: 41133
diff changeset
74 if c != s2[i]:
39744
b3514382c327 Add missing svn:eol-style property to text files.
Tim Peters <tim.peters@gmail.com>
parents: 39742
diff changeset
75 return s1[:i]
41714
c4d57dc60b01 Small speedup.
Raymond Hettinger <python@rcn.com>
parents: 41133
diff changeset
76 return s1
41057
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
77
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
78 # Split a path in root and extension.
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
79 # The extension is everything starting at the last dot in the last
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
80 # pathname component; the root is everything before that.
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
81 # It is always true that root + ext == p.
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
82
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
83 # Generic implementation of splitext, to be parametrized with
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
84 # the separators
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
85 def _splitext(p, sep, altsep, extsep):
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41057
diff changeset
86 """Split the extension from a pathname.
41057
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
87
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
88 Extension is everything from the last dot to the end, ignoring
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
89 leading dots. Returns "(root, ext)"; ext may be empty."""
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
90
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
91 sepIndex = p.rfind(sep)
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
92 if altsep:
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
93 altsepIndex = p.rfind(altsep)
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
94 sepIndex = max(sepIndex, altsepIndex)
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
95
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
96 dotIndex = p.rfind(extsep)
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
97 if dotIndex > sepIndex:
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
98 # skip all leading dots
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
99 filenameIndex = sepIndex + 1
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
100 while filenameIndex < dotIndex:
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
101 if p[filenameIndex] != extsep:
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
102 return p[:dotIndex], p[dotIndex:]
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
103 filenameIndex += 1
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
104
98bbc93bd55b Bug #1115886: os.path.splitext('.cshrc') gives now ('.cshrc', '').
Martin v. Löwis <martin@v.loewis.de>
parents: 39744
diff changeset
105 return p, ''