diff options
| author | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-01-15 15:27:20 +0200 |
|---|---|---|
| committer | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-01-15 15:27:20 +0200 |
| commit | 73721b28765c81764b5aec7bc9dc0b4d43db79d3 (patch) | |
| tree | 726668f2e77b7204e0e7aa1e9d8bbc1d9837acc2 | |
| parent | b94b983211b7bd019a70e2e1ab19b9c5fffe141d (diff) | |
| download | astroid-git-73721b28765c81764b5aec7bc9dc0b4d43db79d3.tar.gz | |
Fix the test's astroid cache problem.
There were two problems until now:
* first, the transformed builtin object was deleting during clear_cache,
leaving the next tests to use the untransformed tests, which yielded
InferenceErrors.
* after the first problem was fixed, another problem with CONST_PROXY
occurred, since it was creating a new builtins object, without
the builtins transformations applied, which was used for creating
the const classes. The problem was that even replacing the
builtins instance in astroid_cache, InferenceErrors occurred,
because the const classes were already built and they were referencing
the untransformed builtins. That's why _astroid_bootstrapping
allows to receive a new builtins object, which is passed
properly by the tests.
| -rw-r--r-- | astroid/manager.py | 7 | ||||
| -rw-r--r-- | astroid/raw_building.py | 10 | ||||
| -rw-r--r-- | astroid/tests/resources.py | 24 | ||||
| -rw-r--r-- | astroid/tests/unittest_manager.py | 7 | ||||
| -rw-r--r-- | astroid/tests/unittest_regrtest.py | 20 |
5 files changed, 42 insertions, 26 deletions
diff --git a/astroid/manager.py b/astroid/manager.py index f5a2404c..322cff93 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -348,15 +348,16 @@ class AstroidManager(OptionsProviderMixIn): """Cache a module if no module with the same name is known yet.""" self.astroid_cache.setdefault(module.name, module) - def clear_cache(self): + 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 - from astroid.raw_building import astroid_bootstrapping - astroid_bootstrapping() + import astroid.raw_building + astroid.raw_building._astroid_bootstrapping( + astroid_builtin=astroid_builtin) class Project(object): diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 08f6af6d..d5e8b3d3 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -332,12 +332,14 @@ class InspectBuilder(object): Astroid_BUILDER = InspectBuilder() _CONST_PROXY = {} -def astroid_bootstrapping(): +def _astroid_bootstrapping(astroid_builtin=None): """astroid boot strapping the builtins module""" # this boot strapping is necessary since we need the Const nodes to # inspect_build builtins, and then we can proxy Const - from logilab.common.compat import builtins - astroid_builtin = Astroid_BUILDER.inspect_build(builtins) + if astroid_builtin is None: + from logilab.common.compat import builtins + astroid_builtin = Astroid_BUILDER.inspect_build(builtins) + for cls, node_cls in CONST_CLS.items(): if cls is type(None): proxy = build_class('NoneType') @@ -349,7 +351,7 @@ def astroid_bootstrapping(): else: _CONST_PROXY[cls] = proxy -astroid_bootstrapping() +_astroid_bootstrapping() # TODO : find a nicer way to handle this situation; # However __proxied introduced an diff --git a/astroid/tests/resources.py b/astroid/tests/resources.py index bce65472..03d45623 100644 --- a/astroid/tests/resources.py +++ b/astroid/tests/resources.py @@ -21,6 +21,8 @@ import sys import pkg_resources from astroid import builder +from astroid import MANAGER +from astroid.bases import BUILTINS DATA_DIR = 'testdata/python{}/'.format(sys.version_info[0]) @@ -46,3 +48,25 @@ class SysPathSetup(object): del sys.path_importer_cache[key] +class AstroidCacheSetupMixin(object): + """Mixin for handling the astroid cache problems. + + When clearing the astroid cache, some tests fails due to + cache inconsistencies, where some objects had a different + builtins object referenced. + This saves the builtins module and makes sure to add it + back to the astroid_cache after the tests finishes. + The builtins module is special, since some of the + transforms for a couple of its objects (str, bytes etc) + are executed only once, so astroid_bootstrapping will be + useless for retrieving the original builtins module. + """ + + @classmethod + def setUpClass(cls): + cls._builtins = MANAGER.astroid_cache.get(BUILTINS) + + @classmethod + def tearDownClass(cls): + if cls._builtins: + MANAGER.astroid_cache[BUILTINS] = cls._builtins diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index 04a76db1..62c09dd7 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -25,11 +25,14 @@ from astroid.exceptions import AstroidBuildingException from astroid.tests import resources -class AstroidManagerTest(resources.SysPathSetup, unittest.TestCase): +class AstroidManagerTest(resources.SysPathSetup, + resources.AstroidCacheSetupMixin, + unittest.TestCase): + def setUp(self): super(AstroidManagerTest, self).setUp() self.manager = AstroidManager() - self.manager.clear_cache() # take care of borg + self.manager.clear_cache(self._builtins) # take care of borg def test_ast_from_file(self): """check if the method return a good astroid object""" diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index 393f9ad2..06ec6105 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -27,22 +27,8 @@ from astroid.manager import AstroidManager from astroid.test_utils import require_version from astroid.tests import resources -class NonRegressionTests(unittest.TestCase): - - # Save the builtins module and make sure to add it - # back to the astroid_cache after the tests finishes. - # The builtins module is special, since some of the - # transforms for a couple of its objects (str, bytes etc) - # are executed only once, so astroid_bootstrapping will be - # useless for retrieving the original builtins module. - - @classmethod - def setUpClass(cls): - cls._builtins = MANAGER.astroid_cache[BUILTINS] - - @classmethod - def tearDownClass(cls): - MANAGER.astroid_cache[BUILTINS] = cls._builtins +class NonRegressionTests(resources.AstroidCacheSetupMixin, + unittest.TestCase): def setUp(self): sys.path.insert(0, resources.find('data')) @@ -52,7 +38,7 @@ class NonRegressionTests(unittest.TestCase): # 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() + MANAGER.clear_cache(self._builtins) MANAGER.always_load_extensions = False sys.path.pop(0) sys.path_importer_cache.pop(resources.find('data'), None) |
