summaryrefslogtreecommitdiff
path: root/astroid
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2019-01-20 17:42:57 +0100
committerClaudiu Popa <pcmanticore@gmail.com>2019-01-20 17:50:27 +0100
commit6f3149a643c04c47b308688af4fe99ed129f0e61 (patch)
tree13fc4b6cd8b4e7ea184336f1905ead8dda9383ad /astroid
parentd7509faaae24ab6bb64435c8bcb3ad9652b949a8 (diff)
downloadastroid-git-6f3149a643c04c47b308688af4fe99ed129f0e61.tar.gz
Make the astroid bootstrapping take care of builtins such as Generator
Currently the astroid bootstrapping function, which builds the builtins module and relevant builtin objects, does not rebuild objects such as generators. This is causing a problem when the cache is cleared, e.g. from pylint's side, given that objects such as generator will not be present any longer in the builtins module created *after* the cache was cleared with MANAGER.cache_clear(). This behaviour was making that function useless from this point of view. The new change instead makes sure to build everything in the same function. Close #446
Diffstat (limited to 'astroid')
-rw-r--r--astroid/manager.py20
-rw-r--r--astroid/raw_building.py97
-rw-r--r--astroid/tests/unittest_manager.py4
-rw-r--r--astroid/tests/unittest_regrtest.py4
4 files changed, 57 insertions, 68 deletions
diff --git a/astroid/manager.py b/astroid/manager.py
index c7295b57..4388514a 100644
--- a/astroid/manager.py
+++ b/astroid/manager.py
@@ -311,13 +311,17 @@ class AstroidManager:
"""Cache a module if no module with the same name is known yet."""
self.astroid_cache.setdefault(module.name, module)
- def clear_cache(self, astroid_builtin=None):
- # XXX clear transforms
- self.astroid_cache.clear()
- # force bootstrap again, else we may ends up with cache inconsistency
- # between the manager and CONST_PROXY, making
- # unittest_lookup.LookupTC.test_builtin_lookup fail depending on the
- # test order
+ def bootstrap(self):
+ """Bootstrap the required AST modules needed for the manager to work
+
+ The bootstrap usually involves building the AST for the builtins
+ module, which is required by the rest of astroid to work correctly.
+ """
import astroid.raw_building
- astroid.raw_building._astroid_bootstrapping(astroid_builtin=astroid_builtin)
+ astroid.raw_building._astroid_bootstrapping()
+
+ def clear_cache(self):
+ """Clear the underlying cache. Also bootstraps the builtins module."""
+ self.astroid_cache.clear()
+ self.bootstrap()
diff --git a/astroid/raw_building.py b/astroid/raw_building.py
index 1cd6c81f..7707ea19 100644
--- a/astroid/raw_building.py
+++ b/astroid/raw_building.py
@@ -261,8 +261,6 @@ class InspectBuilder:
FunctionDef and ClassDef nodes and some others as guessed.
"""
- # astroid from living objects ###############################################
-
def __init__(self):
self._done = {}
self._module = None
@@ -380,17 +378,20 @@ class InspectBuilder:
### astroid bootstrapping ######################################################
-Astroid_BUILDER = InspectBuilder()
_CONST_PROXY = {}
+# TODO : find a nicer way to handle this situation;
+def _set_proxied(const):
+ return _CONST_PROXY[const.value.__class__]
+
-def _astroid_bootstrapping(astroid_builtin=None):
- """astroid boot strapping the builtins module"""
+def _astroid_bootstrapping():
+ """astroid bootstrapping the builtins module"""
# this boot strapping is necessary since we need the Const nodes to
# inspect_build builtins, and then we can proxy Const
- if astroid_builtin is None:
- astroid_builtin = Astroid_BUILDER.inspect_build(builtins)
+ builder = InspectBuilder()
+ astroid_builtin = builder.inspect_build(builtins)
# pylint: disable=redefined-outer-name
for cls, node_cls in node_classes.CONST_CLS.items():
@@ -407,52 +408,42 @@ def _astroid_bootstrapping(astroid_builtin=None):
else:
_CONST_PROXY[cls] = proxy
+ # Set the builtin module as parent for some builtins.
+ nodes.Const._proxied = property(_set_proxied)
-_astroid_bootstrapping()
-
-# TODO : find a nicer way to handle this situation;
-def _set_proxied(const):
- return _CONST_PROXY[const.value.__class__]
-
-
-nodes.Const._proxied = property(_set_proxied)
-
-_GeneratorType = nodes.ClassDef(
- types.GeneratorType.__name__, types.GeneratorType.__doc__
-)
-_GeneratorType.parent = MANAGER.astroid_cache[builtins.__name__]
-bases.Generator._proxied = _GeneratorType
-Astroid_BUILDER.object_build(bases.Generator._proxied, types.GeneratorType)
-
-if hasattr(types, "AsyncGeneratorType"):
- # pylint: disable=no-member; AsyncGeneratorType
- _AsyncGeneratorType = nodes.ClassDef(
- types.AsyncGeneratorType.__name__, types.AsyncGeneratorType.__doc__
+ _GeneratorType = nodes.ClassDef(
+ types.GeneratorType.__name__, types.GeneratorType.__doc__
)
- _AsyncGeneratorType.parent = MANAGER.astroid_cache[builtins.__name__]
- bases.AsyncGenerator._proxied = _AsyncGeneratorType
- Astroid_BUILDER.object_build(
- bases.AsyncGenerator._proxied, types.AsyncGeneratorType
+ _GeneratorType.parent = astroid_builtin
+ bases.Generator._proxied = _GeneratorType
+ builder.object_build(bases.Generator._proxied, types.GeneratorType)
+
+ if hasattr(types, "AsyncGeneratorType"):
+ # pylint: disable=no-member; AsyncGeneratorType
+ _AsyncGeneratorType = nodes.ClassDef(
+ types.AsyncGeneratorType.__name__, types.AsyncGeneratorType.__doc__
+ )
+ _AsyncGeneratorType.parent = astroid_builtin
+ bases.AsyncGenerator._proxied = _AsyncGeneratorType
+ builder.object_build(bases.AsyncGenerator._proxied, types.AsyncGeneratorType)
+ builtin_types = (
+ types.GetSetDescriptorType,
+ types.GeneratorType,
+ types.MemberDescriptorType,
+ type(None),
+ type(NotImplemented),
+ types.FunctionType,
+ types.MethodType,
+ types.BuiltinFunctionType,
+ types.ModuleType,
+ types.TracebackType,
)
-# pylint: enable=no-member
-
-
-_builtins = MANAGER.astroid_cache[builtins.__name__]
-BUILTIN_TYPES = (
- types.GetSetDescriptorType,
- types.GeneratorType,
- types.MemberDescriptorType,
- type(None),
- type(NotImplemented),
- types.FunctionType,
- types.MethodType,
- types.BuiltinFunctionType,
- types.ModuleType,
- types.TracebackType,
-)
-for _type in BUILTIN_TYPES:
- if _type.__name__ not in _builtins:
- cls = nodes.ClassDef(_type.__name__, _type.__doc__)
- cls.parent = MANAGER.astroid_cache[builtins.__name__]
- Astroid_BUILDER.object_build(cls, _type)
- _builtins[_type.__name__] = cls
+ for _type in builtin_types:
+ if _type.__name__ not in astroid_builtin:
+ cls = nodes.ClassDef(_type.__name__, _type.__doc__)
+ cls.parent = astroid_builtin
+ builder.object_build(cls, _type)
+ astroid_builtin[_type.__name__] = cls
+
+
+_astroid_bootstrapping()
diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py
index 505bdf56..5629b4ff 100644
--- a/astroid/tests/unittest_manager.py
+++ b/astroid/tests/unittest_manager.py
@@ -19,6 +19,7 @@ import unittest
import pkg_resources
import six
+import time
import astroid
from astroid import exceptions
@@ -45,7 +46,6 @@ class AstroidManagerTest(
def setUp(self):
super(AstroidManagerTest, self).setUp()
self.manager = manager.AstroidManager()
- self.manager.clear_cache(self._builtins) # take care of borg
def test_ast_from_file(self):
filepath = unittest.__file__
@@ -235,8 +235,6 @@ class AstroidManagerTest(
def test_ast_from_module(self):
ast = self.manager.ast_from_module(unittest)
self.assertEqual(ast.pure_python, True)
- import time
-
ast = self.manager.ast_from_module(time)
self.assertEqual(ast.pure_python, False)
diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py
index b83fa69f..50e9aa6c 100644
--- a/astroid/tests/unittest_regrtest.py
+++ b/astroid/tests/unittest_regrtest.py
@@ -33,10 +33,6 @@ class NonRegressionTests(resources.AstroidCacheSetupMixin, unittest.TestCase):
MANAGER.astroid_cache[BUILTINS] = self._builtins
def tearDown(self):
- # Since we may have created a brainless manager, leading
- # to a new cache builtin module and proxy classes in the constants,
- # clear out the global manager cache.
- MANAGER.clear_cache(self._builtins)
MANAGER.always_load_extensions = False
sys.path.pop(0)
sys.path_importer_cache.pop(resources.find("data"), None)