OLD | NEW |
1 # copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. | 1 # This program is free software; you can redistribute it and/or modify it under |
| 2 # the terms of the GNU Lesser General Public License as published by the Free So
ftware |
| 3 # Foundation; either version 2 of the License, or (at your option) any later |
| 4 # version. |
| 5 # |
| 6 # This program is distributed in the hope that it will be useful, but WITHOUT |
| 7 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 8 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more d
etails. |
| 9 # |
| 10 # You should have received a copy of the GNU Lesser General Public License along
with |
| 11 # this program; if not, write to the Free Software Foundation, Inc., |
| 12 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 13 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr | 14 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
| 15 # copyright 2003-2010 Sylvain Thenault, all rights reserved. |
| 16 # contact mailto:thenault@gmail.com |
3 # | 17 # |
4 # This file is part of astroid. | 18 # This file is part of logilab-astng. |
5 # | 19 # |
6 # astroid is free software: you can redistribute it and/or modify it | 20 # logilab-astng 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 | 21 # 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 | 22 # Free Software Foundation, either version 2.1 of the License, or (at your |
9 # option) any later version. | 23 # option) any later version. |
10 # | 24 # |
11 # astroid is distributed in the hope that it will be useful, but | 25 # logilab-astng is distributed in the hope that it will be useful, but |
12 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | 26 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | 27 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
14 # for more details. | 28 # for more details. |
15 # | 29 # |
16 # You should have received a copy of the GNU Lesser General Public License along | 30 # 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/>. | 31 # with logilab-astng. If not, see <http://www.gnu.org/licenses/>. |
18 """This module contains some mixins for the different nodes. | 32 """This module contains some mixins for the different nodes. |
19 """ | 33 """ |
20 | 34 |
21 from astroid.exceptions import (AstroidBuildingException, InferenceError, | 35 from logilab.astng.exceptions import (ASTNGBuildingException, InferenceError, |
22 NotFoundError) | 36 NotFoundError) |
23 | 37 |
24 | 38 |
25 class BlockRangeMixIn(object): | 39 class BlockRangeMixIn(object): |
26 """override block range """ | 40 """override block range """ |
27 def set_line_info(self, lastchild): | 41 def set_line_info(self, lastchild): |
28 self.fromlineno = self.lineno | 42 self.fromlineno = self.lineno |
29 self.tolineno = lastchild.tolineno | 43 self.tolineno = lastchild.tolineno |
30 self.blockstart_tolineno = self._blockstart_toline() | 44 self.blockstart_tolineno = self._blockstart_toline() |
31 | 45 |
32 def _elsed_block_range(self, lineno, orelse, last=None): | 46 def _elsed_block_range(self, lineno, orelse, last=None): |
33 """handle block line numbers range for try/finally, for, if and while | 47 """handle block line numbers range for try/finally, for, if and while |
34 statements | 48 statements |
35 """ | 49 """ |
36 if lineno == self.fromlineno: | 50 if lineno == self.fromlineno: |
37 return lineno, lineno | 51 return lineno, lineno |
38 if orelse: | 52 if orelse: |
39 if lineno >= orelse[0].fromlineno: | 53 if lineno >= orelse[0].fromlineno: |
40 return lineno, orelse[-1].tolineno | 54 return lineno, orelse[-1].tolineno |
41 return lineno, orelse[0].fromlineno - 1 | 55 return lineno, orelse[0].fromlineno - 1 |
42 return lineno, last or self.tolineno | 56 return lineno, last or self.tolineno |
43 | 57 |
44 | |
45 class FilterStmtsMixin(object): | 58 class FilterStmtsMixin(object): |
46 """Mixin for statement filtering and assignment type""" | 59 """Mixin for statement filtering and assignment type""" |
47 | 60 |
48 def _get_filtered_stmts(self, _, node, _stmts, mystmt): | 61 def _get_filtered_stmts(self, _, node, _stmts, mystmt): |
49 """method used in _filter_stmts to get statemtents and trigger break""" | 62 """method used in _filter_stmts to get statemtents and trigger break""" |
50 if self.statement() is mystmt: | 63 if self.statement() is mystmt: |
51 # original node's statement is the assignment, only keep | 64 # original node's statement is the assignment, only keep |
52 # current node (gen exp, list comp) | 65 # current node (gen exp, list comp) |
53 return [node], True | 66 return [node], True |
54 return _stmts, False | 67 return _stmts, False |
(...skipping 17 matching lines...) Expand all Loading... |
72 return [node], True | 85 return [node], True |
73 return _stmts, False | 86 return _stmts, False |
74 | 87 |
75 | 88 |
76 class ParentAssignTypeMixin(AssignTypeMixin): | 89 class ParentAssignTypeMixin(AssignTypeMixin): |
77 | 90 |
78 def ass_type(self): | 91 def ass_type(self): |
79 return self.parent.ass_type() | 92 return self.parent.ass_type() |
80 | 93 |
81 | 94 |
| 95 |
82 class FromImportMixIn(FilterStmtsMixin): | 96 class FromImportMixIn(FilterStmtsMixin): |
83 """MixIn for From and Import Nodes""" | 97 """MixIn for From and Import Nodes""" |
84 | 98 |
85 def _infer_name(self, frame, name): | 99 def _infer_name(self, frame, name): |
86 return name | 100 return name |
87 | 101 |
88 def do_import_module(self, modname=None): | 102 def do_import_module(self, modname): |
89 """return the ast for a module whose name is <modname> imported by <self
> | 103 """return the ast for a module whose name is <modname> imported by <self
> |
90 """ | 104 """ |
91 # handle special case where we are on a package node importing a module | 105 # 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 | 106 # using the same name as the package, which may end in an infinite loop |
93 # on relative imports | 107 # on relative imports |
94 # XXX: no more needed ? | 108 # XXX: no more needed ? |
95 mymodule = self.root() | 109 mymodule = self.root() |
96 level = getattr(self, 'level', None) # Import as no level | 110 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 | 111 # XXX we should investigate deeper if we really want to check |
100 # importing itself: modname and mymodule.name be relative or absolute | 112 # importing itself: modname and mymodule.name be relative or absolute |
101 if mymodule.relative_to_absolute_name(modname, level) == mymodule.name: | 113 if mymodule.relative_to_absolute_name(modname, level) == mymodule.name: |
102 # FIXME: we used to raise InferenceError here, but why ? | 114 # FIXME: we used to raise InferenceError here, but why ? |
103 return mymodule | 115 return mymodule |
104 try: | 116 try: |
105 return mymodule.import_module(modname, level=level) | 117 return mymodule.import_module(modname, level=level) |
106 except AstroidBuildingException: | 118 except ASTNGBuildingException: |
107 raise InferenceError(modname) | 119 raise InferenceError(modname) |
108 except SyntaxError, ex: | 120 except SyntaxError, ex: |
109 raise InferenceError(str(ex)) | 121 raise InferenceError(str(ex)) |
110 | 122 |
111 def real_name(self, asname): | 123 def real_name(self, asname): |
112 """get name from 'as' name""" | 124 """get name from 'as' name""" |
113 for name, _asname in self.names: | 125 for name, _asname in self.names: |
114 if name == '*': | 126 if name == '*': |
115 return asname | 127 return asname |
116 if not _asname: | 128 if not _asname: |
117 name = name.split('.', 1)[0] | 129 name = name.split('.', 1)[0] |
118 _asname = name | 130 _asname = name |
119 if asname == _asname: | 131 if asname == _asname: |
120 return name | 132 return name |
121 raise NotFoundError(asname) | 133 raise NotFoundError(asname) |
122 | 134 |
| 135 |
| 136 |
OLD | NEW |