]> jfr.im git - yt-dlp.git/commitdiff
[compat_utils] Simplify `EnhancedModule`
authorpukkandan <redacted>
Wed, 8 Feb 2023 20:04:39 +0000 (01:34 +0530)
committerpukkandan <redacted>
Wed, 8 Feb 2023 20:17:13 +0000 (01:47 +0530)
yt_dlp/compat/compat_utils.py
yt_dlp/dependencies/Cryptodome.py

index f8679c98ecf7b10467d9564029e9d2cbfa775c0e..8956b3bf1f7a64b25cd97be5e0f06a0c506e077e 100644 (file)
@@ -28,20 +28,6 @@ def _is_dunder(name):
 
 
 class EnhancedModule(types.ModuleType):
-    def __new__(cls, name, *args, **kwargs):
-        if name not in sys.modules:
-            return super().__new__(cls, name, *args, **kwargs)
-
-        assert not args and not kwargs, 'Cannot pass additional arguments to an existing module'
-        module = sys.modules[name]
-        module.__class__ = cls
-        return module
-
-    def __init__(self, name, *args, **kwargs):
-        # Prevent __new__ from trigerring __init__ again
-        if name not in sys.modules:
-            super().__init__(name, *args, **kwargs)
-
     def __bool__(self):
         return vars(self).get('__bool__', lambda: True)()
 
@@ -60,8 +46,6 @@ def __getattribute__(self, attr):
 
 def passthrough_module(parent, child, allowed_attributes=(..., ), *, callback=lambda _: None):
     """Passthrough parent module into a child module, creating the parent if necessary"""
-    parent = EnhancedModule(parent)
-
     def __getattr__(attr):
         if _is_package(parent):
             with contextlib.suppress(ImportError):
@@ -93,5 +77,7 @@ def from_child(attr):
 
         return _NO_ATTRIBUTE
 
+    parent = sys.modules.get(parent, types.ModuleType(parent))
+    parent.__class__ = EnhancedModule
     parent.__getattr__ = __getattr__
     return parent
index 580ce07533e492b3c38b34df1bf1c5025550cfe3..2adc513740af195891a90b5fbcf10ba8ad7af284 100644 (file)
@@ -1,5 +1,7 @@
+import types
+
 from ..compat import functools
-from ..compat.compat_utils import EnhancedModule, passthrough_module
+from ..compat.compat_utils import passthrough_module
 
 try:
     import Cryptodome as _parent
@@ -7,11 +9,11 @@
     try:
         import Crypto as _parent
     except (ImportError, SyntaxError):  # Old Crypto gives SyntaxError in newer Python
-        _parent = EnhancedModule('Cryptodome')
+        _parent = types.ModuleType('no_Cryptodome')
         __bool__ = lambda: False
 
 passthrough_module(__name__, _parent, (..., '__version__'))
-del passthrough_module, EnhancedModule
+del passthrough_module
 
 
 @property