File tree Expand file tree Collapse file tree 2 files changed +17
-4
lines changed
Expand file tree Collapse file tree 2 files changed +17
-4
lines changed Original file line number Diff line number Diff line change @@ -1088,18 +1088,27 @@ def visit_Name(self, node):
10881088 self .write (node .id )
10891089
10901090 def _write_docstring (self , node ):
1091+ def esc_char (c ):
1092+ if c in ("\n " , "\t " ):
1093+ # In the AST form, we don't know the author's intentation
1094+ # about how this should be displayed. We'll only escape
1095+ # \n and \t, because they are more likely to be unescaped
1096+ # in the source
1097+ return c
1098+ else :
1099+ return c .encode ('unicode_escape' ).decode ('ascii' )
1100+
10911101 self .fill ()
10921102 if node .kind == "u" :
10931103 self .write ("u" )
10941104
10951105 value = node .value
10961106 if value :
10971107 # Preserve quotes in the docstring by escaping them
1098- value = value .replace ("\\ " , "\\ \\ " )
1099- value = value .replace ('"""' , '""\" ' )
1100- value = value .replace ("\r " , "\\ r" )
1108+ value = "" .join (map (esc_char , value ))
11011109 if value [- 1 ] == '"' :
11021110 value = value .replace ('"' , '\\ "' , - 1 )
1111+ value = value .replace ('"""' , '""\\ "' )
11031112
11041113 self .write (f'"""{ value } """' )
11051114
Original file line number Diff line number Diff line change @@ -324,7 +324,11 @@ def test_docstrings(self):
324324 '\\ t' ,
325325 '\n ' ,
326326 '\\ n' ,
327- '\r \\ r\t \\ t\n \\ n'
327+ '\r \\ r\t \\ t\n \\ n' ,
328+ '""">>> content = \" \" \" blabla\" \" \" <<<"""' ,
329+ r'foo\n\x00'
330+ '🐍⛎𩸽üéş^\X\BB\N{LONG RIGHTWARDS SQUIGGLE ARROW} '
331+
328332 )
329333 for docstring in docstrings :
330334 # check as Module docstrings for easy testing
You can’t perform that action at this time.
0 commit comments