@@ -199,7 +199,7 @@ class ImportHandle(BaseHandle):
for _ in range ( Node . level - 1 ) :
parent_dir = os . path . dirname ( parent_dir )
if module :
SearchExtensions = [ ' .pyi ' , ' .py ' ]
SearchExtensions = [ ' .vpi ' , ' .vp ' , ' . pyi ' , ' .py ' ]
sub_path_base = os . path . join ( parent_dir , module )
for ext in SearchExtensions :
candidate = sub_path_base + ext
@@ -215,7 +215,7 @@ class ImportHandle(BaseHandle):
if target_sha1 :
temp_dir = getattr ( Gen , ' _temp_dir ' , None )
if temp_dir :
sha1_pyi = os . path . join ( temp_dir , f " { target_sha1 } .py i " )
sha1_pyi = os . path . join ( temp_dir , f " { target_sha1 } .v pi " )
if os . path . isfile ( sha1_pyi ) :
self . Trans . _ImportedModules . add ( module )
self . _LoadModuleDeclarationsFromFile ( sha1_pyi , Gen , module_name = module , register_module_name = current_module )
@@ -224,7 +224,7 @@ class ImportHandle(BaseHandle):
else :
mod_dir = parent_dir
SearchExtensions = [ ' .pyi ' , ' .py ' ]
SearchExtensions = [ ' .vpi ' , ' .vp ' , ' . pyi ' , ' .py ' ]
pkg_name = ' '
if not module :
# When 'from . import name' is used, Load the package's __init__
@@ -251,7 +251,7 @@ class ImportHandle(BaseHandle):
if target_sha1 :
temp_dir = getattr ( Gen , ' _temp_dir ' , None )
if temp_dir :
sha1_pyi = os . path . join ( temp_dir , f " { target_sha1 } .py i " )
sha1_pyi = os . path . join ( temp_dir , f " { target_sha1 } .v pi " )
if os . path . isfile ( sha1_pyi ) :
self . Trans . _ImportedModules . add ( sub_ModulePath )
self . _LoadModuleDeclarationsFromFile ( sha1_pyi , Gen , module_name = alias . name , register_module_name = current_module )
@@ -274,8 +274,8 @@ class ImportHandle(BaseHandle):
if not module_prefix :
return
basename : str = os . path . basename ( pyi_path )
# 若传入 .py 文件,则从 _source_module_sig_files 查找对应的 {sha1}.py i
if basename . endswith ( ' .py ' ) :
# 若传入 .vp/ .py 文件,则从 _source_module_sig_files 查找对应的 {sha1}.v pi
if basename . endswith ( ' .vp ' ) or basename . endswith ( ' . py ' ) :
source_sig_files = getattr ( self . Trans , ' _source_module_sig_files ' , None )
if source_sig_files :
sig_path = source_sig_files . get ( module_prefix )
@@ -286,9 +286,9 @@ class ImportHandle(BaseHandle):
return
else :
return
if not basename . endswith ( ' .pyi ' ) :
if not ( basename . endswith ( ' .vpi ' ) or basename . endswith ( ' .pyi ' ) ) :
return
sha1 : str = basename [ : - len ( ' .pyi ' ) ]
sha1 : str = basename [ : - 4 ] # 去掉 .vpi 或 .pyi 后缀(均为 4 字符)
shared_doc_meta = getattr ( self . Trans , ' _shared_doc_meta ' , None )
if not shared_doc_meta :
return
@@ -390,7 +390,7 @@ class ImportHandle(BaseHandle):
for _ in range ( node . level - 1 ) :
parent_dir = os . path . dirname ( parent_dir )
if node . module :
SearchExtensions = [ ' .pyi ' , ' .py ' ]
SearchExtensions = [ ' .vpi ' , ' .vp ' , ' . pyi ' , ' .py ' ]
sub_path_base = os . path . join ( parent_dir , node . module )
found_sub = False
# 治本修复:用包目录名(如 'json')而非 register_module_name( 当前编译模块名, 如 '_dict')。
@@ -428,7 +428,7 @@ class ImportHandle(BaseHandle):
if target_sha1 :
temp_dir = getattr ( Gen , ' _temp_dir ' , None )
if temp_dir :
sha1_pyi = os . path . join ( temp_dir , f " { target_sha1 } .py i " )
sha1_pyi = os . path . join ( temp_dir , f " { target_sha1 } .v pi " )
if os . path . isfile ( sha1_pyi ) :
if not getattr ( self . Trans , ' _ImportedModules ' , None ) :
self . Trans . _ImportedModules = set ( )
@@ -440,7 +440,7 @@ class ImportHandle(BaseHandle):
self . _LoadModuleDeclarationsFromFile ( sha1_pyi , Gen , module_name = alias . name , register_module_name = register_module_name , actual_module_name = actual_module , reexport_package = reexport_package or module_name )
else :
mod_dir = parent_dir
SearchExtensions = [ ' .pyi ' , ' .py ' ]
SearchExtensions = [ ' .vpi ' , ' .vp ' , ' . pyi ' , ' .py ' ]
# Load __init__.py to make package-level names available
self . _LoadPackageInitForRelativeImport ( mod_dir , Gen , register_module_name )
# 治本修复:用包目录名(如 'json')而非 register_module_name( 当前编译模块名, 如 '_dict')。
@@ -472,7 +472,7 @@ class ImportHandle(BaseHandle):
if target_sha1 :
temp_dir = getattr ( Gen , ' _temp_dir ' , None )
if temp_dir :
sha1_pyi = os . path . join ( temp_dir , f " { target_sha1 } .py i " )
sha1_pyi = os . path . join ( temp_dir , f " { target_sha1 } .v pi " )
if os . path . isfile ( sha1_pyi ) :
if not getattr ( self . Trans , ' _ImportedModules ' , None ) :
self . Trans . _ImportedModules = set ( )
@@ -528,14 +528,14 @@ class ImportHandle(BaseHandle):
if pkg_sha1 :
temp_dir = getattr ( Gen , ' _temp_dir ' , None )
if temp_dir :
sha1_pyi = os . path . join ( temp_dir , f " { pkg_sha1 } .py i " )
sha1_pyi = os . path . join ( temp_dir , f " { pkg_sha1 } .v pi " )
if os . path . isfile ( sha1_pyi ) :
self . _LoadModuleDeclarationsFromFile ( sha1_pyi , Gen , module_name = pkg_name , register_module_name = register_module_name )
init_Loaded = True
# Fallback: try Loading __init__.py directly from the package directory
if not init_Loaded :
for ext in [ ' .pyi ' , ' .py ' ] :
for ext in [ ' .vpi ' , ' .vp ' , ' . pyi ' , ' .py ' ] :
init_path = os . path . join ( mod_dir , ' __init__ ' + ext )
if os . path . isfile ( init_path ) :
self . _LoadModuleDeclarationsFromFile ( init_path , Gen , module_name = pkg_name , register_module_name = register_module_name )
@@ -552,48 +552,59 @@ class ImportHandle(BaseHandle):
self . _emitted_module_decls . add ( decl_key )
if module_name in ( ' pyzlib ' , ) :
source_sig_files = getattr ( self . Trans , ' _source_module_sig_files ' , None )
ModulePath = module_name . replace ( ' . ' , os . sep ) + ' .pyi '
PackageInitPath = module_name . replace ( ' . ' , os . sep ) + os . sep + ' __init__.pyi '
ModulePathBase = module_name . replace ( ' . ' , os . sep )
PackageInitPathBase = module_name . replace ( ' . ' , os . sep ) + os . sep + ' __init__ '
ProjectRoot = os . path . dirname ( os . path . abspath ( getattr ( self . Trans , ' CurrentFile ' , ' ' ) or ' ' ) ) or os . getcwd ( )
HandlesFile = os . path . abspath ( inspect . getfile ( self . __class__ ) )
TransPyCRoot = os . path . dirname ( os . path . dirname ( os . path . dirname ( os . path . dirname ( HandlesFile ) ) ) )
FullModulePath = None
SearchDirs = [ os . path . join ( TransPyCRoot , ' includes ' ) , os . path . join ( TransPyCRoot , ' lib ' , ' includes ' ) ] + list ( self . Trans . LibraryPaths )
for LibPath in SearchDirs :
SearchPath = os . path . join ( ProjectRoot , LibPath ) if not os . path . isabs ( LibPath ) else LibPath
CandidatePath = os . path . join ( SearchPath , ModulePath )
if os . path . isfile ( CandidatePath ) :
FullModul ePath = CandidatePath
# 优先尝试 .vpi, 回退 .pyi
for stub_ext in ( ' .vpi ' , ' .pyi ' ) :
Candidat ePath = os . path . join ( SearchPath , ModulePathBase + stub_ext )
if os . path . isfile ( CandidatePath ) :
FullModulePath = CandidatePath
break
CandidatePath = os . path . join ( SearchPath , PackageInitPathBase + stub_ext )
if os . path . isfile ( CandidatePath ) :
FullModulePath = CandidatePath
break
if FullModulePath :
break
CandidatePath = os . path . join ( SearchPath , PackageInitPath )
if os . path . isfile ( CandidatePath ) :
FullModulePath = CandidatePath
break
ModuleParts = ModulePath . split ( os . sep )
ModuleParts = ModulePathBase . split ( os . sep )
if len ( ModuleParts ) > 1 :
LibPathBasename = os . path . basename ( LibPath . rstrip ( ' / \\ ' ) )
if LibPathBasename == ModuleParts [ 0 ] :
RemainingPath = os . sep . join ( ModuleParts [ 1 : ] )
CandidatePath = os . path . join ( SearchPath , RemainingPath )
if os . path . isfile ( CandidatePath ) :
FullModulePath = CandidatePath
for stub_ext in ( ' .vpi ' , ' .pyi ' ) :
CandidatePath = os . path . join ( SearchPath , RemainingPath + stub_ext )
if os . path . isfile ( CandidatePath ) :
FullModulePath = CandidatePath
break
if FullModulePath :
break
if not FullModulePath :
ModulePathPy = module_name . replace ( ' . ' , os . sep ) + ' .py '
PackageInitPathPy = module_name . replace ( ' . ' , os . sep ) + os . sep + ' __init__.py '
ModulePathPyBase = module_name . replace ( ' . ' , os . sep )
PackageInitPathPyBase = module_name . replace ( ' . ' , os . sep ) + os . sep + ' __init__ '
SearchDirsForPy = [ os . path . join ( TransPyCRoot , ' includes ' ) , os . path . join ( TransPyCRoot , ' lib ' , ' includes ' ) ] + list ( self . Trans . LibraryPaths )
for LibPath in SearchDirsForPy :
SearchPath = os . path . join ( ProjectRoot , LibPath ) if not os . path . isabs ( LibPath ) else LibPath
CandidatePath = os . path . join ( SearchPath , ModulePathPy )
if os . path . isfile ( CandidatePath ) :
FullModul ePath = CandidatePath
break
CandidatePath = os . path . join ( SearchPath , PackageInitPathPy )
if os . path . isfile ( CandidatePath ) :
FullModul ePath = CandidatePath
# 优先尝试 .vp, 回退 .py
for src_ext in ( ' .vp ' , ' .py ' ) :
Candidat ePath = os . path . join ( SearchPath , ModulePathPyBase + src_ext )
if os . path . isfile ( CandidatePath ) :
FullModulePath = CandidatePath
break
Candidat ePath = os . path . join ( SearchPath , PackageInitPathPyBase + src_ext )
if os . path . isfile ( CandidatePath ) :
FullModulePath = CandidatePath
break
if FullModulePath :
break
if not FullModulePath or not os . path . isfile ( FullModulePath ) :
source_sig_files = getattr ( self . Trans , ' _source_module_sig_files ' , None )
@@ -622,11 +633,11 @@ class ImportHandle(BaseHandle):
for root , dirs , files in os . walk ( SearchPath ) :
if ' __pycache__ ' in root :
continue
if module_name + ' .py ' in files :
FullModulePath = os . path . join ( root , module_name + ' .py ' )
break
if module_name + ' .pyi ' in files :
FullModulePath = os . path . join ( root , module_name + ' .pyi ' )
for src_ext in ( ' .vp ' , ' .py ' , ' .vpi ' , ' .pyi ' ) :
if module_name + src_ext in files :
FullModulePath = os . path . join ( root , module_name + src_ext )
break
if FullModulePath :
break
if FullModulePath :
break
@@ -687,7 +698,7 @@ class ImportHandle(BaseHandle):
for _ in range ( node . level - 1 ) :
parent_dir = os . path . dirname ( parent_dir )
if node . module :
SearchExtensions = [ ' .pyi ' , ' .py ' ]
SearchExtensions = [ ' .vpi ' , ' .vp ' , ' . pyi ' , ' .py ' ]
sub_path_base = os . path . join ( parent_dir , node . module )
found_sub = False
for ext in SearchExtensions :
@@ -715,7 +726,7 @@ class ImportHandle(BaseHandle):
if target_sha1 :
temp_dir = getattr ( Gen , ' _temp_dir ' , None )
if temp_dir :
sha1_pyi = os . path . join ( temp_dir , f " { target_sha1 } .py i " )
sha1_pyi = os . path . join ( temp_dir , f " { target_sha1 } .v pi " )
if os . path . isfile ( sha1_pyi ) :
if not getattr ( self . Trans , ' _ImportedModules ' , None ) :
self . Trans . _ImportedModules = set ( )
@@ -728,7 +739,7 @@ class ImportHandle(BaseHandle):
has_functions_or_classes = True
else :
mod_dir = parent_dir
SearchExtensions = [ ' .pyi ' , ' .py ' ]
SearchExtensions = [ ' .vpi ' , ' .vp ' , ' . pyi ' , ' .py ' ]
# Load __init__.py to make package-level names available
self . _LoadPackageInitForRelativeImport ( mod_dir , Gen , register_module_name or module_name )
for alias in node . names :
@@ -755,7 +766,7 @@ class ImportHandle(BaseHandle):
if target_sha1 :
temp_dir = getattr ( Gen , ' _temp_dir ' , None )
if temp_dir :
sha1_pyi = os . path . join ( temp_dir , f " { target_sha1 } .py i " )
sha1_pyi = os . path . join ( temp_dir , f " { target_sha1 } .v pi " )
if os . path . isfile ( sha1_pyi ) :
if not getattr ( self . Trans , ' _ImportedModules ' , None ) :
self . Trans . _ImportedModules = set ( )
@@ -2063,8 +2074,12 @@ class ImportHandle(BaseHandle):
temp_dir = os . path . join ( ProjectRoot , ' temp ' )
if class_name in Gen . class_members and len ( Gen . class_members [ class_name ] ) > 0 :
return
pyi_basename = stub_filename . replace ( ' .stub.ll ' , ' .py i ' )
pyi_basename = stub_filename . replace ( ' .stub.ll ' , ' .v pi ' )
pyi_path = os . path . join ( temp_dir , pyi_basename )
if not os . path . isfile ( pyi_path ) :
# 回退读取旧缓存 .pyi
pyi_basename = stub_filename . replace ( ' .stub.ll ' , ' .pyi ' )
pyi_path = os . path . join ( temp_dir , pyi_basename )
if self . _pyi_cache_dir != temp_dir :
self . _pyi_cache . clear ( )
@@ -2286,7 +2301,7 @@ class ImportHandle(BaseHandle):
if module_name :
for key , val in source_sig_files . items ( ) :
if key == module_name or key . endswith ( ' . ' + module_name ) :
target_sha1 = os . path . basename ( val ) . replace ( ' .pyi ' , ' ' )
target_sha1 = os . path . basename ( val ) . replace ( ' .vpi ' , ' ' ) . replace ( ' . pyi ' , ' ' )
break
if not target_sha1 :
ModuleSha1Map = getattr ( Gen , ' ModuleSha1Map ' , { } )