summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--builder.py2
-rw-r--r--scoped_nodes.py5
-rw-r--r--test/unittest_scoped_nodes.py18
3 files changed, 23 insertions, 2 deletions
diff --git a/builder.py b/builder.py
index 7c7bca07..16b41be7 100644
--- a/builder.py
+++ b/builder.py
@@ -175,6 +175,8 @@ class ASTNGBuilder:
# Const, Tuple, ... we may be wrong, may be not, but
# anyway we don't want to pollute builtin's namespace
continue
+ elif infered.is_function:
+ iattrs = infered.instance_attrs
else:
iattrs = infered.locals
except AttributeError:
diff --git a/scoped_nodes.py b/scoped_nodes.py
index 527fea96..64057318 100644
--- a/scoped_nodes.py
+++ b/scoped_nodes.py
@@ -521,6 +521,7 @@ class Function(StmtMixIn, Lambda):
self.name = name
self.doc = doc
self.extra_decorators = []
+ self.instance_attrs = {}
def set_line_info(self, lastchild):
self.fromlineno = self.lineno
@@ -543,6 +544,8 @@ class Function(StmtMixIn, Lambda):
"""
if name == '__module__':
return [cf(self.root().qname())]
+ if name in self.instance_attrs:
+ return self.instance_attrs[name]
return std_special_attributes(self, name, False)
def is_method(self):
@@ -669,10 +672,8 @@ class Class(StmtMixIn, LocalsDictNodeNG, FilterStmtsMixin):
_astng_fields = ('decorators', 'bases', 'body') # name
decorators = None
- instance_attrs = None
special_attributes = set(('__name__', '__doc__', '__dict__', '__module__',
'__bases__', '__mro__', '__subclasses__'))
-
blockstart_tolineno = None
_type = None
diff --git a/test/unittest_scoped_nodes.py b/test/unittest_scoped_nodes.py
index 215729e8..ae6ef5ce 100644
--- a/test/unittest_scoped_nodes.py
+++ b/test/unittest_scoped_nodes.py
@@ -286,6 +286,24 @@ a = func()
self.assertIsInstance(func_vals[0], nodes.Const)
self.assertEqual(func_vals[0].value, None)
+ def test_func_instance_attr(self):
+ """test instance attributes for functions"""
+ data= """
+def test():
+ print test.bar
+
+test.bar = 1
+test()
+ """
+ astng = abuilder.string_build(data, 'mod', __file__)
+ func = astng.body[2].value.func.infered()[0]
+ self.assertIsInstance(func, nodes.Function)
+ self.assertEqual(func.name, 'test')
+ one = func.getattr('bar')[0].infered()[0]
+ self.assertIsInstance(one, nodes.Const)
+ self.assertEqual(one.value, 1)
+
+
class ClassNodeTC(TestCase):
def test_dict_interface(self):