26 import xml
.etree
.ElementTree
as etree
27 from subprocess
import DEVNULL
30 # HTMLParseError has been deprecated in Python 3.3 and removed in
31 # Python 3.5. Introducing dummy exception for Python >3.5 for compatible
32 # and uniform cross-version exception handling
33 class compat_HTMLParseError(Exception):
37 # compat_ctypes_WINFUNCTYPE = ctypes.WINFUNCTYPE
38 # will not work since ctypes.WINFUNCTYPE does not exist in UNIX machines
39 def compat_ctypes_WINFUNCTYPE(*args
, **kwargs
):
40 return ctypes
.WINFUNCTYPE(*args
, **kwargs
)
43 class _TreeBuilder(etree
.TreeBuilder
):
44 def doctype(self
, name
, pubid
, system
):
48 def compat_etree_fromstring(text
):
49 return etree
.XML(text
, parser
=etree
.XMLParser(target
=_TreeBuilder()))
52 compat_os_name
= os
._name
if os
.name
== 'java' else os
.name
55 if compat_os_name
== 'nt':
56 def compat_shlex_quote(s
):
57 return s
if re
.match(r
'^[-_\w./]+$', s
) else '"%s"' % s
.replace('"', '\\"')
59 from shlex
import quote
as compat_shlex_quote
69 def compat_setenv(key
, value
, env
=os
.environ
):
73 if compat_os_name
== 'nt' and sys
.version_info
< (3, 8):
74 # os.path.realpath on Windows does not follow symbolic links
75 # prior to Python 3.8 (see https://bugs.python.org/issue9949)
76 def compat_realpath(path
):
77 while os
.path
.islink(path
):
78 path
= os
.path
.abspath(os
.readlink(path
))
81 compat_realpath
= os
.path
.realpath
85 assert isinstance(s
, compat_str
)
89 compat_Pattern
= re
.Pattern
90 except AttributeError:
91 compat_Pattern
= type(re
.compile(''))
95 compat_Match
= re
.Match
96 except AttributeError:
97 compat_Match
= type(re
.compile('').match(''))
101 compat_asyncio_run
= asyncio
.run
# >= 3.7
102 except AttributeError:
103 def compat_asyncio_run(coro
):
105 loop
= asyncio
.get_event_loop()
107 loop
= asyncio
.new_event_loop()
108 asyncio
.set_event_loop(loop
)
109 loop
.run_until_complete(coro
)
111 asyncio
.run
= compat_asyncio_run
115 asyncio
.tasks
.all_tasks
116 except AttributeError:
117 asyncio
.tasks
.all_tasks
= asyncio
.tasks
.Task
.all_tasks
120 import websockets
as compat_websockets
122 compat_websockets
= None
124 # Python 3.8+ does not honor %HOME% on windows, but this breaks compatibility with youtube-dl
125 # See https://github.com/yt-dlp/yt-dlp/issues/792
126 # https://docs.python.org/3/library/os.path.html#os.path.expanduser
127 if compat_os_name
in ('nt', 'ce') and 'HOME' in os
.environ
:
128 _userhome
= os
.environ
['HOME']
130 def compat_expanduser(path
):
131 if not path
.startswith('~'):
133 i
= path
.replace('\\', '/', 1).find('/') # ~user
136 userhome
= os
.path
.join(os
.path
.dirname(_userhome
), path
[1:i
]) if i
> 1 else _userhome
137 return userhome
+ path
[i
:]
139 compat_expanduser
= os
.path
.expanduser
143 from Cryptodome
.Cipher
import AES
as compat_pycrypto_AES
146 from Crypto
.Cipher
import AES
as compat_pycrypto_AES
148 compat_pycrypto_AES
= None
151 import brotlicffi
as compat_brotli
154 import brotli
as compat_brotli
158 WINDOWS_VT_MODE
= False if compat_os_name
== 'nt' else None
161 def windows_enable_vt_mode(): # TODO: Do this the proper way https://bugs.python.org/issue30075
162 if compat_os_name
!= 'nt':
164 global WINDOWS_VT_MODE
165 startupinfo
= subprocess
.STARTUPINFO()
166 startupinfo
.dwFlags |
= subprocess
.STARTF_USESHOWWINDOW
168 subprocess
.Popen('', shell
=True, startupinfo
=startupinfo
)
169 WINDOWS_VT_MODE
= True
176 compat_basestring
= str
178 compat_filter
= filter
180 compat_integer_types
= (int, )
181 compat_kwargs
= lambda kwargs
: kwargs
183 compat_numeric_types
= (int, float, complex)
185 compat_xpath
= lambda xpath
: xpath
187 workaround_optparse_bug9161
= lambda: None
189 compat_collections_abc
= collections
.abc
190 compat_HTMLParser
= html
.parser
.HTMLParser
191 compat_HTTPError
= urllib
.error
.HTTPError
192 compat_Struct
= struct
.Struct
193 compat_b64decode
= base64
.b64decode
194 compat_cookiejar
= http
.cookiejar
195 compat_cookiejar_Cookie
= compat_cookiejar
.Cookie
196 compat_cookies
= http
.cookies
197 compat_cookies_SimpleCookie
= compat_cookies
.SimpleCookie
198 compat_etree_Element
= etree
.Element
199 compat_etree_register_namespace
= etree
.register_namespace
200 compat_get_terminal_size
= shutil
.get_terminal_size
201 compat_getenv
= os
.getenv
202 compat_getpass
= getpass
.getpass
203 compat_html_entities
= html
.entities
204 compat_html_entities_html5
= compat_html_entities
.html5
205 compat_http_client
= http
.client
206 compat_http_server
= http
.server
207 compat_itertools_count
= itertools
.count
208 compat_parse_qs
= urllib
.parse
.parse_qs
209 compat_shlex_split
= shlex
.split
210 compat_socket_create_connection
= socket
.create_connection
211 compat_struct_pack
= struct
.pack
212 compat_struct_unpack
= struct
.unpack
213 compat_subprocess_get_DEVNULL
= lambda: DEVNULL
214 compat_tokenize_tokenize
= tokenize
.tokenize
215 compat_urllib_error
= urllib
.error
216 compat_urllib_parse
= urllib
.parse
217 compat_urllib_parse_quote
= urllib
.parse
.quote
218 compat_urllib_parse_quote_plus
= urllib
.parse
.quote_plus
219 compat_urllib_parse_unquote
= urllib
.parse
.unquote
220 compat_urllib_parse_unquote_plus
= urllib
.parse
.unquote_plus
221 compat_urllib_parse_unquote_to_bytes
= urllib
.parse
.unquote_to_bytes
222 compat_urllib_parse_urlencode
= urllib
.parse
.urlencode
223 compat_urllib_parse_urlparse
= urllib
.parse
.urlparse
224 compat_urllib_parse_urlunparse
= urllib
.parse
.urlunparse
225 compat_urllib_request
= urllib
.request
226 compat_urllib_request_DataHandler
= urllib
.request
.DataHandler
227 compat_urllib_response
= urllib
.response
228 compat_urlparse
= urllib
.parse
229 compat_urlretrieve
= urllib
.request
.urlretrieve
230 compat_xml_parse_error
= etree
.ParseError
237 'compat_HTMLParseError',
243 'compat_asyncio_run',
248 'compat_collections_abc',
250 'compat_cookiejar_Cookie',
252 'compat_cookies_SimpleCookie',
253 'compat_ctypes_WINFUNCTYPE',
254 'compat_etree_Element',
255 'compat_etree_fromstring',
256 'compat_etree_register_namespace',
259 'compat_get_terminal_size',
262 'compat_html_entities',
263 'compat_html_entities_html5',
264 'compat_http_client',
265 'compat_http_server',
267 'compat_integer_types',
268 'compat_itertools_count',
271 'compat_numeric_types',
276 'compat_pycrypto_AES',
279 'compat_shlex_quote',
280 'compat_shlex_split',
281 'compat_socket_create_connection',
283 'compat_struct_pack',
284 'compat_struct_unpack',
285 'compat_subprocess_get_DEVNULL',
286 'compat_tokenize_tokenize',
287 'compat_urllib_error',
288 'compat_urllib_parse',
289 'compat_urllib_parse_quote',
290 'compat_urllib_parse_quote_plus',
291 'compat_urllib_parse_unquote',
292 'compat_urllib_parse_unquote_plus',
293 'compat_urllib_parse_unquote_to_bytes',
294 'compat_urllib_parse_urlencode',
295 'compat_urllib_parse_urlparse',
296 'compat_urllib_parse_urlunparse',
297 'compat_urllib_request',
298 'compat_urllib_request_DataHandler',
299 'compat_urllib_response',
301 'compat_urlretrieve',
303 'compat_xml_parse_error',
306 'windows_enable_vt_mode',
307 'workaround_optparse_bug9161',