fix(imports): 相对导入 fail-fast 误杀 includes 顶级文件
includes 顶级文件(如 viperstring.vp 的 from .string import *)无包上下文(current_package=None)是常态,此时相对导入语义=同目录/根目录模块,回退 module 是正确解析而非降级。恢复 current_package is None / 上溯越界 / pkg 为空的回退,避免误报 ImportError。
This commit is contained in:
1 parent
9d3483e97e
commit
03df4f1a20
1 file changed
+8
-6
@@ -381,8 +381,10 @@ class ImportsGenMixin:
|
||||
if level <= 0:
|
||||
return module
|
||||
if current_package is None:
|
||||
# 相对导入但无当前包上下文:直接报错终止(fail-fast),暴露上游根因
|
||||
HandlesType.fatal_error(None, f"相对导入解析失败:level={level} module='{module}'", "ImportError")
|
||||
# 无包上下文(includes 顶级文件 / App 顶层文件):
|
||||
# `from .X import Y` 的语义 = "同目录/根目录的 X 模块"
|
||||
# (如 includes/viperstring.vp 的 `from .string import *`)。
|
||||
# 直接返回 module 是正确解析,不是降级——禁止 fail-fast。
|
||||
return module
|
||||
|
||||
# 从 current_package 开始,上溯 (level-1) 级
|
||||
@@ -399,8 +401,9 @@ class ImportsGenMixin:
|
||||
found = 1
|
||||
i += 1
|
||||
if found == 0:
|
||||
# 无更多父级(相对导入越界):直接报错终止(fail-fast)
|
||||
HandlesType.fatal_error(None, f"相对导入解析失败:level={level} module='{module}'", "ImportError")
|
||||
# 无更多父级(相对导入级别超过包深度,上溯到根目录):
|
||||
# 回退到 module 原始值(根目录模块语义),后续查找失败
|
||||
# 由 import SHA1 检查明确报"模块未找到",此处不误杀。
|
||||
pkg = None
|
||||
break
|
||||
# 截断到最后一个 '.' 处
|
||||
@@ -417,8 +420,7 @@ class ImportsGenMixin:
|
||||
# from . import Y → 模块就是包本身
|
||||
return pkg
|
||||
if pkg is None:
|
||||
# 包已上溯到空(相对导入越界):直接报错终止(fail-fast)
|
||||
HandlesType.fatal_error(None, f"相对导入解析失败:level={level} module='{module}'", "ImportError")
|
||||
# 包已上溯到空(相对导入越界):回退到 module 原始值
|
||||
return module
|
||||
# 拼接 pkg + "." + module
|
||||
pkg_len2: t.CSizeT = string.strlen(pkg)
|
||||
|
||||
Reference in new issue
Block a user