/usr/share/pyshared/rope/base/pyobjects.py is in python-rope 0.9.2-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | from rope.base import ast, exceptions, utils
class PyObject(object):
def __init__(self, type_):
if type_ is None:
type_ = self
self.type = type_
def get_attributes(self):
if self.type is self:
return {}
return self.type.get_attributes()
def get_attribute(self, name):
if name not in self.get_attributes():
raise exceptions.AttributeNotFoundError(
'Attribute %s not found' % name)
return self.get_attributes()[name]
def get_type(self):
return self.type
def __getitem__(self, key):
"""The same as ``get_attribute(key)``"""
return self.get_attribute(key)
def __contains__(self, key):
"""The same as ``key in self.get_attributes()``"""
return key in self.get_attributes()
def __eq__(self, obj):
"""Check the equality of two `PyObject`\s
Currently it is assumed that instances (the direct instances
of `PyObject`, not the instances of its subclasses) are equal
if their types are equal. For every other object like
defineds or builtins rope assumes objects are reference
objects and their identities should match.
"""
if self.__class__ != obj.__class__:
return False
if type(self) == PyObject:
if self is not self.type:
return self.type == obj.type
else:
return self.type is obj.type
return self is obj
def __ne__(self, obj):
return not self.__eq__(obj)
def __hash__(self):
"""See docs for `__eq__()` method"""
if type(self) == PyObject and self != self.type:
return hash(self.type) + 1
else:
return super(PyObject, self).__hash__()
def __iter__(self):
"""The same as ``iter(self.get_attributes())``"""
return iter(self.get_attributes())
_types = None
_unknown = None
@staticmethod
def _get_base_type(name):
if PyObject._types is None:
PyObject._types = {}
base_type = PyObject(None)
PyObject._types['Type'] = base_type
PyObject._types['Module'] = PyObject(base_type)
PyObject._types['Function'] = PyObject(base_type)
PyObject._types['Unknown'] = PyObject(base_type)
return PyObject._types[name]
def get_base_type(name):
"""Return the base type with name `name`.
The base types are 'Type', 'Function', 'Module' and 'Unknown'. It
was used to check the type of a `PyObject` but currently its use
is discouraged. Use classes defined in this module instead.
For example instead of
``pyobject.get_type() == get_base_type('Function')`` use
``isinstance(pyobject, AbstractFunction)``.
You can use `AbstractClass` for classes, `AbstractFunction` for
functions, and `AbstractModule` for modules. You can also use
`PyFunction` and `PyClass` for testing if an object is
defined somewhere and rope can access its source. These classes
provide more methods.
"""
return PyObject._get_base_type(name)
def get_unknown():
"""Return a pyobject whose type is unknown
Note that two unknown objects are equal. So for example you can
write::
if pyname.get_object() == get_unknown():
print 'cannot determine what this pyname holds'
Rope could have used `None` for indicating unknown objects but
we had to check that in many places. So actually this method
returns a null object.
"""
if PyObject._unknown is None:
PyObject._unknown = PyObject(get_base_type('Unknown'))
return PyObject._unknown
class AbstractClass(PyObject):
def __init__(self):
super(AbstractClass, self).__init__(get_base_type('Type'))
def get_name(self):
pass
def get_doc(self):
pass
def get_superclasses(self):
return []
class AbstractFunction(PyObject):
def __init__(self):
super(AbstractFunction, self).__init__(get_base_type('Function'))
def get_name(self):
pass
def get_doc(self):
pass
def get_param_names(self, special_args=True):
return []
def get_returned_object(self, args):
return get_unknown()
class AbstractModule(PyObject):
def __init__(self, doc=None):
super(AbstractModule, self).__init__(get_base_type('Module'))
def get_doc(self):
pass
def get_resource(self):
pass
class PyDefinedObject(object):
"""Python defined names that rope can access their sources"""
def __init__(self, pycore, ast_node, parent):
self.pycore = pycore
self.ast_node = ast_node
self.scope = None
self.parent = parent
self.structural_attributes = None
self.concluded_attributes = self.get_module()._get_concluded_data()
self.attributes = self.get_module()._get_concluded_data()
self.defineds = None
visitor_class = None
@utils.prevent_recursion(lambda: {})
def _get_structural_attributes(self):
if self.structural_attributes is None:
self.structural_attributes = self._create_structural_attributes()
return self.structural_attributes
@utils.prevent_recursion(lambda: {})
def _get_concluded_attributes(self):
if self.concluded_attributes.get() is None:
self._get_structural_attributes()
self.concluded_attributes.set(self._create_concluded_attributes())
return self.concluded_attributes.get()
def get_attributes(self):
if self.attributes.get() is None:
result = dict(self._get_concluded_attributes())
result.update(self._get_structural_attributes())
self.attributes.set(result)
return self.attributes.get()
def get_attribute(self, name):
if name in self._get_structural_attributes():
return self._get_structural_attributes()[name]
if name in self._get_concluded_attributes():
return self._get_concluded_attributes()[name]
raise exceptions.AttributeNotFoundError('Attribute %s not found' %
name)
def get_scope(self):
if self.scope is None:
self.scope = self._create_scope()
return self.scope
def get_module(self):
current_object = self
while current_object.parent is not None:
current_object = current_object.parent
return current_object
def get_doc(self):
if len(self.get_ast().body) > 0:
expr = self.get_ast().body[0]
if isinstance(expr, ast.Expr) and \
isinstance(expr.value, ast.Str):
return expr.value.s
def _get_defined_objects(self):
if self.defineds is None:
self._get_structural_attributes()
return self.defineds
def _create_structural_attributes(self):
if self.visitor_class is None:
return {}
new_visitor = self.visitor_class(self.pycore, self)
for child in ast.get_child_nodes(self.ast_node):
ast.walk(child, new_visitor)
self.defineds = new_visitor.defineds
return new_visitor.names
def _create_concluded_attributes(self):
return {}
def get_ast(self):
return self.ast_node
def _create_scope(self):
pass
class PyFunction(PyDefinedObject, AbstractFunction):
"""Only a placeholder"""
class PyClass(PyDefinedObject, AbstractClass):
"""Only a placeholder"""
class _ConcludedData(object):
def __init__(self):
self.data_ = None
def set(self, data):
self.data_ = data
def get(self):
return self.data_
data = property(get, set)
def _invalidate(self):
self.data = None
def __str__(self):
return '<' + str(self.data) + '>'
class _PyModule(PyDefinedObject, AbstractModule):
def __init__(self, pycore, ast_node, resource):
self.resource = resource
self.concluded_data = []
AbstractModule.__init__(self)
PyDefinedObject.__init__(self, pycore, ast_node, None)
def _get_concluded_data(self):
new_data = _ConcludedData()
self.concluded_data.append(new_data)
return new_data
def _forget_concluded_data(self):
for data in self.concluded_data:
data._invalidate()
def get_resource(self):
return self.resource
class PyModule(_PyModule):
"""Only a placeholder"""
class PyPackage(_PyModule):
"""Only a placeholder"""
class IsBeingInferredError(exceptions.RopeError):
pass
|