gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / astroid / tests / resources.py @ 745
History | View | Annotate | Download (2.32 KB)
1 |
# Copyright 2014 Google, Inc. All rights reserved.
|
---|---|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
|
3 |
#
|
4 |
# This file is part of astroid.
|
5 |
#
|
6 |
# astroid is free software: you can redistribute it and/or modify it
|
7 |
# under the terms of the GNU Lesser General Public License as published by the
|
8 |
# Free Software Foundation, either version 2.1 of the License, or (at your
|
9 |
# option) any later version.
|
10 |
#
|
11 |
# astroid is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
13 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
14 |
# for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU Lesser General Public License along
|
17 |
# with astroid. If not, see <http://www.gnu.org/licenses/>.
|
18 |
import os |
19 |
import sys |
20 |
|
21 |
import pkg_resources |
22 |
|
23 |
from astroid import builder |
24 |
from astroid import MANAGER |
25 |
from astroid.bases import BUILTINS |
26 |
|
27 |
|
28 |
DATA_DIR = 'testdata/python{}/'.format(sys.version_info[0]) |
29 |
|
30 |
def find(name): |
31 |
return pkg_resources.resource_filename(
|
32 |
'astroid.tests', os.path.normpath(os.path.join(DATA_DIR, name)))
|
33 |
|
34 |
|
35 |
def build_file(path, modname=None): |
36 |
return builder.AstroidBuilder().file_build(find(path), modname)
|
37 |
|
38 |
|
39 |
class SysPathSetup(object): |
40 |
def setUp(self): |
41 |
sys.path.insert(0, find('')) |
42 |
|
43 |
def tearDown(self): |
44 |
del sys.path[0] |
45 |
datadir = find('')
|
46 |
for key in list(sys.path_importer_cache): |
47 |
if key.startswith(datadir):
|
48 |
del sys.path_importer_cache[key]
|
49 |
|
50 |
|
51 |
class AstroidCacheSetupMixin(object): |
52 |
"""Mixin for handling the astroid cache problems.
|
53 |
|
54 |
When clearing the astroid cache, some tests fails due to
|
55 |
cache inconsistencies, where some objects had a different
|
56 |
builtins object referenced.
|
57 |
This saves the builtins module and makes sure to add it
|
58 |
back to the astroid_cache after the tests finishes.
|
59 |
The builtins module is special, since some of the
|
60 |
transforms for a couple of its objects (str, bytes etc)
|
61 |
are executed only once, so astroid_bootstrapping will be
|
62 |
useless for retrieving the original builtins module.
|
63 |
"""
|
64 |
|
65 |
@classmethod
|
66 |
def setUpClass(cls): |
67 |
cls._builtins = MANAGER.astroid_cache.get(BUILTINS) |
68 |
|
69 |
@classmethod
|
70 |
def tearDownClass(cls): |
71 |
if cls._builtins:
|
72 |
MANAGER.astroid_cache[BUILTINS] = cls._builtins |