From 5ff9e8f6f4d18d721df7cf50b8581f61e6094122 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Sun, 21 Aug 2016 22:09:33 +0300 Subject: Make InterpreterObjects be used as their underlying runtime object, by providing a way to access the attributes of the latter seamlessly. --- astroid/tree/node_classes.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/astroid/tree/node_classes.py b/astroid/tree/node_classes.py index 547ebf31..28cf001a 100644 --- a/astroid/tree/node_classes.py +++ b/astroid/tree/node_classes.py @@ -683,14 +683,23 @@ class Ellipsis(base.NodeNG): # pylint: disable=redefined-builtin def bool_value(self): return True + +_INTERPRETER_OBJECT_SENTINEL = object() + + @util.register_implementation(treeabc.InterpreterObject) class InterpreterObject(base.NodeNG): - '''InterpreterObjects are used in manufactured ASTs that simulate features of - real ASTs for inference, usually to handle behavior implemented in - the interpreter or in C extensions. + '''Used for connecting ASTs and runtime objects + InterpreterObjects are used in manufactured ASTs that simulate features of + real ASTs for inference, usually to handle behavior implemented in + the interpreter or in C extensions. They can be used as a "translator" + from a non-AST object, or in astroid's parlance, a runtime object + to an AST. They mimick their underlying object, which means that an + InterpreterObject can act as the object it is wrapping. ''' _other_fields = ('name', 'object') + object = _INTERPRETER_OBJECT_SENTINEL def __init__(self, object_=None, name=None, lineno=None, col_offset=None, parent=None): if object_ is not None: @@ -699,7 +708,12 @@ class InterpreterObject(base.NodeNG): super(InterpreterObject, self).__init__(lineno, col_offset, parent) def has_underlying_object(self): - return hasattr(self, 'object') + return self.object != _INTERPRETER_OBJECT_SENTINEL + + def __getattr__(self, attr): + if self.has_underlying_object(): + return getattr(self.object, attr) + raise AttributeError(attr) @util.register_implementation(treeabc.ExceptHandler) -- cgit v1.2.1