Skip to content

Commit

Permalink
Prefer using the module loader to get source in builder. (pylint-dev#…
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
2 people authored and tushar-deepsource committed Dec 20, 2021
1 parent 4b0bb1a commit a6b36f2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 9 additions & 1 deletion astroid/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,15 @@ def module_build(
"""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)
Expand Down

0 comments on commit a6b36f2

Please sign in to comment.