|
1 | | -# code package |
| 1 | +""" |
| 2 | +Compatibility helpers for the local ``code`` package. |
| 3 | +
|
| 4 | +The project reuses the stdlib module name ``code`` for its own package, |
| 5 | +which can break third-party imports that expect the built-in module |
| 6 | +(``from code import InteractiveConsole`` for example). We proxy those |
| 7 | +requests back to the real stdlib module so that our package can coexist |
| 8 | +with prebuilt libraries that rely on it. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import importlib |
| 14 | +import importlib.util |
| 15 | +import os |
| 16 | +import sys |
| 17 | +import sysconfig |
| 18 | +from functools import lru_cache |
| 19 | +from types import ModuleType |
| 20 | +from typing import Any, Iterable, Set |
| 21 | + |
| 22 | +__all__: list[str] = [] |
| 23 | + |
| 24 | +_PACKAGE_DIR = os.path.dirname(__file__) |
| 25 | +_STDLIB_BOOTSTRAP_KEY = "code._stdlib_bootstrap" |
| 26 | + |
| 27 | + |
| 28 | +def _register_package_path() -> None: |
| 29 | + """ |
| 30 | + Guarantee that sibling modules remain importable via absolute paths |
| 31 | + even when the project is executed outside of an installed environment. |
| 32 | + """ |
| 33 | + project_root = os.path.dirname(_PACKAGE_DIR) |
| 34 | + candidates = [project_root, _PACKAGE_DIR] |
| 35 | + for path in candidates: |
| 36 | + if path and path not in sys.path: |
| 37 | + sys.path.insert(0, path) |
| 38 | + |
| 39 | + |
| 40 | +_register_package_path() |
| 41 | + |
| 42 | + |
| 43 | +def _stdlib_candidates() -> Iterable[str]: |
| 44 | + """Yield plausible filesystem locations for the stdlib ``code`` module.""" |
| 45 | + seen: Set[str] = set() |
| 46 | + for key in ("stdlib", "platstdlib"): |
| 47 | + path = sysconfig.get_path(key) |
| 48 | + if not path: |
| 49 | + continue |
| 50 | + candidate = os.path.join(path, "code.py") |
| 51 | + if candidate not in seen and os.path.exists(candidate): |
| 52 | + seen.add(candidate) |
| 53 | + yield candidate |
| 54 | + |
| 55 | + |
| 56 | +@lru_cache(maxsize=1) |
| 57 | +def _load_stdlib_code() -> ModuleType | None: |
| 58 | + """Import the stdlib ``code`` module from disk without clobbering this package.""" |
| 59 | + # If sitecustomize stashed the original module, prefer that. |
| 60 | + existing = sys.modules.get(_STDLIB_BOOTSTRAP_KEY) |
| 61 | + if existing: |
| 62 | + return existing |
| 63 | + |
| 64 | + for module_path in _stdlib_candidates(): |
| 65 | + spec = importlib.util.spec_from_file_location("code.__stdlib", module_path) |
| 66 | + if spec and spec.loader: |
| 67 | + module = importlib.util.module_from_spec(spec) |
| 68 | + spec.loader.exec_module(module) |
| 69 | + sys.modules.setdefault("code._stdlib", module) |
| 70 | + return module |
| 71 | + return None |
| 72 | + |
| 73 | + |
| 74 | +def __getattr__(name: str) -> Any: |
| 75 | + """ |
| 76 | + Lazily proxy missing attributes to the stdlib ``code`` module. |
| 77 | +
|
| 78 | + This keeps ``from code import InteractiveConsole`` working even though |
| 79 | + the project shadows the module name with a package. |
| 80 | + """ |
| 81 | + stdlib_module = _load_stdlib_code() |
| 82 | + if stdlib_module and hasattr(stdlib_module, name): |
| 83 | + value = getattr(stdlib_module, name) |
| 84 | + # Cache the attribute locally so repeated lookups are fast. |
| 85 | + globals()[name] = value |
| 86 | + return value |
| 87 | + raise AttributeError(f"module 'code' has no attribute '{name}'") |
| 88 | + |
| 89 | + |
| 90 | +def __dir__() -> list[str]: |
| 91 | + """Combine local attributes with any public names from the stdlib module.""" |
| 92 | + names = set(globals()) |
| 93 | + stdlib_module = _load_stdlib_code() |
| 94 | + if stdlib_module: |
| 95 | + names.update(attr for attr in dir(stdlib_module) if not attr.startswith("_")) |
| 96 | + return sorted(names) |
| 97 | + |
| 98 | + |
| 99 | +# expose project submodules through attribute access |
| 100 | +def _expose_submodules() -> None: |
| 101 | + submodules = [ |
| 102 | + "main_handler", |
| 103 | + "data_processing", |
| 104 | + "transfer.path_logic", |
| 105 | + ] |
| 106 | + for dotted in submodules: |
| 107 | + short = dotted.split(".")[0] |
| 108 | + try: |
| 109 | + module = importlib.import_module(f"{__name__}.{dotted}") |
| 110 | + except ModuleNotFoundError: |
| 111 | + continue |
| 112 | + globals()[short] = importlib.import_module(f"{__name__}.{short}") |
| 113 | + sys.modules.setdefault(f"{__name__}.{short}", globals()[short]) |
| 114 | + sys.modules.setdefault(f"{__name__}.{dotted}", module) |
| 115 | + |
| 116 | + |
| 117 | +_expose_submodules() |
| 118 | + |
| 119 | + |
| 120 | +# Populate __all__ with any names proxied from the stdlib module so |
| 121 | +# star-import behaviour mirrors the original as closely as possible. |
| 122 | +stdlib_module = _load_stdlib_code() |
| 123 | +if stdlib_module: |
| 124 | + proxy_names = [attr for attr in dir(stdlib_module) if not attr.startswith("_")] |
| 125 | + __all__.extend(sorted(proxy_names)) |
0 commit comments