aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpukkandan <pukkandan.ytdlp@gmail.com>2023-02-09 01:34:39 +0530
committerpukkandan <pukkandan.ytdlp@gmail.com>2023-02-09 01:47:13 +0530
commit768a00178109508893488e53a0e720b117fbccf6 (patch)
tree102cb1d0816e91e0a0de0bc14b0c22e6025d0cfa
parentacb1042a9ffa8769fe691beac1011d6da1fcf321 (diff)
downloadhypervideo-pre-768a00178109508893488e53a0e720b117fbccf6.tar.lz
hypervideo-pre-768a00178109508893488e53a0e720b117fbccf6.tar.xz
hypervideo-pre-768a00178109508893488e53a0e720b117fbccf6.zip
[compat_utils] Simplify `EnhancedModule`
-rw-r--r--yt_dlp/compat/compat_utils.py18
-rw-r--r--yt_dlp/dependencies/Cryptodome.py8
2 files changed, 7 insertions, 19 deletions
diff --git a/yt_dlp/compat/compat_utils.py b/yt_dlp/compat/compat_utils.py
index f8679c98e..8956b3bf1 100644
--- a/yt_dlp/compat/compat_utils.py
+++ b/yt_dlp/compat/compat_utils.py
@@ -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 @@ class EnhancedModule(types.ModuleType):
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 passthrough_module(parent, child, allowed_attributes=(..., ), *, callback=la
return _NO_ATTRIBUTE
+ parent = sys.modules.get(parent, types.ModuleType(parent))
+ parent.__class__ = EnhancedModule
parent.__getattr__ = __getattr__
return parent
diff --git a/yt_dlp/dependencies/Cryptodome.py b/yt_dlp/dependencies/Cryptodome.py
index 580ce0753..2adc51374 100644
--- a/yt_dlp/dependencies/Cryptodome.py
+++ b/yt_dlp/dependencies/Cryptodome.py
@@ -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 @@ except ImportError:
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