Files
TransPyC/lib/_bootstrap.py
2026-07-18 19:25:40 +08:00

38 lines
1.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""集中化 sys.path 引导模块
在首次导入时一次性设置好编译器运行所需的 sys.path
- 项目根目录:让 ``import TransPyC``、``from StubGen import``、动态 importlib 能解析
- lib/includes让用户注解模块中的 ``import t`` / ``import c`` 能解析
入口点TransPyC.py / Projectrans.py应在最开头 ``import lib._bootstrap``。
lib 内部模块也可 ``import lib._bootstrap`` 作为安全网,重复导入是无副作用的 no-op。
"""
from __future__ import annotations
import os
import sys
_BOOTSTRAPPED: bool = False
def _bootstrap() -> None:
global _BOOTSTRAPPED
if _BOOTSTRAPPED:
return
_BOOTSTRAPPED = True
# lib/_bootstrap.py 所在目录
_LibDir: str = os.path.dirname(os.path.abspath(__file__))
# 项目根目录 = lib 的父目录
_ProjectRoot: str = os.path.dirname(_LibDir)
# lib/includes 目录(编译器内部类型系统 t.py / c.py
_IncludesDir: str = os.path.join(_LibDir, 'includes')
if _ProjectRoot not in sys.path:
sys.path.insert(0, _ProjectRoot)
if _IncludesDir not in sys.path:
sys.path.append(_IncludesDir)
_bootstrap()