summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain Thénault <sylvain.thenault@logilab.fr>2011-09-07 10:23:10 +0200
committerSylvain Thénault <sylvain.thenault@logilab.fr>2011-09-07 10:23:10 +0200
commite44eee868fab3b72f100da3aeae4338215a9b5a0 (patch)
tree1890aedc930714372518fbc573d04f5664080d0f
parent7d24bf13562c03eace54dd0414abcbbe61675d97 (diff)
downloadastroid-git-e44eee868fab3b72f100da3aeae4338215a9b5a0.tar.gz
closes #74748: getitem protocal should return Const node, not the bare python value
This one is cute. It's a fix for a crash that occurs on code like "any string literal"[0].upper() due to the fact that the extracted string literal is not properly wrapped, but returned as a naked string object. (patch by google)
-rw-r--r--ChangeLog7
-rw-r--r--node_classes.py2
-rw-r--r--test/unittest_inference.py4
3 files changed, 11 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index 36d8f72e..8b9aa2a0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,7 +2,12 @@ Change log for the astng package
================================
--
- * #74746: should return empty module when __main__ is imported (patch by google)
+ * #74746: should return empty module when __main__ is imported (patch by
+ google)
+
+ * #74748: getitem protocal return constant value instead of a Const node
+ (patch by google)
+
2011-07-18 -- 0.22.0
* added column offset information on nodes (patch by fawce)
diff --git a/node_classes.py b/node_classes.py
index 4dd8520c..607ad907 100644
--- a/node_classes.py
+++ b/node_classes.py
@@ -435,7 +435,7 @@ class Const(NodeNG, Instance):
def getitem(self, index, context=None):
if isinstance(self.value, basestring):
- return self.value[index]
+ return Const(self.value[index])
raise TypeError('%r (value=%s)' % (self, self.value))
def has_dynamic_getattr(self):
diff --git a/test/unittest_inference.py b/test/unittest_inference.py
index a1483a18..5eca958f 100644
--- a/test/unittest_inference.py
+++ b/test/unittest_inference.py
@@ -397,6 +397,7 @@ l = [1]
t = (2,)
d = {}
s = ''
+s2 = '_'
'''
astng = builder.string_build(code, __name__, __file__)
n = astng['l']
@@ -427,6 +428,9 @@ s = ''
self.assertIsInstance(infered, Instance)
self.failUnlessEqual(infered.name, 'str')
self.failUnless('lower' in infered._proxied.locals)
+ n = astng['s2']
+ infered = n.infer().next()
+ self.failUnlessEqual(infered.getitem(0).value, '_')
def test_unicode_type(self):
if sys.version_info >= (3, 0):