summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog2
-rw-r--r--astroid/manager.py18
-rw-r--r--astroid/modutils.py5
3 files changed, 17 insertions, 8 deletions
diff --git a/ChangeLog b/ChangeLog
index 7960ea2e..45d7307d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -10,6 +10,8 @@ Release date: TBA
Closes #1780
+* Reduce file system access in ``ast_from_file()``.
+
* ``nodes.FunctionDef`` no longer inherits from ``nodes.Lambda``.
This is a breaking change but considered a bug fix as the nodes did not share the same
API and were not interchangeable.
diff --git a/astroid/manager.py b/astroid/manager.py
index 1f6ef482..7f62fd42 100644
--- a/astroid/manager.py
+++ b/astroid/manager.py
@@ -101,11 +101,6 @@ class AstroidManager:
source: bool = False,
) -> nodes.Module:
"""Given a module name, return the astroid object."""
- try:
- filepath = get_source_file(filepath, include_no_ext=True)
- source = True
- except NoSourceFile:
- pass
if modname is None:
try:
modname = ".".join(modpath_from_file(filepath))
@@ -116,6 +111,19 @@ class AstroidManager:
and self.astroid_cache[modname].file == filepath
):
return self.astroid_cache[modname]
+ # Call get_source_file() only after a cache miss,
+ # since it calls os.path.exists().
+ try:
+ filepath = get_source_file(filepath, include_no_ext=True)
+ source = True
+ except NoSourceFile:
+ pass
+ # Second attempt on the cache after get_source_file().
+ if (
+ modname in self.astroid_cache
+ and self.astroid_cache[modname].file == filepath
+ ):
+ return self.astroid_cache[modname]
if source:
# pylint: disable=import-outside-toplevel; circular import
from astroid.builder import AstroidBuilder
diff --git a/astroid/modutils.py b/astroid/modutils.py
index 266344a8..4e8d8f9f 100644
--- a/astroid/modutils.py
+++ b/astroid/modutils.py
@@ -488,9 +488,8 @@ def get_module_files(
def get_source_file(filename: str, include_no_ext: bool = False) -> str:
"""Given a python module's file name return the matching source file
- name (the filename will be returned identically if it's already an.
-
- absolute path to a python source file...)
+ name (the filename will be returned identically if it's already an
+ absolute path to a python source file).
:param filename: python module's file name