Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hot reload issue #116

Open
guy-asaert opened this issue Sep 17, 2024 · 3 comments
Open

Hot reload issue #116

guy-asaert opened this issue Sep 17, 2024 · 3 comments

Comments

@guy-asaert
Copy link

guy-asaert commented Sep 17, 2024

Currently running 0.18.7. Our Python code base contains some auto-generated code which we cannot change. The watch_module function in the ImportHelper class in importer.py fails as the supplied module parameter is set to None. No idea why that is the case. But the auto-generated code looks like the culprit. I "fixed" the problem by added a check for module as follows:

    def watch_module(self, module: ModuleType):
        if not module:
            return

        filepath = module.__spec__.origin
        
        if filepath == "frozen" or any((filepath.startswith(x) for x in self._ignore_paths)):
            return
        
        if filepath in self._watched:
            return
        .....

This might have been resolved but because of limitation in my environment I cannot upgrade at the moment. Also can't really send you the code base so might be hard to impossible for you to replicate. Just hoping that this is a known issue and there is an easy solution.

@cunnane
Copy link
Owner

cunnane commented Sep 23, 2024

I'll add this check in. xlOil hooks the import mechanism: in this case it looks like exec_module is being called with no module. I'm not sure under what circumstances this happens but if you have code loading other than the usual way from py files, it could be down to this.

@guy-asaert
Copy link
Author

Thanks for the response. The library that is causing the issue is code generated by ZeroC Ice (https://zeroc.com/ice) which is an RPC framework. I did some more detailed debugging and found that at every package level the __init__.py file has code that updates all the properties of all the module on that level via calls to sys.modules, wiping some of the data in the process! So the import triggers the hook but by the time xlOil gets hold of the module the contents are modified. So this is a bit of an edge case but the simple check I added fixes the issue.

One thing though. I typed the fix from memory but made a mistake. The change I made was as follows:

def watch_module(self, module: ModuleType):
    if not module.__spec__:
        return

    filepath = module.__spec__.origin
        
    if filepath == "frozen" or any((filepath.startswith(x) for x in self._ignore_paths)):
        return
        

It is the __spec__ attributes that is set to None as a result of the modules manipulation which triggers an error when trying to get the file path. Appreciate you considering adding this to the library. Really impressed by what xlOil can do.

@cunnane
Copy link
Owner

cunnane commented Oct 9, 2024

That's poor design on their part: the python docs are clear that spec should always be set except for the main model. I'll add the check anyway since it's no problem on the xloil side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants