summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2015-12-01 18:49:47 +0200
committerClaudiu Popa <pcmanticore@gmail.com>2015-12-01 18:49:47 +0200
commit2466e41279a9c54228df79baff694b38438b620d (patch)
tree8787105b275354b9228bf1d8d2854d26706c3383
parentc486ba77ea44edc119e723286e26158a65436037 (diff)
downloadastroid-2466e41279a9c54228df79baff694b38438b620d.tar.gz
Use printf-style formatting in as_string, in order
to avoid a potential problem with encodings when using .format. Closes issue #273.
-rw-r--r--ChangeLog17
-rw-r--r--astroid/as_string.py12
-rw-r--r--astroid/tests/unittest_regrtest.py17
3 files changed, 38 insertions, 8 deletions
diff --git a/ChangeLog b/ChangeLog
index 6b93e83..0da0bcf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,7 +1,22 @@
Change log for the astroid package (used to be astng)
=====================================================
---
+ * Revert to using printf-style formatting in as_string, in order
+ to avoid a potential problem with encodings when using .format.
+ Closes issue #273.
+
+ * Add support for handling Uninferable nodes when calling as_string
+
+ Some object, for instance List or Tuple can have, after inference,
+ Uninferable as their elements, happening when their components
+ weren't couldn't be inferred properly. This means that as_string
+ needs to cope with expecting Uninferable nodes part of the other
+ nodes coming for a string transformation. The patch adds a visit
+ method in AsString and ``accept`` on Yes / Uninferable nodes.
+ Closes issue #270.
+
+
+2015-11-29 -- 1.4.0
* Class.getattr('__mro__') returns the actual MRO. Closes issue #128.
diff --git a/astroid/as_string.py b/astroid/as_string.py
index b2b2c95..5bd6233 100644
--- a/astroid/as_string.py
+++ b/astroid/as_string.py
@@ -245,13 +245,11 @@ class AsStringVisitor(object):
trailer = return_annotation + ":"
else:
trailer = ":"
- def_format = "\n{decorators}def {name}({args}){trailer}{docs}\n{body}"
- return def_format.format(decorators=decorate,
- name=node.name,
- args=node.args.accept(self),
- trailer=trailer,
- docs=docs,
- body=self._stmt_list(node.body))
+ def_format = "\n%sdef %s(%s)%s%s\n%s"
+ return def_format % (decorate, node.name,
+ node.args.accept(self),
+ trailer, docs,
+ self._stmt_list(node.body))
def visit_generatorexp(self, node):
"""return an astroid.GeneratorExp node as string"""
diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py
index 8e60e8a..e409172 100644
--- a/astroid/tests/unittest_regrtest.py
+++ b/astroid/tests/unittest_regrtest.py
@@ -282,6 +282,23 @@ def test():
''')
self.assertRaises(exceptions.InferenceError, next, node.infer())
+ def test_unicode_in_docstring(self):
+ # Crashed for astroid==1.4.1
+ # Test for https://bitbucket.org/logilab/astroid/issues/273/
+
+ # In a regular file, "coding: utf-8" would have been used.
+ node = extract_node(u'''
+ from __future__ import unicode_literals
+
+ class MyClass(object):
+ def method(self):
+ "With unicode : %s "
+
+ instance = MyClass()
+ ''' % u"\u2019")
+
+ next(node.value.infer()).as_string()
+
class Whatever(object):
a = property(lambda x: x, lambda x: x)