]>
Commit | Line | Data |
---|---|---|
77f90330 | 1 | import os |
77f90330 | 2 | import sys |
9196cbfe | 3 | import warnings |
77f90330 | 4 | import xml.etree.ElementTree as etree |
5 | ||
77f90330 | 6 | from ._deprecated import * # noqa: F401, F403 |
9196cbfe | 7 | from .compat_utils import passthrough_module |
8 | ||
9196cbfe | 9 | # XXX: Implement this the same way as other DeprecationWarnings without circular import |
f5e438a9 | 10 | passthrough_module(__name__, '._legacy', callback=lambda attr: warnings.warn( |
6929b41a | 11 | DeprecationWarning(f'{__name__}.{attr} is deprecated'), stacklevel=3)) |
77f90330 | 12 | |
13 | ||
14 | # HTMLParseError has been deprecated in Python 3.3 and removed in | |
15 | # Python 3.5. Introducing dummy exception for Python >3.5 for compatible | |
16 | # and uniform cross-version exception handling | |
17 | class compat_HTMLParseError(Exception): | |
18 | pass | |
19 | ||
20 | ||
21 | class _TreeBuilder(etree.TreeBuilder): | |
22 | def doctype(self, name, pubid, system): | |
23 | pass | |
24 | ||
25 | ||
26 | def compat_etree_fromstring(text): | |
27 | return etree.XML(text, parser=etree.XMLParser(target=_TreeBuilder())) | |
28 | ||
29 | ||
30 | compat_os_name = os._name if os.name == 'java' else os.name | |
31 | ||
32 | ||
33 | if compat_os_name == 'nt': | |
34 | def compat_shlex_quote(s): | |
6929b41a | 35 | import re |
77f90330 | 36 | return s if re.match(r'^[-_\w./]+$', s) else '"%s"' % s.replace('"', '\\"') |
37 | else: | |
38 | from shlex import quote as compat_shlex_quote # noqa: F401 | |
39 | ||
40 | ||
41 | def compat_ord(c): | |
42 | return c if isinstance(c, int) else ord(c) | |
43 | ||
44 | ||
77f90330 | 45 | if compat_os_name == 'nt' and sys.version_info < (3, 8): |
46 | # os.path.realpath on Windows does not follow symbolic links | |
47 | # prior to Python 3.8 (see https://bugs.python.org/issue9949) | |
48 | def compat_realpath(path): | |
49 | while os.path.islink(path): | |
50 | path = os.path.abspath(os.readlink(path)) | |
57e0f077 | 51 | return os.path.realpath(path) |
77f90330 | 52 | else: |
53 | compat_realpath = os.path.realpath | |
54 | ||
55 | ||
77f90330 | 56 | # Python 3.8+ does not honor %HOME% on windows, but this breaks compatibility with youtube-dl |
57 | # See https://github.com/yt-dlp/yt-dlp/issues/792 | |
58 | # https://docs.python.org/3/library/os.path.html#os.path.expanduser | |
59 | if compat_os_name in ('nt', 'ce'): | |
60 | def compat_expanduser(path): | |
61 | HOME = os.environ.get('HOME') | |
62 | if not HOME: | |
63 | return os.path.expanduser(path) | |
64 | elif not path.startswith('~'): | |
65 | return path | |
66 | i = path.replace('\\', '/', 1).find('/') # ~user | |
67 | if i < 0: | |
68 | i = len(path) | |
69 | userhome = os.path.join(os.path.dirname(HOME), path[1:i]) if i > 1 else HOME | |
70 | return userhome + path[i:] | |
71 | else: | |
72 | compat_expanduser = os.path.expanduser | |
f5e438a9 | 73 | |
74 | ||
75 | # NB: Add modules that are imported dynamically here so that PyInstaller can find them | |
76 | # See https://github.com/pyinstaller/pyinstaller-hooks-contrib/issues/438 | |
77 | if False: | |
78 | from . import _legacy # noqa: F401 |