aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/compat.py
diff options
context:
space:
mode:
authordirkf <fieldhouse@gmx.net>2024-01-20 18:28:52 +0000
committerdirkf <fieldhouse@gmx.net>2024-01-22 11:10:34 +0000
commit66518714169185195a359e173cef73fba31d76b8 (patch)
treed617f6a28fa9b579ff9e85bbd520127ca1fdac58 /youtube_dl/compat.py
parentbe008e657d79832642e2158557c899249c9e31cd (diff)
[compat] Rework compat for `method` parameter of `compat_urllib_request.Request` constructor
* fixes #32573 * does not break `utils.HEADrequest` (eg)
Diffstat (limited to 'youtube_dl/compat.py')
-rw-r--r--youtube_dl/compat.py27
1 files changed, 17 insertions, 10 deletions
diff --git a/youtube_dl/compat.py b/youtube_dl/compat.py
index 3c526a78d..818ccebd0 100644
--- a/youtube_dl/compat.py
+++ b/youtube_dl/compat.py
@@ -58,19 +58,26 @@ except ImportError: # Python 2
# Also fix up lack of method arg in old Pythons
try:
- _req = compat_urllib_request.Request
- _req('http://127.0.0.1', method='GET')
+ type(compat_urllib_request.Request('http://127.0.0.1', method='GET'))
except TypeError:
- class _request(object):
- def __new__(cls, url, *args, **kwargs):
- method = kwargs.pop('method', None)
- r = _req(url, *args, **kwargs)
- if method:
- r.get_method = types.MethodType(lambda _: method, r)
- return r
+ def _add_init_method_arg(cls):
- compat_urllib_request.Request = _request
+ init = cls.__init__
+ def wrapped_init(self, *args, **kwargs):
+ method = kwargs.pop('method', 'GET')
+ init(self, *args, **kwargs)
+ if any(callable(x.__dict__.get('get_method')) for x in (self.__class__, self) if x != cls):
+ # allow instance or its subclass to override get_method()
+ return
+ if self.has_data() and method == 'GET':
+ method = 'POST'
+ self.get_method = types.MethodType(lambda _: method, self)
+
+ cls.__init__ = wrapped_init
+
+ _add_init_method_arg(compat_urllib_request.Request)
+ del _add_init_method_arg
try:
import urllib.error as compat_urllib_error