MCPcopy
hub / github.com/rocky/python-uncompyle6 / print_docstring

Function print_docstring

uncompyle6/semantics/helper.py:154–219  ·  view source on GitHub ↗
(self, indent, docstring)

Source from the content-addressed store, hash-verified

152
153
154def print_docstring(self, indent, docstring):
155 if isinstance(docstring, bytes):
156 docstring = docstring.decode("utf8", errors="backslashreplace")
157
158 quote = '"""'
159 if docstring.find(quote) >= 0:
160 if docstring.find("'''") == -1:
161 quote = "'''"
162
163 self.write(indent)
164 docstring = repr(docstring.expandtabs())[1:-1]
165
166 for (orig, replace) in (('\\\\', '\t'),
167 ('\\r\\n', '\n'),
168 ('\\n', '\n'),
169 ('\\r', '\n'),
170 ('\\"', '"'),
171 ("\\'", "'")):
172 docstring = docstring.replace(orig, replace)
173
174 # Do a raw string if there are backslashes but no other escaped characters:
175 # also check some edge cases
176 if ('\t' in docstring
177 and '\\' not in docstring
178 and len(docstring) >= 2
179 and docstring[-1] != '\t'
180 and (docstring[-1] != '"'
181 or docstring[-2] == '\t')):
182 self.write('r') # raw string
183 # Restore backslashes unescaped since raw
184 docstring = docstring.replace('\t', '\\')
185 else:
186 # Escape the last character if it is the same as the
187 # triple quote character.
188 quote1 = quote[-1]
189 if len(docstring) and docstring[-1] == quote1:
190 docstring = docstring[:-1] + '\\' + quote1
191
192 # Escape triple quote when needed
193 if quote == '"""':
194 replace_str = '\\"""'
195 else:
196 assert quote == "'''"
197 replace_str = "\\'''"
198
199 docstring = docstring.replace(quote, replace_str)
200 docstring = docstring.replace('\t', '\\\\')
201
202 lines = docstring.split('\n')
203
204 self.write(quote)
205 if len(lines) == 0:
206 self.println(quote)
207 elif len(lines) == 1:
208 self.println(lines[0], quote)
209 else:
210 self.println(lines[0])
211 for line in lines[1:-1]:

Callers 7

build_classMethod · 0.90
code_deparseFunction · 0.90
n_docstringMethod · 0.90
make_function2Function · 0.90
make_function3_annotateFunction · 0.90
make_function3Function · 0.90
make_function1Function · 0.90

Calls 2

writeMethod · 0.45
printlnMethod · 0.45

Tested by

no test coverage detected