scoped_nodes.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
  2. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
  3. # copyright 2003-2010 Sylvain Thenault, all rights reserved.
  4. # contact mailto:thenault@gmail.com
  5. #
  6. # This file is part of logilab-astng.
  7. #
  8. # logilab-astng is free software: you can redistribute it and/or modify it
  9. # under the terms of the GNU Lesser General Public License as published by the
  10. # Free Software Foundation, either version 2.1 of the License, or (at your
  11. # option) any later version.
  12. #
  13. # logilab-astng is distributed in the hope that it will be useful, but
  14. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  16. # for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with logilab-astng. If not, see <http://www.gnu.org/licenses/>.
  20. """This module contains the classes for "scoped" node, i.e. which are opening a
  21. new local scope in the language definition : Module, Class, Function (and
  22. Lambda, GenExpr, DictComp and SetComp to some extent).
  23. """
  24. from __future__ import with_statement
  25. __doctype__ = "restructuredtext en"
  26. import sys
  27. from itertools import chain
  28. from logilab.common.compat import builtins
  29. from logilab.common.decorators import cached
  30. from logilab.astng import BUILTINS_MODULE
  31. from logilab.astng.exceptions import NotFoundError, NoDefault, \
  32. ASTNGBuildingException, InferenceError
  33. from logilab.astng.node_classes import Const, DelName, DelAttr, \
  34. Dict, From, List, Name, Pass, Raise, Return, Tuple, Yield, \
  35. are_exclusive, LookupMixIn, const_factory as cf, unpack_infer
  36. from logilab.astng.bases import NodeNG, InferenceContext, Instance,\
  37. YES, Generator, UnboundMethod, BoundMethod, _infer_stmts, copy_context, \
  38. BUILTINS_NAME
  39. from logilab.astng.mixins import FilterStmtsMixin
  40. from logilab.astng.bases import Statement
  41. from logilab.astng.manager import ASTNGManager
  42. def remove_nodes(func, cls):
  43. def wrapper(*args, **kwargs):
  44. nodes = [n for n in func(*args, **kwargs) if not isinstance(n, cls)]
  45. if not nodes:
  46. raise NotFoundError()
  47. return nodes
  48. return wrapper
  49. def function_to_method(n, klass):
  50. if isinstance(n, Function):
  51. if n.type == 'classmethod':
  52. return BoundMethod(n, klass)
  53. if n.type != 'staticmethod':
  54. return UnboundMethod(n)
  55. return n
  56. def std_special_attributes(self, name, add_locals=True):
  57. if add_locals:
  58. locals = self.locals
  59. else:
  60. locals = {}
  61. if name == '__name__':
  62. return [cf(self.name)] + locals.get(name, [])
  63. if name == '__doc__':
  64. return [cf(self.doc)] + locals.get(name, [])
  65. if name == '__dict__':
  66. return [Dict()] + locals.get(name, [])
  67. raise NotFoundError(name)
  68. MANAGER = ASTNGManager()
  69. def builtin_lookup(name):
  70. """lookup a name into the builtin module
  71. return the list of matching statements and the astng for the builtin
  72. module
  73. """
  74. builtin_astng = MANAGER.astng_from_module(builtins)
  75. if name == '__dict__':
  76. return builtin_astng, ()
  77. try:
  78. stmts = builtin_astng.locals[name]
  79. except KeyError:
  80. stmts = ()
  81. return builtin_astng, stmts
  82. # TODO move this Mixin to mixins.py; problem: 'Function' in _scope_lookup
  83. class LocalsDictNodeNG(LookupMixIn, NodeNG):
  84. """ this class provides locals handling common to Module, Function
  85. and Class nodes, including a dict like interface for direct access
  86. to locals information
  87. """
  88. # attributes below are set by the builder module or by raw factories
  89. # dictionary of locals with name as key and node defining the local as
  90. # value
  91. def qname(self):
  92. """return the 'qualified' name of the node, eg module.name,
  93. module.class.name ...
  94. """
  95. if self.parent is None:
  96. return self.name
  97. return '%s.%s' % (self.parent.frame().qname(), self.name)
  98. def frame(self):
  99. """return the first parent frame node (i.e. Module, Function or Class)
  100. """
  101. return self
  102. def scope(self):
  103. """return the first node defining a new scope (i.e. Module,
  104. Function, Class, Lambda but also GenExpr, DictComp and SetComp)
  105. """
  106. return self
  107. def _scope_lookup(self, node, name, offset=0):
  108. """XXX method for interfacing the scope lookup"""
  109. try:
  110. stmts = node._filter_stmts(self.locals[name], self, offset)
  111. except KeyError:
  112. stmts = ()
  113. if stmts:
  114. return self, stmts
  115. if self.parent: # i.e. not Module
  116. # nested scope: if parent scope is a function, that's fine
  117. # else jump to the module
  118. pscope = self.parent.scope()
  119. if not pscope.is_function:
  120. pscope = pscope.root()
  121. return pscope.scope_lookup(node, name)
  122. return builtin_lookup(name) # Module
  123. def set_local(self, name, stmt):
  124. """define <name> in locals (<stmt> is the node defining the name)
  125. if the node is a Module node (i.e. has globals), add the name to
  126. globals
  127. if the name is already defined, ignore it
  128. """
  129. #assert not stmt in self.locals.get(name, ()), (self, stmt)
  130. self.locals.setdefault(name, []).append(stmt)
  131. __setitem__ = set_local
  132. def _append_node(self, child):
  133. """append a child, linking it in the tree"""
  134. self.body.append(child)
  135. child.parent = self
  136. def add_local_node(self, child_node, name=None):
  137. """append a child which should alter locals to the given node"""
  138. if name != '__class__':
  139. # add __class__ node as a child will cause infinite recursion later!
  140. self._append_node(child_node)
  141. self.set_local(name or child_node.name, child_node)
  142. def __getitem__(self, item):
  143. """method from the `dict` interface returning the first node
  144. associated with the given name in the locals dictionary
  145. :type item: str
  146. :param item: the name of the locally defined object
  147. :raises KeyError: if the name is not defined
  148. """
  149. return self.locals[item][0]
  150. def __iter__(self):
  151. """method from the `dict` interface returning an iterator on
  152. `self.keys()`
  153. """
  154. return iter(self.keys())
  155. def keys(self):
  156. """method from the `dict` interface returning a tuple containing
  157. locally defined names
  158. """
  159. return self.locals.keys()
  160. def values(self):
  161. """method from the `dict` interface returning a tuple containing
  162. locally defined nodes which are instance of `Function` or `Class`
  163. """
  164. return [self[key] for key in self.keys()]
  165. def items(self):
  166. """method from the `dict` interface returning a list of tuple
  167. containing each locally defined name with its associated node,
  168. which is an instance of `Function` or `Class`
  169. """
  170. return zip(self.keys(), self.values())
  171. def __contains__(self, name):
  172. return name in self.locals
  173. has_key = __contains__
  174. # Module #####################################################################
  175. class Module(LocalsDictNodeNG):
  176. _astng_fields = ('body',)
  177. fromlineno = 0
  178. lineno = 0
  179. # attributes below are set by the builder module or by raw factories
  180. # the file from which as been extracted the astng representation. It may
  181. # be None if the representation has been built from a built-in module
  182. file = None
  183. # the module name
  184. name = None
  185. # boolean for astng built from source (i.e. ast)
  186. pure_python = None
  187. # boolean for package module
  188. package = None
  189. # dictionary of globals with name as key and node defining the global
  190. # as value
  191. globals = None
  192. # names of python special attributes (handled by getattr impl.)
  193. special_attributes = set(('__name__', '__doc__', '__file__', '__path__',
  194. '__dict__'))
  195. # names of module attributes available through the global scope
  196. scope_attrs = set(('__name__', '__doc__', '__file__', '__path__'))
  197. def __init__(self, name, doc, pure_python=True):
  198. self.name = name
  199. self.doc = doc
  200. self.pure_python = pure_python
  201. self.locals = self.globals = {}
  202. self.body = []
  203. @property
  204. def file_stream(self):
  205. if self.file is not None:
  206. return open(self.file)
  207. return None
  208. def block_range(self, lineno):
  209. """return block line numbers.
  210. start from the beginning whatever the given lineno
  211. """
  212. return self.fromlineno, self.tolineno
  213. def scope_lookup(self, node, name, offset=0):
  214. if name in self.scope_attrs and not name in self.locals:
  215. try:
  216. return self, self.getattr(name)
  217. except NotFoundError:
  218. return self, ()
  219. return self._scope_lookup(node, name, offset)
  220. def pytype(self):
  221. return '%s.module' % BUILTINS_MODULE
  222. def display_type(self):
  223. return 'Module'
  224. def getattr(self, name, context=None, ignore_locals=False):
  225. if name in self.special_attributes:
  226. if name == '__file__':
  227. return [cf(self.file)] + self.locals.get(name, [])
  228. if name == '__path__' and self.package:
  229. return [List()] + self.locals.get(name, [])
  230. return std_special_attributes(self, name)
  231. if not ignore_locals and name in self.locals:
  232. return self.locals[name]
  233. if self.package:
  234. try:
  235. return [self.import_module(name, relative_only=True)]
  236. except ASTNGBuildingException:
  237. raise NotFoundError(name)
  238. except Exception:# XXX pylint tests never pass here; do we need it?
  239. import traceback
  240. traceback.print_exc()
  241. raise NotFoundError(name)
  242. getattr = remove_nodes(getattr, DelName)
  243. def igetattr(self, name, context=None):
  244. """inferred getattr"""
  245. # set lookup name since this is necessary to infer on import nodes for
  246. # instance
  247. context = copy_context(context)
  248. context.lookupname = name
  249. try:
  250. return _infer_stmts(self.getattr(name, context), context, frame=self)
  251. except NotFoundError:
  252. raise InferenceError(name)
  253. def fully_defined(self):
  254. """return True if this module has been built from a .py file
  255. and so contains a complete representation including the code
  256. """
  257. return self.file is not None and self.file.endswith('.py')
  258. def statement(self):
  259. """return the first parent node marked as statement node
  260. consider a module as a statement...
  261. """
  262. return self
  263. def previous_sibling(self):
  264. """module has no sibling"""
  265. return
  266. def next_sibling(self):
  267. """module has no sibling"""
  268. return
  269. if sys.version_info < (2, 8):
  270. def absolute_import_activated(self):
  271. for stmt in self.locals.get('absolute_import', ()):
  272. if isinstance(stmt, From) and stmt.modname == '__future__':
  273. return True
  274. return False
  275. else:
  276. absolute_import_activated = lambda self: True
  277. def import_module(self, modname, relative_only=False, level=None):
  278. """import the given module considering self as context"""
  279. if relative_only and level is None:
  280. level = 0
  281. absmodname = self.relative_to_absolute_name(modname, level)
  282. try:
  283. return MANAGER.astng_from_module_name(absmodname)
  284. except ASTNGBuildingException:
  285. # we only want to import a sub module or package of this module,
  286. # skip here
  287. if relative_only:
  288. raise
  289. return MANAGER.astng_from_module_name(modname)
  290. def relative_to_absolute_name(self, modname, level):
  291. """return the absolute module name for a relative import.
  292. The relative import can be implicit or explicit.
  293. """
  294. # XXX this returns non sens when called on an absolute import
  295. # like 'pylint.checkers.logilab.astng.utils'
  296. # XXX doesn't return absolute name if self.name isn't absolute name
  297. if self.absolute_import_activated() and level is None:
  298. return modname
  299. if level:
  300. if self.package:
  301. level = level - 1
  302. package_name = self.name.rsplit('.', level)[0]
  303. elif self.package:
  304. package_name = self.name
  305. else:
  306. package_name = self.name.rsplit('.', 1)[0]
  307. if package_name:
  308. if not modname:
  309. return package_name
  310. return '%s.%s' % (package_name, modname)
  311. return modname
  312. def wildcard_import_names(self):
  313. """return the list of imported names when this module is 'wildcard
  314. imported'
  315. It doesn't include the '__builtins__' name which is added by the
  316. current CPython implementation of wildcard imports.
  317. """
  318. # take advantage of a living module if it exists
  319. try:
  320. living = sys.modules[self.name]
  321. except KeyError:
  322. pass
  323. else:
  324. try:
  325. return living.__all__
  326. except AttributeError:
  327. return [name for name in living.__dict__.keys()
  328. if not name.startswith('_')]
  329. # else lookup the astng
  330. #
  331. # We separate the different steps of lookup in try/excepts
  332. # to avoid catching too many Exceptions
  333. # However, we can not analyse dynamically constructed __all__
  334. try:
  335. all = self['__all__']
  336. except KeyError:
  337. return [name for name in self.keys() if not name.startswith('_')]
  338. try:
  339. explicit = all.assigned_stmts().next()
  340. except InferenceError:
  341. return [name for name in self.keys() if not name.startswith('_')]
  342. except AttributeError:
  343. # not an assignment node
  344. # XXX infer?
  345. return [name for name in self.keys() if not name.startswith('_')]
  346. try:
  347. # should be a Tuple/List of constant string / 1 string not allowed
  348. return [const.value for const in explicit.elts]
  349. except AttributeError:
  350. return [name for name in self.keys() if not name.startswith('_')]
  351. class ComprehensionScope(LocalsDictNodeNG):
  352. def frame(self):
  353. return self.parent.frame()
  354. scope_lookup = LocalsDictNodeNG._scope_lookup
  355. class GenExpr(ComprehensionScope):
  356. _astng_fields = ('elt', 'generators')
  357. def __init__(self):
  358. self.locals = {}
  359. self.elt = None
  360. self.generators = []
  361. class DictComp(ComprehensionScope):
  362. _astng_fields = ('key', 'value', 'generators')
  363. def __init__(self):
  364. self.locals = {}
  365. self.key = None
  366. self.value = None
  367. self.generators = []
  368. class SetComp(ComprehensionScope):
  369. _astng_fields = ('elt', 'generators')
  370. def __init__(self):
  371. self.locals = {}
  372. self.elt = None
  373. self.generators = []
  374. class _ListComp(NodeNG):
  375. """class representing a ListComp node"""
  376. _astng_fields = ('elt', 'generators')
  377. elt = None
  378. generators = None
  379. if sys.version_info >= (3, 0):
  380. class ListComp(_ListComp, ComprehensionScope):
  381. """class representing a ListComp node"""
  382. def __init__(self):
  383. self.locals = {}
  384. else:
  385. class ListComp(_ListComp):
  386. """class representing a ListComp node"""
  387. # Function ###################################################################
  388. class Lambda(LocalsDictNodeNG, FilterStmtsMixin):
  389. _astng_fields = ('args', 'body',)
  390. name = '<lambda>'
  391. # function's type, 'function' | 'method' | 'staticmethod' | 'classmethod'
  392. type = 'function'
  393. def __init__(self):
  394. self.locals = {}
  395. self.args = []
  396. self.body = []
  397. def pytype(self):
  398. if 'method' in self.type:
  399. return '%s.instancemethod' % BUILTINS_MODULE
  400. return '%s.function' % BUILTINS_MODULE
  401. def display_type(self):
  402. if 'method' in self.type:
  403. return 'Method'
  404. return 'Function'
  405. def callable(self):
  406. return True
  407. def argnames(self):
  408. """return a list of argument names"""
  409. if self.args.args: # maybe None with builtin functions
  410. names = _rec_get_names(self.args.args)
  411. else:
  412. names = []
  413. if self.args.vararg:
  414. names.append(self.args.vararg)
  415. if self.args.kwarg:
  416. names.append(self.args.kwarg)
  417. return names
  418. def infer_call_result(self, caller, context=None):
  419. """infer what a function is returning when called"""
  420. return self.body.infer(context)
  421. def scope_lookup(self, node, name, offset=0):
  422. if node in self.args.defaults:
  423. frame = self.parent.frame()
  424. # line offset to avoid that def func(f=func) resolve the default
  425. # value to the defined function
  426. offset = -1
  427. else:
  428. # check this is not used in function decorators
  429. frame = self
  430. return frame._scope_lookup(node, name, offset)
  431. class Function(Statement, Lambda):
  432. _astng_fields = ('decorators', 'args', 'body')
  433. special_attributes = set(('__name__', '__doc__', '__dict__'))
  434. is_function = True
  435. # attributes below are set by the builder module or by raw factories
  436. blockstart_tolineno = None
  437. decorators = None
  438. def __init__(self, name, doc):
  439. self.locals = {}
  440. self.args = []
  441. self.body = []
  442. self.decorators = None
  443. self.name = name
  444. self.doc = doc
  445. self.extra_decorators = []
  446. self.instance_attrs = {}
  447. def set_line_info(self, lastchild):
  448. self.fromlineno = self.lineno
  449. # lineno is the line number of the first decorator, we want the def statement lineno
  450. if self.decorators is not None:
  451. self.fromlineno += len(self.decorators.nodes)
  452. self.tolineno = lastchild.tolineno
  453. self.blockstart_tolineno = self.args.tolineno
  454. def block_range(self, lineno):
  455. """return block line numbers.
  456. start from the "def" position whatever the given lineno
  457. """
  458. return self.fromlineno, self.tolineno
  459. def getattr(self, name, context=None):
  460. """this method doesn't look in the instance_attrs dictionary since it's
  461. done by an Instance proxy at inference time.
  462. """
  463. if name == '__module__':
  464. return [cf(self.root().qname())]
  465. if name in self.instance_attrs:
  466. return self.instance_attrs[name]
  467. return std_special_attributes(self, name, False)
  468. def is_method(self):
  469. """return true if the function node should be considered as a method"""
  470. # check we are defined in a Class, because this is usually expected
  471. # (e.g. pylint...) when is_method() return True
  472. return self.type != 'function' and isinstance(self.parent.frame(), Class)
  473. def decoratornames(self):
  474. """return a list of decorator qualified names"""
  475. result = set()
  476. decoratornodes = []
  477. if self.decorators is not None:
  478. decoratornodes += self.decorators.nodes
  479. decoratornodes += self.extra_decorators
  480. for decnode in decoratornodes:
  481. for infnode in decnode.infer():
  482. result.add(infnode.qname())
  483. return result
  484. decoratornames = cached(decoratornames)
  485. def is_bound(self):
  486. """return true if the function is bound to an Instance or a class"""
  487. return self.type == 'classmethod'
  488. def is_abstract(self, pass_is_abstract=True):
  489. """return true if the method is abstract
  490. It's considered as abstract if the only statement is a raise of
  491. NotImplementError, or, if pass_is_abstract, a pass statement
  492. """
  493. for child_node in self.body:
  494. if isinstance(child_node, Raise):
  495. if child_node.raises_not_implemented():
  496. return True
  497. if pass_is_abstract and isinstance(child_node, Pass):
  498. return True
  499. return False
  500. # empty function is the same as function with a single "pass" statement
  501. if pass_is_abstract:
  502. return True
  503. def is_generator(self):
  504. """return true if this is a generator function"""
  505. # XXX should be flagged, not computed
  506. try:
  507. return self.nodes_of_class(Yield, skip_klass=Function).next()
  508. except StopIteration:
  509. return False
  510. def infer_call_result(self, caller, context=None):
  511. """infer what a function is returning when called"""
  512. if self.is_generator():
  513. yield Generator(self)
  514. return
  515. returns = self.nodes_of_class(Return, skip_klass=Function)
  516. for returnnode in returns:
  517. if returnnode.value is None:
  518. yield Const(None)
  519. else:
  520. try:
  521. for infered in returnnode.value.infer(context):
  522. yield infered
  523. except InferenceError:
  524. yield YES
  525. def _rec_get_names(args, names=None):
  526. """return a list of all argument names"""
  527. if names is None:
  528. names = []
  529. for arg in args:
  530. if isinstance(arg, Tuple):
  531. _rec_get_names(arg.elts, names)
  532. else:
  533. names.append(arg.name)
  534. return names
  535. # Class ######################################################################
  536. def _class_type(klass, ancestors=None):
  537. """return a Class node type to differ metaclass, interface and exception
  538. from 'regular' classes
  539. """
  540. # XXX we have to store ancestors in case we have a ancestor loop
  541. if klass._type is not None:
  542. return klass._type
  543. if klass.name == 'type':
  544. klass._type = 'metaclass'
  545. elif klass.name.endswith('Interface'):
  546. klass._type = 'interface'
  547. elif klass.name.endswith('Exception'):
  548. klass._type = 'exception'
  549. else:
  550. if ancestors is None:
  551. ancestors = set()
  552. if klass in ancestors:
  553. # XXX we are in loop ancestors, and have found no type
  554. klass._type = 'class'
  555. return 'class'
  556. ancestors.add(klass)
  557. # print >> sys.stderr, '_class_type', repr(klass)
  558. for base in klass.ancestors(recurs=False):
  559. if _class_type(base, ancestors) != 'class':
  560. klass._type = base.type
  561. break
  562. if klass._type is None:
  563. klass._type = 'class'
  564. return klass._type
  565. def _iface_hdlr(iface_node):
  566. """a handler function used by interfaces to handle suspicious
  567. interface nodes
  568. """
  569. return True
  570. class Class(Statement, LocalsDictNodeNG, FilterStmtsMixin):
  571. # some of the attributes below are set by the builder module or
  572. # by a raw factories
  573. # a dictionary of class instances attributes
  574. _astng_fields = ('decorators', 'bases', 'body') # name
  575. decorators = None
  576. special_attributes = set(('__name__', '__doc__', '__dict__', '__module__',
  577. '__bases__', '__mro__', '__subclasses__'))
  578. blockstart_tolineno = None
  579. _type = None
  580. type = property(_class_type,
  581. doc="class'type, possible values are 'class' | "
  582. "'metaclass' | 'interface' | 'exception'")
  583. def __init__(self, name, doc):
  584. self.instance_attrs = {}
  585. self.locals = {}
  586. self.bases = []
  587. self.body = []
  588. self.name = name
  589. self.doc = doc
  590. def _newstyle_impl(self, context=None):
  591. if context is None:
  592. context = InferenceContext()
  593. if self._newstyle is not None:
  594. return self._newstyle
  595. for base in self.ancestors(recurs=False, context=context):
  596. if base._newstyle_impl(context):
  597. self._newstyle = True
  598. break
  599. if self._newstyle is None:
  600. self._newstyle = False
  601. return self._newstyle
  602. _newstyle = None
  603. newstyle = property(_newstyle_impl,
  604. doc="boolean indicating if it's a new style class"
  605. "or not")
  606. def set_line_info(self, lastchild):
  607. self.fromlineno = self.lineno
  608. self.blockstart_tolineno = self.bases and self.bases[-1].tolineno or self.fromlineno
  609. if lastchild is not None:
  610. self.tolineno = lastchild.tolineno
  611. # else this is a class with only a docstring, then tolineno is (should be) already ok
  612. def block_range(self, lineno):
  613. """return block line numbers.
  614. start from the "class" position whatever the given lineno
  615. """
  616. return self.fromlineno, self.tolineno
  617. def pytype(self):
  618. if self.newstyle:
  619. return '%s.type' % BUILTINS_MODULE
  620. return '%s.classobj' % BUILTINS_MODULE
  621. def display_type(self):
  622. return 'Class'
  623. def callable(self):
  624. return True
  625. def infer_call_result(self, caller, context=None):
  626. """infer what a class is returning when called"""
  627. yield Instance(self)
  628. def scope_lookup(self, node, name, offset=0):
  629. if node in self.bases:
  630. frame = self.parent.frame()
  631. # line offset to avoid that class A(A) resolve the ancestor to
  632. # the defined class
  633. offset = -1
  634. else:
  635. frame = self
  636. return frame._scope_lookup(node, name, offset)
  637. # list of parent class as a list of string (i.e. names as they appear
  638. # in the class definition) XXX bw compat
  639. def basenames(self):
  640. return [bnode.as_string() for bnode in self.bases]
  641. basenames = property(basenames)
  642. def ancestors(self, recurs=True, context=None):
  643. """return an iterator on the node base classes in a prefixed
  644. depth first order
  645. :param recurs:
  646. boolean indicating if it should recurse or return direct
  647. ancestors only
  648. """
  649. # FIXME: should be possible to choose the resolution order
  650. # XXX inference make infinite loops possible here (see BaseTransformer
  651. # manipulation in the builder module for instance)
  652. yielded = set([self])
  653. if context is None:
  654. context = InferenceContext()
  655. for stmt in self.bases:
  656. with context.restore_path():
  657. try:
  658. for baseobj in stmt.infer(context):
  659. if not isinstance(baseobj, Class):
  660. # duh ?
  661. continue
  662. if baseobj in yielded:
  663. continue # cf xxx above
  664. yielded.add(baseobj)
  665. yield baseobj
  666. if recurs:
  667. for grandpa in baseobj.ancestors(True, context):
  668. if grandpa in yielded:
  669. continue # cf xxx above
  670. yielded.add(grandpa)
  671. yield grandpa
  672. except InferenceError:
  673. # XXX log error ?
  674. continue
  675. def local_attr_ancestors(self, name, context=None):
  676. """return an iterator on astng representation of parent classes
  677. which have <name> defined in their locals
  678. """
  679. for astng in self.ancestors(context=context):
  680. if name in astng:
  681. yield astng
  682. def instance_attr_ancestors(self, name, context=None):
  683. """return an iterator on astng representation of parent classes
  684. which have <name> defined in their instance attribute dictionary
  685. """
  686. for astng in self.ancestors(context=context):
  687. if name in astng.instance_attrs:
  688. yield astng
  689. def has_base(self, node):
  690. return node in self.bases
  691. def local_attr(self, name, context=None):
  692. """return the list of assign node associated to name in this class
  693. locals or in its parents
  694. :raises `NotFoundError`:
  695. if no attribute with this name has been find in this class or
  696. its parent classes
  697. """
  698. try:
  699. return self.locals[name]
  700. except KeyError:
  701. # get if from the first parent implementing it if any
  702. for class_node in self.local_attr_ancestors(name, context):
  703. return class_node.locals[name]
  704. raise NotFoundError(name)
  705. local_attr = remove_nodes(local_attr, DelAttr)
  706. def instance_attr(self, name, context=None):
  707. """return the astng nodes associated to name in this class instance
  708. attributes dictionary and in its parents
  709. :raises `NotFoundError`:
  710. if no attribute with this name has been find in this class or
  711. its parent classes
  712. """
  713. values = self.instance_attrs.get(name, [])
  714. # get all values from parents
  715. for class_node in self.instance_attr_ancestors(name, context):
  716. values += class_node.instance_attrs[name]
  717. if not values:
  718. raise NotFoundError(name)
  719. return values
  720. instance_attr = remove_nodes(instance_attr, DelAttr)
  721. def instanciate_class(self):
  722. """return Instance of Class node, else return self"""
  723. return Instance(self)
  724. def getattr(self, name, context=None):
  725. """this method doesn't look in the instance_attrs dictionary since it's
  726. done by an Instance proxy at inference time.
  727. It may return a YES object if the attribute has not been actually
  728. found but a __getattr__ or __getattribute__ method is defined
  729. """
  730. values = self.locals.get(name, [])
  731. if name in self.special_attributes:
  732. if name == '__module__':
  733. return [cf(self.root().qname())] + values
  734. # FIXME : what is expected by passing the list of ancestors to cf:
  735. # you can just do [cf(tuple())] + values without breaking any test
  736. # this is ticket http://www.logilab.org/ticket/52785
  737. if name == '__bases__':
  738. return [cf(tuple(self.ancestors(recurs=False, context=context)))] + values
  739. # XXX need proper meta class handling + MRO implementation
  740. if name == '__mro__' and self.newstyle:
  741. # XXX mro is read-only but that's not our job to detect that
  742. return [cf(tuple(self.ancestors(recurs=True, context=context)))] + values
  743. return std_special_attributes(self, name)
  744. # don't modify the list in self.locals!
  745. values = list(values)
  746. for classnode in self.ancestors(recurs=True, context=context):
  747. values += classnode.locals.get(name, [])
  748. if not values:
  749. raise NotFoundError(name)
  750. return values
  751. def igetattr(self, name, context=None):
  752. """inferred getattr, need special treatment in class to handle
  753. descriptors
  754. """
  755. # set lookup name since this is necessary to infer on import nodes for
  756. # instance
  757. context = copy_context(context)
  758. context.lookupname = name
  759. try:
  760. for infered in _infer_stmts(self.getattr(name, context), context,
  761. frame=self):
  762. # yield YES object instead of descriptors when necessary
  763. if not isinstance(infered, Const) and isinstance(infered, Instance):
  764. try:
  765. infered._proxied.getattr('__get__', context)
  766. except NotFoundError:
  767. yield infered
  768. else:
  769. yield YES
  770. else:
  771. yield function_to_method(infered, self)
  772. except NotFoundError:
  773. if not name.startswith('__') and self.has_dynamic_getattr(context):
  774. # class handle some dynamic attributes, return a YES object
  775. yield YES
  776. else:
  777. raise InferenceError(name)
  778. def has_dynamic_getattr(self, context=None):
  779. """return True if the class has a custom __getattr__ or
  780. __getattribute__ method
  781. """
  782. # need to explicitly handle optparse.Values (setattr is not detected)
  783. if self.name == 'Values' and self.root().name == 'optparse':
  784. return True
  785. try:
  786. self.getattr('__getattr__', context)
  787. return True
  788. except NotFoundError:
  789. #if self.newstyle: XXX cause an infinite recursion error
  790. try:
  791. getattribute = self.getattr('__getattribute__', context)[0]
  792. if getattribute.root().name != BUILTINS_NAME:
  793. # class has a custom __getattribute__ defined
  794. return True
  795. except NotFoundError:
  796. pass
  797. return False
  798. def methods(self):
  799. """return an iterator on all methods defined in the class and
  800. its ancestors
  801. """
  802. done = {}
  803. for astng in chain(iter((self,)), self.ancestors()):
  804. for meth in astng.mymethods():
  805. if meth.name in done:
  806. continue
  807. done[meth.name] = None
  808. yield meth
  809. def mymethods(self):
  810. """return an iterator on all methods defined in the class"""
  811. for member in self.values():
  812. if isinstance(member, Function):
  813. yield member
  814. def interfaces(self, herited=True, handler_func=_iface_hdlr):
  815. """return an iterator on interfaces implemented by the given
  816. class node
  817. """
  818. # FIXME: what if __implements__ = (MyIFace, MyParent.__implements__)...
  819. try:
  820. implements = Instance(self).getattr('__implements__')[0]
  821. except NotFoundError:
  822. return
  823. if not herited and not implements.frame() is self:
  824. return
  825. found = set()
  826. missing = False
  827. for iface in unpack_infer(implements):
  828. if iface is YES:
  829. missing = True
  830. continue
  831. if not iface in found and handler_func(iface):
  832. found.add(iface)
  833. yield iface
  834. if missing:
  835. raise InferenceError()