ronaldoussoren / modulegraph2

Modulegraph2 is a library for creating and introspecting the dependency graph between Python modules.
MIT License
11 stars 8 forks source link

add_script raising AttributeError #11

Closed Legorooj closed 4 years ago

Legorooj commented 4 years ago

@ronaldoussoren first of all, thanks for writing this library. Secondly, I'm a core developer of PyInstaller. I'm in the process of migrating modulegraph2 into PyInstaller to replace our heavily modded copy of the original modulegraph. However, in the process, I came across a rather major error which I'm honestly surprised has occured. ModuleGraph.add_script doesn't work - it errors out:

(venv) D:\PyInstaller>ipython
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.17.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from PyInstaller.lib import modulegraph

In [2]: graph = modulegraph.ModuleGraph()

In [3]: graph.add_script('setup.py')
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-3-ec75154bffb4> in <module>
----> 1 graph.add_script('setup.py')

D:\PyInstaller\PyInstaller\lib\modulegraph\_modulegraph.py in add_script(self, script_path)
    167         node = self._load_script(script_path)
    168         self.add_root(node)
--> 169         self._run_stack()
    170         return node
    171

D:\PyInstaller\PyInstaller\lib\modulegraph\_modulegraph.py in _run_stack(self)
    362         while self._work_stack:
    363             func, args = self._work_stack.pop()
--> 364             func(*args)
    365
    366     def _implied_references(

D:\PyInstaller\PyInstaller\lib\modulegraph\_modulegraph.py in _process_namelist(self, importing_module, imported_module, import_info)
    814           import_info: Information about the import
    815         """
--> 816         assert isinstance(importing_module, (Module, Package))
    817         if import_info.star_import:
    818             if isinstance(imported_module, (Package, Module)):

AssertionError:

I traced a bit more detail on this issue: _process_namelist is called when running the workstack, and importing_module is an instance of Script, in this case referring to the setup.py script I added to the graph.

The exact folder structure I'm using can be found at https://github.com/Legorooj/pyinstaller/commit/d335bcebafcc. I'm not sure what else to add here, so please let me know if there's any info I can provide that I haven't already.

(I should note that this copy of the graph I'm running with this error has been modded slightly, though I can say for certain that the mod's I've made shouldn't affect add_script. None of the mods actually change how modulegraph works; they add features on top of it.)

Legorooj commented 4 years ago

Update: I ran this with vanilla modulegraph2, and after removing a type hint which imported PyInstaller(?) I managed to reproduce this using the exact command set in a fresh virtual environment. (Replacing PyInstaller.lib.modulegraph with modulegraph2 of course.)

ronaldoussoren commented 4 years ago

If pushed a fix for this particular problem to the repo.

W.r.t. the update: A slightly clear message would be appreciated, what type hint that imported PyInstaller?

BTW. Note that modulegraph2 is a complete rewrite, you'll likely encounter more problems. The semantics are also slightly different from the old version as it uses the actual importlib machinery to load modules/extensions instead of reproducing it in the graph builder.

Legorooj commented 4 years ago

@ronaldoussoren thanks! I can't find this type hint now though for some reason (maybe I added it myself idk). If I come across it again I'll let you know.