@@ -79,7 +79,7 @@ def metacall_inspect():
7979# Save the original Python import
8080__python_import__ = builtins .__import__
8181
82- def __metacall_import__ (self , name , globals = None , locals = None , fromlist = (), level = 0 ):
82+ def __metacall_import__ (name , globals = None , locals = None , fromlist = (), level = 0 ):
8383 def find_handle (handle_name ):
8484 metadata = metacall_inspect ()
8585
@@ -144,13 +144,22 @@ def generate_module(handle_name, handle):
144144 # Probably in the future we can differenciate between them, but it is not trivial
145145 }
146146
147+ # Try to load it as a Python module
148+ mod = None
149+
150+ with suppress (ImportError ):
151+ mod = __python_import__ (name , globals , locals , fromlist , level )
152+
153+ if mod :
154+ return mod
155+
147156 # Obtain the extension of the module if any
148- extension = None if self .count ('.' ) == 0 else self .split ('.' )[- 1 ]
157+ extension = None if name .count ('.' ) == 0 else name .split ('.' )[- 1 ]
149158
150159 # Load by extension if there is any (import puppeteer.js)
151160 if extension and extension in extensions_to_tag .keys ():
152161 # Get handle name without extension
153- handle_name = self .split ('.' )[- 2 ]
162+ handle_name = name .split ('.' )[- 2 ]
154163
155164 # Check if it is already loaded in MetaCall
156165 handle = find_handle (handle_name )
@@ -159,38 +168,28 @@ def generate_module(handle_name, handle):
159168 # Generate the module from cached handle
160169 return generate_module (handle_name , handle )
161170
162- if metacall_load_from_file (extensions_to_tag [extension ], [self ]):
171+ if metacall_load_from_file (extensions_to_tag [extension ], [name ]):
163172 handle = find_handle (handle_name )
164173 if handle != None :
165174 # Generate the module from cached handle
166175 return generate_module (handle_name , handle )
167176 else :
168- # Try to load it as a Python module
169- mod = None
170-
171- with suppress (ImportError ):
172- # TODO: Why level is not needed? Does it depend on the Python version?
173- mod = __python_import__ (self , name , globals , locals , fromlist ) #, level)
174-
175- if mod :
176- return mod
177-
178177 # Check if it is already loaded in MetaCall
179- handle = find_handle (self )
178+ handle = find_handle (name )
180179
181180 if handle != None :
182181 # Generate the module from cached handle
183- return generate_module (self , handle )
182+ return generate_module (name , handle )
184183
185184 # Otherwhise, try to load it by guessing the loader
186185 for tag in list (set (extensions_to_tag .values ())):
187- if metacall_load_from_file (tag , [self ]):
188- handle = find_handle (self )
186+ if metacall_load_from_file (tag , [name ]):
187+ handle = find_handle (name )
189188 if handle != None :
190189 # Generate the module from cached handle
191- return generate_module (self , handle )
190+ return generate_module (name , handle )
192191
193- raise ImportError ('MetaCall could not import:' , self )
192+ raise ImportError ('MetaCall could not import:' , name )
194193
195194# Override Python import
196195builtins .__import__ = __metacall_import__
0 commit comments