Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down