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 / objects.py @ 745
History | View | Annotate | Download (6.25 KB)
1 |
# copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), 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 |
|
19 |
"""
|
20 |
Inference objects are a way to represent composite AST nodes,
|
21 |
which are used only as inference results, so they can't be found in the
|
22 |
code tree. For instance, inferring the following frozenset use, leads to an
|
23 |
inferred FrozenSet:
|
24 |
|
25 |
CallFunc(func=Name('frozenset'), args=Tuple(...))
|
26 |
|
27 |
"""
|
28 |
|
29 |
import six |
30 |
|
31 |
from astroid import MANAGER |
32 |
from astroid.bases import ( |
33 |
BUILTINS, NodeNG, Instance, _infer_stmts, |
34 |
BoundMethod, _is_property |
35 |
) |
36 |
from astroid.decorators import cachedproperty |
37 |
from astroid.exceptions import ( |
38 |
SuperError, SuperArgumentTypeError, |
39 |
NotFoundError, MroError |
40 |
) |
41 |
from astroid.node_classes import const_factory |
42 |
from astroid.scoped_nodes import ClassDef, FunctionDef |
43 |
from astroid.mixins import ParentAssignTypeMixin |
44 |
|
45 |
|
46 |
class FrozenSet(NodeNG, Instance, ParentAssignTypeMixin): |
47 |
"""class representing a FrozenSet composite node"""
|
48 |
|
49 |
def __init__(self, elts=None): |
50 |
if elts is None: |
51 |
self.elts = []
|
52 |
else:
|
53 |
self.elts = [const_factory(e) for e in elts] |
54 |
|
55 |
def pytype(self): |
56 |
return '%s.frozenset' % BUILTINS |
57 |
|
58 |
def itered(self): |
59 |
return self.elts |
60 |
|
61 |
def _infer(self, context=None): |
62 |
yield self |
63 |
|
64 |
@cachedproperty
|
65 |
def _proxied(self): |
66 |
builtins = MANAGER.astroid_cache[BUILTINS] |
67 |
return builtins.getattr('frozenset')[0] |
68 |
|
69 |
|
70 |
class Super(NodeNG): |
71 |
"""Proxy class over a super call.
|
72 |
|
73 |
This class offers almost the same behaviour as Python's super,
|
74 |
which is MRO lookups for retrieving attributes from the parents.
|
75 |
|
76 |
The *mro_pointer* is the place in the MRO from where we should
|
77 |
start looking, not counting it. *mro_type* is the object which
|
78 |
provides the MRO, it can be both a type or an instance.
|
79 |
*self_class* is the class where the super call is, while
|
80 |
*scope* is the function where the super call is.
|
81 |
"""
|
82 |
|
83 |
def __init__(self, mro_pointer, mro_type, self_class, scope): |
84 |
self.type = mro_type
|
85 |
self.mro_pointer = mro_pointer
|
86 |
self._class_based = False |
87 |
self._self_class = self_class
|
88 |
self._scope = scope
|
89 |
self._model = {
|
90 |
'__thisclass__': self.mro_pointer, |
91 |
'__self_class__': self._self_class, |
92 |
'__self__': self.type, |
93 |
'__class__': self._proxied, |
94 |
} |
95 |
|
96 |
def _infer(self, context=None): |
97 |
yield self |
98 |
|
99 |
def super_mro(self): |
100 |
"""Get the MRO which will be used to lookup attributes in this super."""
|
101 |
if not isinstance(self.mro_pointer, ClassDef): |
102 |
raise SuperArgumentTypeError("The first super argument must be type.") |
103 |
|
104 |
if isinstance(self.type, ClassDef): |
105 |
# `super(type, type)`, most likely in a class method.
|
106 |
self._class_based = True |
107 |
mro_type = self.type
|
108 |
else:
|
109 |
mro_type = getattr(self.type, '_proxied', None) |
110 |
if not isinstance(mro_type, (Instance, ClassDef)): |
111 |
raise SuperArgumentTypeError("super(type, obj): obj must be an " |
112 |
"instance or subtype of type")
|
113 |
|
114 |
if not mro_type.newstyle: |
115 |
raise SuperError("Unable to call super on old-style classes.") |
116 |
|
117 |
mro = mro_type.mro() |
118 |
if self.mro_pointer not in mro: |
119 |
raise SuperArgumentTypeError("super(type, obj): obj must be an " |
120 |
"instance or subtype of type")
|
121 |
|
122 |
index = mro.index(self.mro_pointer)
|
123 |
return mro[index + 1:] |
124 |
|
125 |
@cachedproperty
|
126 |
def _proxied(self): |
127 |
builtins = MANAGER.astroid_cache[BUILTINS] |
128 |
return builtins.getattr('super')[0] |
129 |
|
130 |
def pytype(self): |
131 |
return '%s.super' % BUILTINS |
132 |
|
133 |
def display_type(self): |
134 |
return 'Super of' |
135 |
|
136 |
@property
|
137 |
def name(self): |
138 |
"""Get the name of the MRO pointer."""
|
139 |
return self.mro_pointer.name |
140 |
|
141 |
def igetattr(self, name, context=None): |
142 |
"""Retrieve the inferred values of the given attribute name."""
|
143 |
|
144 |
local_name = self._model.get(name)
|
145 |
if local_name:
|
146 |
yield local_name
|
147 |
return
|
148 |
|
149 |
try:
|
150 |
mro = self.super_mro()
|
151 |
except (MroError, SuperError) as exc: |
152 |
# Don't let invalid MROs or invalid super calls
|
153 |
# to leak out as is from this function.
|
154 |
six.raise_from(NotFoundError, exc) |
155 |
|
156 |
found = False
|
157 |
for cls in mro: |
158 |
if name not in cls._locals: |
159 |
continue
|
160 |
|
161 |
found = True
|
162 |
for infered in _infer_stmts([cls[name]], context, frame=self): |
163 |
if not isinstance(infered, FunctionDef): |
164 |
yield infered
|
165 |
continue
|
166 |
|
167 |
# We can obtain different descriptors from a super depending
|
168 |
# on what we are accessing and where the super call is.
|
169 |
if infered.type == 'classmethod': |
170 |
yield BoundMethod(infered, cls)
|
171 |
elif self._scope.type == 'classmethod' and infered.type == 'method': |
172 |
yield infered
|
173 |
elif self._class_based or infered.type == 'staticmethod': |
174 |
yield infered
|
175 |
elif _is_property(infered):
|
176 |
# TODO: support other descriptors as well.
|
177 |
for value in infered.infer_call_result(self, context): |
178 |
yield value
|
179 |
else:
|
180 |
yield BoundMethod(infered, cls)
|
181 |
|
182 |
if not found: |
183 |
raise NotFoundError(name)
|
184 |
|
185 |
def getattr(self, name, context=None): |
186 |
return list(self.igetattr(name, context=context)) |