From 27f288b9861f7648de059f88264afc8291590269 Mon Sep 17 00:00:00 2001 From: Bryce Guinta Date: Thu, 21 Jun 2018 11:54:01 -0600 Subject: Prevent Const copy resulting in recursion Calling object.__new__ on a Const node would result in infinite recursion Fix infinite recursion by explicitly raising AttributeError if __getattr__ is called for value See https://nedbatchelder.com/blog/201010/surprising_getattr_recursion.html for more details Close #565 --- astroid/node_classes.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'astroid/node_classes.py') diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 2236721e..36003fff 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -2300,6 +2300,17 @@ class Const(mixins.NoChildrenMixin, NodeNG, bases.Instance): super(Const, self).__init__(lineno, col_offset, parent) + def __getattr__(self, name): + # This is needed because of Proxy's __getattr__ method. + # Calling object.__new__ on this class without calling + # __init__ would result in an infinite loop otherwise + # since __getattr__ is called when an attribute doesn't + # exist and self._proxied indirectly calls self.value + # and Proxy __getattr__ calls self.value + if name == "value": + raise AttributeError + return super().__getattr__(name) + def getitem(self, index, context=None): """Get an item from this node if subscriptable. -- cgit v1.2.1