From 665574122cb6302fa5342c6f3570c674fcaf0a44 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Wed, 15 Dec 2021 06:16:27 -0800 Subject: Prefer using the module loader to get source in builder. (#1207) * Use loader.get_source() in builder.module_build() Otherwise we fail to get the module source code when running in an embedded hermetic interpreter environment where the source does not live on a filesystem. Co-authored-by: Pierre Sassoulas --- ChangeLog | 6 ++++++ astroid/builder.py | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index af8dad12..62ba3693 100644 --- a/ChangeLog +++ b/ChangeLog @@ -12,6 +12,12 @@ What's New in astroid 2.9.1? ============================ Release date: TBA +* Prefer the module loader get_source() method in AstroidBuilder's + module_build() when possible to avoid assumptions about source + code being available on a filesystem. Otherwise the source cannot + be found and application behavior changes when running within an + embedded hermetic interpreter environment (pyoxidizer, etc.). + * Require Python 3.6.2 to use astroid. * Fix ``deque.insert()`` signature in ``collections`` brain. diff --git a/astroid/builder.py b/astroid/builder.py index cfa8525e..b2c83262 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -87,7 +87,15 @@ class AstroidBuilder(raw_building.InspectBuilder): """Build an astroid from a living module instance.""" node = None path = getattr(module, "__file__", None) - if path is not None: + loader = getattr(module, "__loader__", None) + # Prefer the loader to get the source rather than assuming we have a + # filesystem to read the source file from ourselves. + if loader: + modname = modname or module.__name__ + source = loader.get_source(modname) + if source: + node = self.string_build(source, modname, path=path) + if node is None and path is not None: path_, ext = os.path.splitext(modutils._path_from_filename(path)) if ext in {".py", ".pyc", ".pyo"} and os.path.exists(path_ + ".py"): node = self.file_build(path_ + ".py", modname) -- cgit v1.2.1