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 | 900747213bfb106eacf650e09ef821b3289df2ac (patch) | |
| tree | d9e2ab572a8384736cb2f55b7b13b928f09b45b5 | |
| parent | bcd6dfe5f98fe27ed54bfbb45410134b6bc85d56 (diff) | |
| download | astroid-git-900747213bfb106eacf650e09ef821b3289df2ac.tar.gz | |
Use printf-style formatting in as_string, in order
to avoid a potential problem with encodings when using .format.
Closes issue #273.
--HG--
branch : 1.4.0
| -rw-r--r-- | ChangeLog | 7 | ||||
| -rw-r--r-- | astroid/as_string.py | 12 | ||||
| -rw-r--r-- | astroid/tests/unittest_regrtest.py | 17 |
3 files changed, 29 insertions, 7 deletions
@@ -1,6 +1,13 @@ 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. + + 2015-11-29 -- 1.4.1 diff --git a/astroid/as_string.py b/astroid/as_string.py index d7786fec..2b07200c 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -287,13 +287,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 3e221529..181c60c8 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) |
