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