From 076723f7fc95a91780909f1bc101a835a643b346 Mon Sep 17 00:00:00 2001 From: Matthew Schinckel Date: Thu, 24 Apr 2014 22:56:49 +0930 Subject: [PATCH] Write multi-line-strings using the triple-quoted syntax We only do this if we have a '>>>' in the string, as that probably means it's a doctest, and that module will only pick up tests correctly if they do use the triple-quoted syntax. --- codegen.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/codegen.py b/codegen.py index 113d9be..9840454 100644 --- a/codegen.py +++ b/codegen.py @@ -397,7 +397,24 @@ def visit_Name(self, node): self.write(node.id) def visit_Str(self, node): - self.write(repr(node.s)) + # If we have newlines, and '>>>' it means we probably have a doctest. + # We need to write back triple-quoted strings in that case, and not + # a repr(node.s) + if '\n' in node.s and '>>>' in node.s: + if '"""' not in node.s: + self.write('"""') + self.write(node.s) + self.write('"""') + elif "'''" not in node.s: + self.write("'''") + self.write(node.s) + self.write("'''") + else: + # Seriously, you wrote a string with both ''' and """ in it? + # I have no idea what to do now! + self.write(repr(node.s)) + else: + self.write(repr(node.s)) def visit_Bytes(self, node): self.write(repr(node.s))