diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2015-12-05 13:25:34 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2015-12-05 13:25:34 +0200 |
commit | cf052851cab3176fc6442114cbdc8194b24b5b2e (patch) | |
tree | 80f8e86b479cf9a87f344054fcac70183f519551 /astroid/node_classes.py | |
parent | 9c21c9d3846b50d8fa4e612ed695b53dd43520dd (diff) | |
download | astroid-cf052851cab3176fc6442114cbdc8194b24b5b2e.tar.gz |
assigned_stmts methods have the same signature from now on.
They used to have different signatures and each one made
assumptions about what could be passed to other implementations,
leading to various possible crashes when one or more arguments
weren't given. Closes issue #277.
Diffstat (limited to 'astroid/node_classes.py')
-rw-r--r-- | astroid/node_classes.py | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/astroid/node_classes.py b/astroid/node_classes.py index d248ae9..b004785 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -1847,6 +1847,27 @@ def _update_const_classes(): _update_const_classes() +def _two_step_initialization(cls, value): + instance = cls() + instance.postinit(value) + return instance + + +def _dict_initialization(cls, value): + if isinstance(value, dict): + value = tuple(value.items()) + return _two_step_initialization(cls, value) + + +_CONST_CLS_CONSTRUCTORS = { + List: _two_step_initialization, + Tuple: _two_step_initialization, + Dict: _dict_initialization, + Set: _two_step_initialization, + Const: lambda cls, value: cls(value) +} + + def const_factory(value): """return an astroid node for a python value""" # XXX we should probably be stricter here and only consider stuff in @@ -1856,8 +1877,10 @@ def const_factory(value): # not in CONST_CLS) assert not isinstance(value, NodeNG) try: - return CONST_CLS[value.__class__](value) - except (KeyError, AttributeError): + initializer_cls = CONST_CLS[value.__class__] + initializer = _CONST_CLS_CONSTRUCTORS[initializer_cls] + return initializer(initializer_cls, value) + except (KeyError, AttributeError) as exc: node = EmptyNode() node.object = value return node |