]> jfr.im git - yt-dlp.git/commitdiff
[compat] Fix `compat.WINDOWS_VT_MODE`
authorpukkandan <redacted>
Fri, 24 Jun 2022 10:10:13 +0000 (15:40 +0530)
committerpukkandan <redacted>
Fri, 24 Jun 2022 18:38:52 +0000 (00:08 +0530)
test/test_compat.py
yt_dlp/compat/_legacy.py
yt_dlp/compat/compat_utils.py

index 224175c6581ca4550479298f2bb5006f81dd4076..ce95a6afaf6fd47dd7b7ff84d69f4b8af2163c26 100644 (file)
@@ -26,6 +26,9 @@ def test_compat_passthrough(self):
         with self.assertWarns(DeprecationWarning):
             compat.compat_basestring
 
+        with self.assertWarns(DeprecationWarning):
+            compat.WINDOWS_VT_MODE
+
         compat.asyncio.events  # Must not raise error
 
     def test_compat_getenv(self):
index c4d95e1fbd1f02b16641fc7e696dbf6bc3c957be..79461617d5d7d10bdc1932c8bb1cef8087ef4232 100644 (file)
@@ -14,6 +14,7 @@
 import xml.etree.ElementTree as etree
 from subprocess import DEVNULL
 
+from .compat_utils import passthrough_module
 from .asyncio import run as compat_asyncio_run  # noqa: F401
 from .re import Pattern as compat_Pattern  # noqa: F401
 from .re import match as compat_Match  # noqa: F401
@@ -22,6 +23,9 @@
 from ..dependencies import websockets as compat_websockets  # noqa: F401
 
 
+passthrough_module(__name__, '...utils', ('WINDOWS_VT_MODE', 'windows_enable_vt_mode'))
+
+
 # compat_ctypes_WINFUNCTYPE = ctypes.WINFUNCTYPE
 # will not work since ctypes.WINFUNCTYPE does not exist in UNIX machines
 def compat_ctypes_WINFUNCTYPE(*args, **kwargs):
@@ -55,10 +59,3 @@ def compat_ctypes_WINFUNCTYPE(*args, **kwargs):
 compat_xpath = lambda xpath: xpath
 compat_zip = zip
 workaround_optparse_bug9161 = lambda: None
-
-
-def __getattr__(name):
-    if name in ('WINDOWS_VT_MODE', 'windows_enable_vt_mode'):
-        from .. import utils
-        return getattr(utils, name)
-    raise AttributeError(name)
index 8da4fcc0a22243df88843f128199bec640632efa..79e253056b584380905b002749cfc8a78f7acbe5 100644 (file)
@@ -31,7 +31,7 @@ def _is_package(module):
     return True
 
 
-def passthrough_module(parent, child, *, callback=lambda _: None):
+def passthrough_module(parent, child, allowed_attributes=None, *, callback=lambda _: None):
     parent_module = importlib.import_module(parent)
     child_module = None  # Import child module only as needed
 
@@ -41,22 +41,30 @@ def __getattr__(self, attr):
                 with contextlib.suppress(ImportError):
                     return importlib.import_module(f'.{attr}', parent)
 
+            ret = self.__from_child(attr)
+            if ret is _NO_ATTRIBUTE:
+                raise AttributeError(f'module {parent} has no attribute {attr}')
+            callback(attr)
+            return ret
+
+        def __from_child(self, attr):
+            if allowed_attributes is None:
+                if attr.startswith('__') and attr.endswith('__'):
+                    return _NO_ATTRIBUTE
+            elif attr not in allowed_attributes:
+                return _NO_ATTRIBUTE
+
             nonlocal child_module
             child_module = child_module or importlib.import_module(child, parent)
 
-            ret = _NO_ATTRIBUTE
             with contextlib.suppress(AttributeError):
-                ret = getattr(child_module, attr)
+                return getattr(child_module, attr)
 
             if _is_package(child_module):
                 with contextlib.suppress(ImportError):
-                    ret = importlib.import_module(f'.{attr}', child)
+                    return importlib.import_module(f'.{attr}', child)
 
-            if ret is _NO_ATTRIBUTE:
-                raise AttributeError(f'module {parent} has no attribute {attr}')
-
-            callback(attr)
-            return ret
+            return _NO_ATTRIBUTE
 
     # Python 3.6 does not have module level __getattr__
     # https://peps.python.org/pep-0562/