ยปCore Development>Code coverage>Lib/genericpath.py

Python code coverage for Lib/genericpath.py

#countcontent
1n/a"""
2n/aPath operations common to more than one OS
3n/aDo not use directly. The OS specific modules import the appropriate
4n/afunctions from this module themselves.
5n/a"""
6n/aimport os
7n/aimport stat
8n/a
9n/a__all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',
10n/a 'getsize', 'isdir', 'isfile', 'samefile', 'sameopenfile',
11n/a 'samestat']
12n/a
13n/a
14n/a# Does a path exist?
15n/a# This is false for dangling symbolic links on systems that support them.
16n/adef exists(path):
17n/a """Test whether a path exists. Returns False for broken symbolic links"""
18n/a try:
19n/a os.stat(path)
20n/a except OSError:
21n/a return False
22n/a return True
23n/a
24n/a
25n/a# This follows symbolic links, so both islink() and isdir() can be true
26n/a# for the same path on systems that support symlinks
27n/adef isfile(path):
28n/a """Test whether a path is a regular file"""
29n/a try:
30n/a st = os.stat(path)
31n/a except OSError:
32n/a return False
33n/a return stat.S_ISREG(st.st_mode)
34n/a
35n/a
36n/a# Is a path a directory?
37n/a# This follows symbolic links, so both islink() and isdir()
38n/a# can be true for the same path on systems that support symlinks
39n/adef isdir(s):
40n/a """Return true if the pathname refers to an existing directory."""
41n/a try:
42n/a st = os.stat(s)
43n/a except OSError:
44n/a return False
45n/a return stat.S_ISDIR(st.st_mode)
46n/a
47n/a
48n/adef getsize(filename):
49n/a """Return the size of a file, reported by os.stat()."""
50n/a return os.stat(filename).st_size
51n/a
52n/a
53n/adef getmtime(filename):
54n/a """Return the last modification time of a file, reported by os.stat()."""
55n/a return os.stat(filename).st_mtime
56n/a
57n/a
58n/adef getatime(filename):
59n/a """Return the last access time of a file, reported by os.stat()."""
60n/a return os.stat(filename).st_atime
61n/a
62n/a
63n/adef getctime(filename):
64n/a """Return the metadata change time of a file, reported by os.stat()."""
65n/a return os.stat(filename).st_ctime
66n/a
67n/a
68n/a# Return the longest prefix of all list elements.
69n/adef commonprefix(m):
70n/a "Given a list of pathnames, returns the longest common leading component"
71n/a if not m: return ''
72n/a # Some people pass in a list of pathname parts to operate in an OS-agnostic
73n/a # fashion; don't try to translate in that case as that's an abuse of the
74n/a # API and they are already doing what they need to be OS-agnostic and so
75n/a # they most likely won't be using an os.PathLike object in the sublists.
76n/a if not isinstance(m[0], (list, tuple)):
77n/a m = tuple(map(os.fspath, m))
78n/a s1 = min(m)
79n/a s2 = max(m)
80n/a for i, c in enumerate(s1):
81n/a if c != s2[i]:
82n/a return s1[:i]
83n/a return s1
84n/a
85n/a# Are two stat buffers (obtained from stat, fstat or lstat)
86n/a# describing the same file?
87n/adef samestat(s1, s2):
88n/a """Test whether two stat buffers reference the same file"""
89n/a return (s1.st_ino == s2.st_ino and
90n/a s1.st_dev == s2.st_dev)
91n/a
92n/a
93n/a# Are two filenames really pointing to the same file?
94n/adef samefile(f1, f2):
95n/a """Test whether two pathnames reference the same actual file"""
96n/a s1 = os.stat(f1)
97n/a s2 = os.stat(f2)
98n/a return samestat(s1, s2)
99n/a
100n/a
101n/a# Are two open files really referencing the same file?
102n/a# (Not necessarily the same file descriptor!)
103n/adef sameopenfile(fp1, fp2):
104n/a """Test whether two open file objects reference the same file"""
105n/a s1 = os.fstat(fp1)
106n/a s2 = os.fstat(fp2)
107n/a return samestat(s1, s2)
108n/a
109n/a
110n/a# Split a path in root and extension.
111n/a# The extension is everything starting at the last dot in the last
112n/a# pathname component; the root is everything before that.
113n/a# It is always true that root + ext == p.
114n/a
115n/a# Generic implementation of splitext, to be parametrized with
116n/a# the separators
117n/adef _splitext(p, sep, altsep, extsep):
118n/a """Split the extension from a pathname.
119n/a
120n/a Extension is everything from the last dot to the end, ignoring
121n/a leading dots. Returns "(root, ext)"; ext may be empty."""
122n/a # NOTE: This code must work for text and bytes strings.
123n/a
124n/a sepIndex = p.rfind(sep)
125n/a if altsep:
126n/a altsepIndex = p.rfind(altsep)
127n/a sepIndex = max(sepIndex, altsepIndex)
128n/a
129n/a dotIndex = p.rfind(extsep)
130n/a if dotIndex > sepIndex:
131n/a # skip all leading dots
132n/a filenameIndex = sepIndex + 1
133n/a while filenameIndex < dotIndex:
134n/a if p[filenameIndex:filenameIndex+1] != extsep:
135n/a return p[:dotIndex], p[dotIndex:]
136n/a filenameIndex += 1
137n/a
138n/a return p, p[:0]
139n/a
140n/adef _check_arg_types(funcname, *args):
141n/a hasstr = hasbytes = False
142n/a for s in args:
143n/a if isinstance(s, str):
144n/a hasstr = True
145n/a elif isinstance(s, bytes):
146n/a hasbytes = True
147n/a else:
148n/a raise TypeError('%s() argument must be str or bytes, not %r' %
149n/a (funcname, s.__class__.__name__)) from None
150n/a if hasstr and hasbytes:
151n/a raise TypeError("Can't mix strings and bytes in path components") from None