# setuptools-lean package stub

# Running the re-export in a function prevents variable names
# in the stub code from clashing with the exported names
def _lean():
    import os
    import sys
    from importlib import import_module
    from importlib.util import module_from_spec

    # Bind builtins as locals. Name lookup falls back to this
    # module's namespace, where a Lean export could shadow the builtin.
    from builtins import TypeError, frozenset, getattr, globals, vars

    # On Windows, PATH doesn't affect DLL resolution for extensions.
    # Add the directory containing Lean's shared libraries so they are found.
    if sys.platform == "win32":
        os.add_dll_directory(os.path.join(os.path.dirname(__file__), "_lean.libs"))

    ns = globals()
    # The syntax `from . import _lean` is avoided since it
    # reuses any `_lean` in scope (e.g., the stub function).
    _lean = import_module('._lean', __name__)
    # Free the `_lean` name for extension use. The previous `import_module`
    # will also set `_lean` on this package, its parent, on an initial load
    # (but not on a reload).
    ns.pop('_lean')

    # Attributes the import system sets on a module, obtained from the import
    # system itself. A Lean export cannot claim these. CPython also designates
    # `__builtins__` as an implementation detail that must be left alone.
    reserved = frozenset(vars(module_from_spec(__spec__))) | {'__builtins__'}

    # Re-export the extension's names, private and dunder ones included.
    for name, obj in vars(_lean).items():
        if name in reserved:
            continue
        ns[name] = obj
        # Use this module instead of `_lean` for re-exported definitions
        if getattr(obj, '__module__', None) == _lean.__name__:
            try:
                obj.__module__ = __name__
            except TypeError:
                pass  # immutable type; it keeps the extension's name
    ns['__doc__'] = _lean.__doc__  # the one reserved name the Lean module owns

_lean()
