diff options
-rw-r--r-- | _nodes.py | 75 | ||||
-rw-r--r-- | inference.py | 2 | ||||
-rw-r--r-- | infutils.py | 2 | ||||
-rw-r--r-- | nodes.py | 10 | ||||
-rw-r--r-- | protocols.py | 2 | ||||
-rw-r--r-- | scoped_nodes.py | 2 |
6 files changed, 81 insertions, 12 deletions
diff --git a/_nodes.py b/_nodes.py new file mode 100644 index 00000000..5958378d --- /dev/null +++ b/_nodes.py @@ -0,0 +1,75 @@ +# -*- coding: utf-8 -*- +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., +# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +""" +Module containing the node classes; it is only used for avoiding circular imports +""" + +from __future__ import generators + +__docformat__ = "restructuredtext en" + +from itertools import imap + +try: + from logilab.astng._nodes_ast import * + from logilab.astng._nodes_ast import _const_factory + AST_MODE = '_ast' +except ImportError: + from logilab.astng._nodes_compiler import * + from logilab.astng._nodes_compiler import _const_factory + AST_MODE = 'compiler' + + +INFER_NEED_NAME_STMTS = (From, Import, Global, TryExcept) +LOOP_SCOPES = (Comprehension, For,) + + +STMT_NODES = ( + Assert, Assign, AugAssign, Break, Class, Continue, Delete, Discard, + ExceptHandler, Exec, For, From, Function, Global, If, Import, Pass, Print, + Raise, Return, TryExcept, TryFinally, While, With, Yield + ) + +ALL_NODES = STMT_NODES + ( + Arguments, AssAttr, AssName, BinOp, BoolOp, Backquote, CallFunc, Compare, + Comprehension, Const, Decorators, DelAttr, DelName, Dict, Ellipsis, + EmptyNode, ExtSlice, Getattr, GenExpr, IfExp, Index, Keyword, Lambda, + List, ListComp, Module, Name, Slice, Subscript, UnaryOp, Tuple + ) + +# constants ... ############################################################## + +CONST_CLS = { + list: List, + tuple: Tuple, + dict: Dict, + } + +def const_factory(value): + """return an astng node for a python value""" + try: + # if value is of class list, tuple, dict use specific class, not Const + cls = CONST_CLS[value.__class__] + node = cls() + if isinstance(node, Dict): + node.items = () + else: + node.elts = () + except KeyError: + try: + node = Const(value) + except KeyError: + node = _const_factory(value) + return node + diff --git a/inference.py b/inference.py index f8ab6169..5bb3e77a 100644 --- a/inference.py +++ b/inference.py @@ -30,7 +30,7 @@ try: except NameError: class GeneratorExit(Exception): pass -from logilab.astng import MANAGER, nodes, raw_building +from logilab.astng import MANAGER, _nodes as nodes, raw_building from logilab.astng import ASTNGError, InferenceError, UnresolvableName, \ NoDefault, NotFoundError, ASTNGBuildingException from logilab.astng.infutils import YES, Instance, InferenceContext, \ diff --git a/infutils.py b/infutils.py index 4226b0f7..55ba0e62 100644 --- a/infutils.py +++ b/infutils.py @@ -25,7 +25,7 @@ __doctype__ = "restructuredtext en" from logilab.common.compat import chain, imap from logilab.astng._exceptions import InferenceError, NotFoundError, UnresolvableName -from logilab.astng.nodes import Proxy_, List, Tuple, Function, If, TryExcept +from logilab.astng._nodes import Proxy_, List, Tuple, Function, If, TryExcept class Proxy(Proxy_): @@ -41,14 +41,8 @@ __docformat__ = "restructuredtext en" from itertools import imap -try: - from logilab.astng._nodes_ast import * - from logilab.astng._nodes_ast import _const_factory - AST_MODE = '_ast' -except ImportError: - from logilab.astng._nodes_compiler import * - from logilab.astng._nodes_compiler import _const_factory - AST_MODE = 'compiler' +from logilab.astng._nodes import * +from logilab.astng._nodes import _const_factory from logilab.astng._exceptions import UnresolvableName, NotFoundError, \ InferenceError, ASTNGError diff --git a/protocols.py b/protocols.py index 8385c666..7ffe8c2a 100644 --- a/protocols.py +++ b/protocols.py @@ -24,7 +24,7 @@ from __future__ import generators __doctype__ = "restructuredtext en" -from logilab.astng import InferenceError, NoDefault, nodes +from logilab.astng import InferenceError, NoDefault, _nodes as nodes from logilab.astng.infutils import copy_context, unpack_infer, \ raise_if_nothing_infered, yes_if_nothing_infered, Instance, Generator, YES from logilab.astng.nodes import Const, Class, Function, Tuple, List, \ diff --git a/scoped_nodes.py b/scoped_nodes.py index bd70b920..027b9668 100644 --- a/scoped_nodes.py +++ b/scoped_nodes.py @@ -35,7 +35,7 @@ from logilab.common.decorators import cached from logilab.astng import MANAGER, NotFoundError, NoDefault, \ ASTNGBuildingException, InferenceError -from logilab.astng.nodes import Arguments, Class, Const, Function, GenExpr, \ +from logilab.astng._nodes import Arguments, Class, Const, Function, GenExpr, \ From, Lambda, Module, Name, Pass, Raise, Tuple, List, Dict, Yield, \ DelAttr, DelName, const_factory as cf from logilab.astng.utils import extend_class |