OLD | NEW |
(Empty) | |
| 1 # copyright 2003-2013 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 """This module contains some mixins for the different nodes. |
| 19 """ |
| 20 |
| 21 from logilab.common.decorators import cachedproperty |
| 22 |
| 23 from astroid.exceptions import (AstroidBuildingException, InferenceError, |
| 24 NotFoundError) |
| 25 |
| 26 |
| 27 class BlockRangeMixIn(object): |
| 28 """override block range """ |
| 29 |
| 30 @cachedproperty |
| 31 def blockstart_tolineno(self): |
| 32 return self.lineno |
| 33 |
| 34 def _elsed_block_range(self, lineno, orelse, last=None): |
| 35 """handle block line numbers range for try/finally, for, if and while |
| 36 statements |
| 37 """ |
| 38 if lineno == self.fromlineno: |
| 39 return lineno, lineno |
| 40 if orelse: |
| 41 if lineno >= orelse[0].fromlineno: |
| 42 return lineno, orelse[-1].tolineno |
| 43 return lineno, orelse[0].fromlineno - 1 |
| 44 return lineno, last or self.tolineno |
| 45 |
| 46 |
| 47 class FilterStmtsMixin(object): |
| 48 """Mixin for statement filtering and assignment type""" |
| 49 |
| 50 def _get_filtered_stmts(self, _, node, _stmts, mystmt): |
| 51 """method used in _filter_stmts to get statemtents and trigger break""" |
| 52 if self.statement() is mystmt: |
| 53 # original node's statement is the assignment, only keep |
| 54 # current node (gen exp, list comp) |
| 55 return [node], True |
| 56 return _stmts, False |
| 57 |
| 58 def ass_type(self): |
| 59 return self |
| 60 |
| 61 |
| 62 class AssignTypeMixin(object): |
| 63 |
| 64 def ass_type(self): |
| 65 return self |
| 66 |
| 67 def _get_filtered_stmts(self, lookup_node, node, _stmts, mystmt): |
| 68 """method used in filter_stmts""" |
| 69 if self is mystmt: |
| 70 return _stmts, True |
| 71 if self.statement() is mystmt: |
| 72 # original node's statement is the assignment, only keep |
| 73 # current node (gen exp, list comp) |
| 74 return [node], True |
| 75 return _stmts, False |
| 76 |
| 77 |
| 78 class ParentAssignTypeMixin(AssignTypeMixin): |
| 79 |
| 80 def ass_type(self): |
| 81 return self.parent.ass_type() |
| 82 |
| 83 |
| 84 class FromImportMixIn(FilterStmtsMixin): |
| 85 """MixIn for From and Import Nodes""" |
| 86 |
| 87 def _infer_name(self, frame, name): |
| 88 return name |
| 89 |
| 90 def do_import_module(self, modname=None): |
| 91 """return the ast for a module whose name is <modname> imported by <self
> |
| 92 """ |
| 93 # handle special case where we are on a package node importing a module |
| 94 # using the same name as the package, which may end in an infinite loop |
| 95 # on relative imports |
| 96 # XXX: no more needed ? |
| 97 mymodule = self.root() |
| 98 level = getattr(self, 'level', None) # Import as no level |
| 99 if modname is None: |
| 100 modname = self.modname |
| 101 # XXX we should investigate deeper if we really want to check |
| 102 # importing itself: modname and mymodule.name be relative or absolute |
| 103 if mymodule.relative_to_absolute_name(modname, level) == mymodule.name: |
| 104 # FIXME: we used to raise InferenceError here, but why ? |
| 105 return mymodule |
| 106 try: |
| 107 return mymodule.import_module(modname, level=level) |
| 108 except AstroidBuildingException: |
| 109 raise InferenceError(modname) |
| 110 except SyntaxError as ex: |
| 111 raise InferenceError(str(ex)) |
| 112 |
| 113 def real_name(self, asname): |
| 114 """get name from 'as' name""" |
| 115 for name, _asname in self.names: |
| 116 if name == '*': |
| 117 return asname |
| 118 if not _asname: |
| 119 name = name.split('.', 1)[0] |
| 120 _asname = name |
| 121 if asname == _asname: |
| 122 return name |
| 123 raise NotFoundError(asname) |
| 124 |
OLD | NEW |