gemini-code-assist[bot] commented on code in PR #19731:
URL: https://github.com/apache/tvm/pull/19731#discussion_r3394025617


##########
python/tvm/script/__init__.py:
##########
@@ -175,13 +183,24 @@ class _AliasLoader:
 
     def __init__(self, module):
         self._module = module
+        # ``module_from_spec`` unconditionally stamps the alias spec onto the
+        # module returned by ``create_module``; capture the canonical values
+        # so ``exec_module`` can restore them.
+        self._spec = getattr(module, "__spec__", None)
+        self._loader = getattr(module, "__loader__", None)

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   To prevent mutating the canonical module's identity and package context, we 
should also capture and restore the original `__name__` and `__package__` 
attributes. Otherwise, the canonical module's `__name__` and `__package__` will 
be permanently overwritten with the alias's values, which can break logging, 
debugging, or other name-based lookups.
   
   ```suggestion
       def __init__(self, module):
           self._module = module
           # ``module_from_spec`` unconditionally stamps the alias spec onto the
           # module returned by ``create_module``; capture the canonical values
           # so ``exec_module`` can restore them.
           self._name = getattr(module, "__name__", None)
           self._package = getattr(module, "__package__", None)
           self._spec = getattr(module, "__spec__", None)
           self._loader = getattr(module, "__loader__", None)
   ```



##########
python/tvm/script/__init__.py:
##########
@@ -175,13 +183,24 @@ class _AliasLoader:
 
     def __init__(self, module):
         self._module = module
+        # ``module_from_spec`` unconditionally stamps the alias spec onto the
+        # module returned by ``create_module``; capture the canonical values
+        # so ``exec_module`` can restore them.
+        self._spec = getattr(module, "__spec__", None)
+        self._loader = getattr(module, "__loader__", None)
 
     def create_module(self, spec):
         return self._module
 
     def exec_module(self, module):
-        # Module is already populated by the redirect target.
-        return None
+        # Module is already populated by the redirect target; just restore
+        # the canonical ``__spec__``/``__loader__`` that the import machinery
+        # overwrote with the alias spec (a stale alias ``__spec__.parent``
+        # breaks relative imports inside the module).
+        if self._spec is not None:
+            module.__spec__ = self._spec
+        if self._loader is not None:
+            module.__loader__ = self._loader

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Restore the captured canonical `__name__` and `__package__` attributes here 
to ensure the canonical module remains completely unmodified by the aliasing 
process.
   
   ```suggestion
       def exec_module(self, module):
           # Module is already populated by the redirect target; just restore
           # the canonical attributes that the import machinery overwrote with
           # the alias spec (a stale alias ``__spec__.parent`` breaks relative
           # imports inside the module).
           if self._name is not None:
               module.__name__ = self._name
           if self._package is not None:
               module.__package__ = self._package
           if self._spec is not None:
               module.__spec__ = self._spec
           if self._loader is not None:
               module.__loader__ = self._loader
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to