summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2016-08-21 22:09:33 +0300
committerClaudiu Popa <pcmanticore@gmail.com>2016-08-21 22:09:33 +0300
commit5ff9e8f6f4d18d721df7cf50b8581f61e6094122 (patch)
tree71a9161f86497d4c0979362356fc70f23879809f
parentec6e7e002b1d86682cda793f9048ef9c4133d844 (diff)
downloadastroid-git-5ff9e8f6f4d18d721df7cf50b8581f61e6094122.tar.gz
Make InterpreterObjects be used as their underlying runtime object, by providing a way to access the attributes of the latter seamlessly.
-rw-r--r--astroid/tree/node_classes.py22
1 files 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)