25 import xml
.etree
.ElementTree
as etree
26 from subprocess
import DEVNULL
29 # HTMLParseError has been deprecated in Python 3.3 and removed in
30 # Python 3.5. Introducing dummy exception for Python >3.5 for compatible
31 # and uniform cross-version exception handling
32 class compat_HTMLParseError(Exception):
36 # compat_ctypes_WINFUNCTYPE = ctypes.WINFUNCTYPE
37 # will not work since ctypes.WINFUNCTYPE does not exist in UNIX machines
38 def compat_ctypes_WINFUNCTYPE(*args
, **kwargs
):
39 return ctypes
.WINFUNCTYPE(*args
, **kwargs
)
42 class _TreeBuilder(etree
.TreeBuilder
):
43 def doctype(self
, name
, pubid
, system
):
47 def compat_etree_fromstring(text
):
48 return etree
.XML(text
, parser
=etree
.XMLParser(target
=_TreeBuilder()))
51 compat_os_name
= os
._name
if os
.name
== 'java' else os
.name
54 if compat_os_name
== 'nt':
55 def compat_shlex_quote(s
):
56 return s
if re
.match(r
'^[-_\w./]+$', s
) else '"%s"' % s
.replace('"', '\\"')
58 from shlex
import quote
as compat_shlex_quote
68 def compat_setenv(key
, value
, env
=os
.environ
):
72 if compat_os_name
== 'nt' and sys
.version_info
< (3, 8):
73 # os.path.realpath on Windows does not follow symbolic links
74 # prior to Python 3.8 (see https://bugs.python.org/issue9949)
75 def compat_realpath(path
):
76 while os
.path
.islink(path
):
77 path
= os
.path
.abspath(os
.readlink(path
))
80 compat_realpath
= os
.path
.realpath
84 assert isinstance(s
, compat_str
)
88 # Fix https://github.com/ytdl-org/youtube-dl/issues/4223
89 # See http://bugs.python.org/issue9161 for what is broken
90 def workaround_optparse_bug9161():
91 op
= optparse
.OptionParser()
92 og
= optparse
.OptionGroup(op
, 'foo')
96 real_add_option
= optparse
.OptionGroup
.add_option
98 def _compat_add_option(self
, *args
, **kwargs
):
100 v
.encode('ascii', 'replace') if isinstance(v
, compat_str
)
102 bargs
= [enc(a
) for a
in args
]
104 (k
, enc(v
)) for k
, v
in kwargs
.items())
105 return real_add_option(self
, *bargs
, **bkwargs
)
106 optparse
.OptionGroup
.add_option
= _compat_add_option
110 compat_Pattern
= re
.Pattern
111 except AttributeError:
112 compat_Pattern
= type(re
.compile(''))
116 compat_Match
= re
.Match
117 except AttributeError:
118 compat_Match
= type(re
.compile('').match(''))
122 compat_asyncio_run
= asyncio
.run
# >= 3.7
123 except AttributeError:
124 def compat_asyncio_run(coro
):
126 loop
= asyncio
.get_event_loop()
128 loop
= asyncio
.new_event_loop()
129 asyncio
.set_event_loop(loop
)
130 loop
.run_until_complete(coro
)
132 asyncio
.run
= compat_asyncio_run
135 # Python 3.8+ does not honor %HOME% on windows, but this breaks compatibility with youtube-dl
136 # See https://github.com/yt-dlp/yt-dlp/issues/792
137 # https://docs.python.org/3/library/os.path.html#os.path.expanduser
138 if compat_os_name
in ('nt', 'ce') and 'HOME' in os
.environ
:
139 _userhome
= os
.environ
['HOME']
141 def compat_expanduser(path
):
142 if not path
.startswith('~'):
144 i
= path
.replace('\\', '/', 1).find('/') # ~user
147 userhome
= os
.path
.join(os
.path
.dirname(_userhome
), path
[1:i
]) if i
> 1 else _userhome
148 return userhome
+ path
[i
:]
150 compat_expanduser
= os
.path
.expanduser
154 from Cryptodome
.Cipher
import AES
as compat_pycrypto_AES
157 from Crypto
.Cipher
import AES
as compat_pycrypto_AES
159 compat_pycrypto_AES
= None
162 def windows_enable_vt_mode(): # TODO: Do this the proper way https://bugs.python.org/issue30075
163 if compat_os_name
!= 'nt':
170 compat_basestring
= str
173 compat_integer_types
= (int, )
174 compat_kwargs
= lambda kwargs
: kwargs
175 compat_numeric_types
= (int, float, complex)
177 compat_xpath
= lambda xpath
: xpath
180 compat_HTMLParser
= html
.parser
.HTMLParser
181 compat_HTTPError
= urllib
.error
.HTTPError
182 compat_Struct
= struct
.Struct
183 compat_b64decode
= base64
.b64decode
184 compat_cookiejar
= http
.cookiejar
185 compat_cookiejar_Cookie
= compat_cookiejar
.Cookie
186 compat_cookies
= http
.cookies
187 compat_cookies_SimpleCookie
= compat_cookies
.SimpleCookie
188 compat_etree_Element
= etree
.Element
189 compat_etree_register_namespace
= etree
.register_namespace
190 compat_get_terminal_size
= shutil
.get_terminal_size
191 compat_getenv
= os
.getenv
192 compat_getpass
= getpass
.getpass
193 compat_html_entities
= html
.entities
194 compat_html_entities_html5
= compat_html_entities
.html5
195 compat_http_client
= http
.client
196 compat_http_server
= http
.server
197 compat_itertools_count
= itertools
.count
198 compat_parse_qs
= urllib
.parse
.parse_qs
199 compat_shlex_split
= shlex
.split
200 compat_socket_create_connection
= socket
.create_connection
201 compat_struct_pack
= struct
.pack
202 compat_struct_unpack
= struct
.unpack
203 compat_subprocess_get_DEVNULL
= lambda: DEVNULL
204 compat_tokenize_tokenize
= tokenize
.tokenize
205 compat_urllib_error
= urllib
.error
206 compat_urllib_parse
= urllib
.parse
207 compat_urllib_parse_quote
= urllib
.parse
.quote
208 compat_urllib_parse_quote_plus
= urllib
.parse
.quote_plus
209 compat_urllib_parse_unquote
= urllib
.parse
.unquote
210 compat_urllib_parse_unquote_plus
= urllib
.parse
.unquote_plus
211 compat_urllib_parse_unquote_to_bytes
= urllib
.parse
.unquote_to_bytes
212 compat_urllib_parse_urlencode
= urllib
.parse
.urlencode
213 compat_urllib_parse_urlparse
= urllib
.parse
.urlparse
214 compat_urllib_parse_urlunparse
= urllib
.parse
.urlunparse
215 compat_urllib_request
= urllib
.request
216 compat_urllib_request_DataHandler
= urllib
.request
.DataHandler
217 compat_urllib_response
= urllib
.response
218 compat_urlparse
= urllib
.parse
219 compat_urlretrieve
= urllib
.request
.urlretrieve
220 compat_xml_parse_error
= etree
.ParseError
226 'compat_HTMLParseError',
232 'compat_asyncio_run',
237 'compat_cookiejar_Cookie',
239 'compat_cookies_SimpleCookie',
240 'compat_ctypes_WINFUNCTYPE',
241 'compat_etree_Element',
242 'compat_etree_fromstring',
243 'compat_etree_register_namespace',
245 'compat_get_terminal_size',
248 'compat_html_entities',
249 'compat_html_entities_html5',
250 'compat_http_client',
251 'compat_http_server',
253 'compat_integer_types',
254 'compat_itertools_count',
256 'compat_numeric_types',
261 'compat_pycrypto_AES',
264 'compat_shlex_quote',
265 'compat_shlex_split',
266 'compat_socket_create_connection',
268 'compat_struct_pack',
269 'compat_struct_unpack',
270 'compat_subprocess_get_DEVNULL',
271 'compat_tokenize_tokenize',
272 'compat_urllib_error',
273 'compat_urllib_parse',
274 'compat_urllib_parse_quote',
275 'compat_urllib_parse_quote_plus',
276 'compat_urllib_parse_unquote',
277 'compat_urllib_parse_unquote_plus',
278 'compat_urllib_parse_unquote_to_bytes',
279 'compat_urllib_parse_urlencode',
280 'compat_urllib_parse_urlparse',
281 'compat_urllib_parse_urlunparse',
282 'compat_urllib_request',
283 'compat_urllib_request_DataHandler',
284 'compat_urllib_response',
286 'compat_urlretrieve',
287 'compat_xml_parse_error',
290 'windows_enable_vt_mode',
291 'workaround_optparse_bug9161',