Debian Codegraph @

src/python/backports/zstd/_compat.py

raw
1 import os
2 import sys
3
4 if sys.version_info >= (3, 12):
5 os_path_splitroot = os.path.splitroot
6
7 elif os.name == 'posix':
8 # from Lib/posixpath.py
9 def os_path_splitroot(p):
10 """Split a pathname into drive, root and tail.
11
12 The tail contains anything after the root."""
13 p = os.fspath(p)
14 if isinstance(p, bytes):
15 sep = b'/'
16 empty = b''
17 else:
18 sep = '/'
19 empty = ''
20 if p[:1] != sep:
21 # Relative path, e.g.: 'foo'
22 return empty, empty, p
23 elif p[1:2] != sep or p[2:3] == sep:
24 # Absolute path, e.g.: '/foo', '///foo', '////foo', etc.
25 return empty, sep, p[1:]
26 else:
27 # Precisely two leading slashes, e.g.: '//foo'. Implementation defined per POSIX, see
28 # https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
29 return empty, p[:2], p[2:]
30
31 elif os.name == 'nt':
32 # from Lib/ntpath.py
33 def os_path_splitroot(p):
34 """Split a pathname into drive, root and tail.
35
36 The tail contains anything after the root."""
37 p = os.fspath(p)
38 if isinstance(p, bytes):
39 sep = b'\\'
40 altsep = b'/'
41 colon = b':'
42 unc_prefix = b'\\\\?\\UNC\\'
43 empty = b''
44 else:
45 sep = '\\'
46 altsep = '/'
47 colon = ':'
48 unc_prefix = '\\\\?\\UNC\\'
49 empty = ''
50 normp = p.replace(altsep, sep)
51 if normp[:1] == sep:
52 if normp[1:2] == sep:
53 # UNC drives, e.g. \\server\share or \\?\UNC\server\share
54 # Device drives, e.g. \\.\device or \\?\device
55 start = 8 if normp[:8].upper() == unc_prefix else 2
56 index = normp.find(sep, start)
57 if index == -1:
58 return p, empty, empty
59 index2 = normp.find(sep, index + 1)
60 if index2 == -1:
61 return p, empty, empty
62 return p[:index2], p[index2:index2 + 1], p[index2 + 1:]
63 else:
64 # Relative path with root, e.g. \Windows
65 return empty, p[:1], p[1:]
66 elif normp[1:2] == colon:
67 if normp[2:3] == sep:
68 # Absolute drive-letter path, e.g. X:\Windows
69 return p[:2], p[2:3], p[3:]
70 else:
71 # Relative path with drive, e.g. X:Windows
72 return p[:2], empty, p[2:]
73 else:
74 # Relative path, e.g. Windows
75 return empty, empty, p
76
77 else:
78 raise ImportError('no os specific module found')