diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2015-12-01 18:49:47 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2015-12-01 18:49:47 +0200 |
commit | 2466e41279a9c54228df79baff694b38438b620d (patch) | |
tree | 8787105b275354b9228bf1d8d2854d26706c3383 /astroid | |
parent | c486ba77ea44edc119e723286e26158a65436037 (diff) | |
download | astroid-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.
Diffstat (limited to 'astroid')
-rw-r--r-- | astroid/as_string.py | 12 | ||||
-rw-r--r-- | astroid/tests/unittest_regrtest.py | 17 |
2 files changed, 22 insertions, 7 deletions
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) |