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

Python code coverage for Lib/macpath.py

#countcontent
1n/a"""Pathname and path-related operations for the Macintosh."""
2n/a
3n/aimport os
4n/afrom stat import *
5n/aimport genericpath
6n/afrom genericpath import *
7n/a
8n/a__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
9n/a "basename","dirname","commonprefix","getsize","getmtime",
10n/a "getatime","getctime", "islink","exists","lexists","isdir","isfile",
11n/a "expanduser","expandvars","normpath","abspath",
12n/a "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
13n/a "devnull","realpath","supports_unicode_filenames"]
14n/a
15n/a# strings representing various path-related bits and pieces
16n/a# These are primarily for export; internally, they are hardcoded.
17n/acurdir = ':'
18n/apardir = '::'
19n/aextsep = '.'
20n/asep = ':'
21n/apathsep = '\n'
22n/adefpath = ':'
23n/aaltsep = None
24n/adevnull = 'Dev:Null'
25n/a
26n/adef _get_colon(path):
27n/a if isinstance(path, bytes):
28n/a return b':'
29n/a else:
30n/a return ':'
31n/a
32n/a# Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here.
33n/a
34n/adef normcase(path):
35n/a if not isinstance(path, (bytes, str)):
36n/a raise TypeError("normcase() argument must be str or bytes, "
37n/a "not '{}'".format(path.__class__.__name__))
38n/a return path.lower()
39n/a
40n/a
41n/adef isabs(s):
42n/a """Return true if a path is absolute.
43n/a On the Mac, relative paths begin with a colon,
44n/a but as a special case, paths with no colons at all are also relative.
45n/a Anything else is absolute (the string up to the first colon is the
46n/a volume name)."""
47n/a
48n/a colon = _get_colon(s)
49n/a return colon in s and s[:1] != colon
50n/a
51n/a
52n/adef join(s, *p):
53n/a try:
54n/a colon = _get_colon(s)
55n/a path = s
56n/a if not p:
57n/a path[:0] + colon #23780: Ensure compatible data type even if p is null.
58n/a for t in p:
59n/a if (not path) or isabs(t):
60n/a path = t
61n/a continue
62n/a if t[:1] == colon:
63n/a t = t[1:]
64n/a if colon not in path:
65n/a path = colon + path
66n/a if path[-1:] != colon:
67n/a path = path + colon
68n/a path = path + t
69n/a return path
70n/a except (TypeError, AttributeError, BytesWarning):
71n/a genericpath._check_arg_types('join', s, *p)
72n/a raise
73n/a
74n/a
75n/adef split(s):
76n/a """Split a pathname into two parts: the directory leading up to the final
77n/a bit, and the basename (the filename, without colons, in that directory).
78n/a The result (s, t) is such that join(s, t) yields the original argument."""
79n/a
80n/a colon = _get_colon(s)
81n/a if colon not in s: return s[:0], s
82n/a col = 0
83n/a for i in range(len(s)):
84n/a if s[i:i+1] == colon: col = i + 1
85n/a path, file = s[:col-1], s[col:]
86n/a if path and not colon in path:
87n/a path = path + colon
88n/a return path, file
89n/a
90n/a
91n/adef splitext(p):
92n/a if isinstance(p, bytes):
93n/a return genericpath._splitext(p, b':', altsep, b'.')
94n/a else:
95n/a return genericpath._splitext(p, sep, altsep, extsep)
96n/asplitext.__doc__ = genericpath._splitext.__doc__
97n/a
98n/adef splitdrive(p):
99n/a """Split a pathname into a drive specification and the rest of the
100n/a path. Useful on DOS/Windows/NT; on the Mac, the drive is always
101n/a empty (don't use the volume name -- it doesn't have the same
102n/a syntactic and semantic oddities as DOS drive letters, such as there
103n/a being a separate current directory per drive)."""
104n/a
105n/a return p[:0], p
106n/a
107n/a
108n/a# Short interfaces to split()
109n/a
110n/adef dirname(s): return split(s)[0]
111n/adef basename(s): return split(s)[1]
112n/a
113n/adef ismount(s):
114n/a if not isabs(s):
115n/a return False
116n/a components = split(s)
117n/a return len(components) == 2 and not components[1]
118n/a
119n/adef islink(s):
120n/a """Return true if the pathname refers to a symbolic link."""
121n/a
122n/a try:
123n/a import Carbon.File
124n/a return Carbon.File.ResolveAliasFile(s, 0)[2]
125n/a except:
126n/a return False
127n/a
128n/a# Is `stat`/`lstat` a meaningful difference on the Mac? This is safe in any
129n/a# case.
130n/a
131n/adef lexists(path):
132n/a """Test whether a path exists. Returns True for broken symbolic links"""
133n/a
134n/a try:
135n/a st = os.lstat(path)
136n/a except OSError:
137n/a return False
138n/a return True
139n/a
140n/adef expandvars(path):
141n/a """Dummy to retain interface-compatibility with other operating systems."""
142n/a return path
143n/a
144n/a
145n/adef expanduser(path):
146n/a """Dummy to retain interface-compatibility with other operating systems."""
147n/a return path
148n/a
149n/aclass norm_error(Exception):
150n/a """Path cannot be normalized"""
151n/a
152n/adef normpath(s):
153n/a """Normalize a pathname. Will return the same result for
154n/a equivalent paths."""
155n/a
156n/a colon = _get_colon(s)
157n/a
158n/a if colon not in s:
159n/a return colon + s
160n/a
161n/a comps = s.split(colon)
162n/a i = 1
163n/a while i < len(comps)-1:
164n/a if not comps[i] and comps[i-1]:
165n/a if i > 1:
166n/a del comps[i-1:i+1]
167n/a i = i - 1
168n/a else:
169n/a # best way to handle this is to raise an exception
170n/a raise norm_error('Cannot use :: immediately after volume name')
171n/a else:
172n/a i = i + 1
173n/a
174n/a s = colon.join(comps)
175n/a
176n/a # remove trailing ":" except for ":" and "Volume:"
177n/a if s[-1:] == colon and len(comps) > 2 and s != colon*len(s):
178n/a s = s[:-1]
179n/a return s
180n/a
181n/adef abspath(path):
182n/a """Return an absolute path."""
183n/a if not isabs(path):
184n/a if isinstance(path, bytes):
185n/a cwd = os.getcwdb()
186n/a else:
187n/a cwd = os.getcwd()
188n/a path = join(cwd, path)
189n/a return normpath(path)
190n/a
191n/a# realpath is a no-op on systems without islink support
192n/adef realpath(path):
193n/a path = abspath(path)
194n/a try:
195n/a import Carbon.File
196n/a except ImportError:
197n/a return path
198n/a if not path:
199n/a return path
200n/a colon = _get_colon(path)
201n/a components = path.split(colon)
202n/a path = components[0] + colon
203n/a for c in components[1:]:
204n/a path = join(path, c)
205n/a try:
206n/a path = Carbon.File.FSResolveAliasFile(path, 1)[0].as_pathname()
207n/a except Carbon.File.Error:
208n/a pass
209n/a return path
210n/a
211n/asupports_unicode_filenames = True