Skip to content

Commit 27c869b

Browse files
authored
Merge pull request rocky#502 from gdesmar/docstring_bytes
Fix for print_docstring()'s `docstring.find(quote)` Type error
2 parents 193c262 + 7db6a27 commit 27c869b

File tree

6 files changed

+95
-65
lines changed

6 files changed

+95
-65
lines changed
1.69 KB
Binary file not shown.
1.21 KB
Binary file not shown.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""Module docstring"""
2+
class A:
3+
b"""Got \xe7\xfe Bytes?"""
4+
assert __doc__ == b"""Got \xe7\xfe Bytes?"""
5+
6+
def class_func(self):
7+
b"""Got \xe7\xfe Bytes?"""
8+
assert __doc__ == """Module docstring"""
9+
10+
class B:
11+
"""Got no Bytes?"""
12+
assert __doc__ == """Got no Bytes?"""
13+
14+
def class_func(self):
15+
"""Got no Bytes?"""
16+
assert __doc__ == """Module docstring"""
17+
18+
def single_func():
19+
"""single docstring?"""
20+
assert __doc__ == """Module docstring"""
21+
22+
def single_byte_func():
23+
b"""Got \xe7\xfe Bytes?"""
24+
assert __doc__ == """Module docstring"""
25+
26+
assert __doc__ == """Module docstring"""
27+
28+
assert single_func.__doc__ == """single docstring?"""
29+
single_func()
30+
31+
assert single_byte_func.__doc__ == b"""Got \xe7\xfe Bytes?"""
32+
single_byte_func()
33+
34+
assert A.__doc__ == b"""Got \xe7\xfe Bytes?"""
35+
assert A.class_func.__doc__ == b"""Got \xe7\xfe Bytes?"""
36+
a = A()
37+
assert a.class_func.__doc__ == b"""Got \xe7\xfe Bytes?"""
38+
a.class_func()
39+
40+
assert B.__doc__ == """Got no Bytes?"""
41+
assert B.class_func.__doc__ == """Got no Bytes?"""
42+
b = B()
43+
assert b.class_func.__doc__ == """Got no Bytes?"""
44+
b.class_func()
45+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""Module docstring"""
2+
class A:
3+
b"""Got \xe7\xfe Bytes?"""
4+
assert __doc__ == """Module docstring"""
5+
6+
def class_func(self):
7+
b"""Got \xe7\xfe Bytes?"""
8+
assert __doc__ == """Module docstring"""
9+
10+
class B:
11+
"""Got no Bytes?"""
12+
assert __doc__ == """Got no Bytes?"""
13+
14+
def class_func(self):
15+
"""Got no Bytes?"""
16+
assert __doc__ == """Module docstring"""
17+
18+
def single_func():
19+
"""single docstring?"""
20+
assert __doc__ == """Module docstring"""
21+
22+
def single_byte_func():
23+
b"""Got \xe7\xfe Bytes?"""
24+
assert __doc__ == """Module docstring"""
25+
26+
assert __doc__ == """Module docstring"""
27+
28+
assert single_func.__doc__ == """single docstring?"""
29+
single_func()
30+
31+
assert single_byte_func.__doc__ is None
32+
single_byte_func()
33+
34+
assert A.__doc__ is None
35+
assert A.class_func.__doc__ is None
36+
a = A()
37+
assert a.class_func.__doc__ is None
38+
a.class_func()
39+
40+
assert B.__doc__ == """Got no Bytes?"""
41+
assert B.class_func.__doc__ == """Got no Bytes?"""
42+
b = B()
43+
assert b.class_func.__doc__ == """Got no Bytes?"""
44+
b.class_func()
45+

uncompyle6/semantics/helper.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ def is_lambda_mode(compile_mode: str) -> bool:
152152

153153

154154
def print_docstring(self, indent, docstring):
155+
if isinstance(docstring, bytes):
156+
docstring = docstring.decode("utf8", errors="backslashreplace")
157+
155158
quote = '"""'
156159
if docstring.find(quote) >= 0:
157160
if docstring.find("'''") == -1:

uncompyle6/semantics/n_actions.py

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
PRECEDENCE,
2727
minint,
2828
)
29-
from uncompyle6.semantics.helper import find_code_node, flatten_list
29+
from uncompyle6.semantics.helper import find_code_node, flatten_list, print_docstring
3030
from uncompyle6.util import better_repr, get_code_name
3131

3232

@@ -541,70 +541,7 @@ def n_docstring(self, node):
541541
else:
542542
docstring = node[0].pattr
543543

544-
quote = '"""'
545-
if docstring.find(quote) >= 0:
546-
if docstring.find("'''") == -1:
547-
quote = "'''"
548-
549-
self.write(indent)
550-
docstring = repr(docstring.expandtabs())[1:-1]
551-
552-
for orig, replace in (
553-
("\\\\", "\t"),
554-
("\\r\\n", "\n"),
555-
("\\n", "\n"),
556-
("\\r", "\n"),
557-
('\\"', '"'),
558-
("\\'", "'"),
559-
):
560-
docstring = docstring.replace(orig, replace)
561-
562-
# Do a raw string if there are backslashes but no other escaped characters:
563-
# also check some edge cases
564-
if (
565-
"\t" in docstring
566-
and "\\" not in docstring
567-
and len(docstring) >= 2
568-
and docstring[-1] != "\t"
569-
and (docstring[-1] != '"' or docstring[-2] == "\t")
570-
):
571-
self.write("r") # raw string
572-
# Restore backslashes unescaped since raw
573-
docstring = docstring.replace("\t", "\\")
574-
else:
575-
# Escape the last character if it is the same as the
576-
# triple quote character.
577-
quote1 = quote[-1]
578-
if len(docstring) and docstring[-1] == quote1:
579-
docstring = docstring[:-1] + "\\" + quote1
580-
581-
# Escape triple quote when needed
582-
if quote == '"""':
583-
replace_str = '\\"""'
584-
else:
585-
assert quote == "'''"
586-
replace_str = "\\'''"
587-
588-
docstring = docstring.replace(quote, replace_str)
589-
docstring = docstring.replace("\t", "\\\\")
590-
591-
lines = docstring.split("\n")
592-
593-
self.write(quote)
594-
if len(lines) == 0:
595-
self.println(quote)
596-
elif len(lines) == 1:
597-
self.println(lines[0], quote)
598-
else:
599-
self.println(lines[0])
600-
for line in lines[1:-1]:
601-
if line:
602-
self.println(line)
603-
else:
604-
self.println("\n\n")
605-
pass
606-
pass
607-
self.println(lines[-1], quote)
544+
print_docstring(self, indent, docstring)
608545
self.prune()
609546

610547
def n_elifelsestmtr(self, node: SyntaxTree):

0 commit comments

Comments
 (0)