Skip to content

Commit

Permalink
Don't cache exception values in the module cache
Browse files Browse the repository at this point in the history
Exceptions hold tracebacks, which can get very large (especially for
highly recursive functions). Especially if the exceptions are being used
for control flow instead of actual exception management, we should be
throwing them away as fast as possible.

This commit deals with this with a bit of a hacky solution, but really
the way the module cache handles things ought to be reworked a bit.
  • Loading branch information
rtpg committed Aug 30, 2020
1 parent 25384d4 commit 1d9fdff
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions astroid/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ def safe_repr(obj):
return "???"


class AstroidImportError:
"""
Error class to keep track of when exceptions happened.
This error class avoids holding onto the initial exception to avoid
memory usage explosion
"""

def __init__(self, modname, error):
self.modname = modname
self.error = str(error)

def __str__(self):
return "AstroidImportError: Failed to import module {modname} with error:\n{error}.".format(
modname=self.modname, error=self.error
)


class AstroidManager:
"""the astroid manager, responsible to build astroid from files
or modules.
Expand Down Expand Up @@ -233,14 +251,12 @@ def file_from_module_name(self, modname, contextfile):
modname.split("."), context_file=contextfile
)
except ImportError as ex:
value = exceptions.AstroidImportError(
"Failed to import module {modname} with error:\n{error}.",
modname=modname,
error=ex,
)
value = AstroidImportError(modname=modname, error=ex)
self._mod_file_cache[(modname, contextfile)] = value
if isinstance(value, exceptions.AstroidBuildingError):
raise value
if isinstance(value, AstroidImportError):
raise exceptions.AstroidImportError(
str(value), modname=value.modname, error=value.error
)
return value

def ast_from_module(self, module, modname=None):
Expand Down

0 comments on commit 1d9fdff

Please sign in to comment.