name=getattr(module, '_yt_dlp__identifier', module.__name__),
version=str(next(filter(None, (
getattr(module, attr, None)
- for attr in ('__version__', 'version_string', 'version')
+ for attr in ('_yt_dlp__version', '__version__', 'version_string', 'version')
)), None)))
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)()
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):
+ with contextlib.suppress(ModuleNotFoundError):
return importlib.import_module(f'.{attr}', parent.__name__)
ret = from_child(attr)
return _NO_ATTRIBUTE
+ parent = sys.modules.get(parent, types.ModuleType(parent))
+ parent.__class__ = EnhancedModule
parent.__getattr__ = __getattr__
return parent