Skip to content

Commit 82963cd

Browse files
committed
Preserve docstring in closures..
This change synchronized from decompyle3
1 parent a20972d commit 82963cd

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

uncompyle6/semantics/customize36.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def n_classdef36(node):
170170
class_name = node[1][1].attr
171171
if self.is_pypy and class_name.find("<locals>") > 0:
172172
class_name = class_name.split(".")[-1]
173+
173174
else:
174175
class_name = node[1][2].attr
175176
build_class = node
@@ -206,7 +207,9 @@ def n_classdef36(node):
206207
elif build_class[1][0] == "load_closure":
207208
# Python 3 with closures not functions
208209
load_closure = build_class[1]
209-
if hasattr(load_closure[-3], "attr"):
210+
if load_closure[-4] == "LOAD_CODE":
211+
subclass_code = load_closure[-4].attr
212+
elif hasattr(load_closure[-3], "attr"):
210213
# Python 3.3 classes with closures work like this.
211214
# Note have to test before 3.2 case because
212215
# index -2 also has an attr.
@@ -215,14 +218,18 @@ def n_classdef36(node):
215218
# Python 3.2 works like this
216219
subclass_code = load_closure[-2].attr
217220
else:
218-
raise "Internal Error n_classdef: cannot find class body"
221+
raise RuntimeError(
222+
"Internal Error n_classdef: cannot find " "class body"
223+
)
219224
if hasattr(build_class[3], "__len__"):
220225
if not subclass_info:
221226
subclass_info = build_class[3]
222227
elif hasattr(build_class[2], "__len__"):
223228
subclass_info = build_class[2]
224229
else:
225-
raise "Internal Error n_classdef: cannot superclass name"
230+
raise RuntimeError(
231+
"Internal Error n_classdef: cannot " "superclass name"
232+
)
226233
elif node == "classdefdeco2":
227234
subclass_info = node
228235
subclass_code = build_class[1][0].attr

uncompyle6/semantics/customize37.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019-2022 by Rocky Bernstein
1+
# Copyright (c) 2019-2023 by Rocky Bernstein
22
#
33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -16,12 +16,8 @@
1616
"""
1717

1818
import re
19-
from uncompyle6.semantics.consts import (
20-
PRECEDENCE,
21-
TABLE_DIRECT,
22-
INDENT_PER_LEVEL,
23-
)
2419

20+
from uncompyle6.semantics.consts import INDENT_PER_LEVEL, PRECEDENCE, TABLE_DIRECT
2521
from uncompyle6.semantics.helper import flatten_list
2622

2723
FSTRING_CONVERSION_MAP = {1: "!s", 2: "!r", 3: "!a", "X": ":X"}
@@ -54,10 +50,13 @@ def customize_for_version37(self, version):
5450
{
5551
"and_not": ("%c and not %c", (0, "expr"), (2, "expr")),
5652
"ann_assign": (
57-
"%|%[2]{attr}: %c\n", 0,
53+
"%|%[2]{attr}: %c\n",
54+
0,
5855
),
5956
"ann_assign_init": (
60-
"%|%[2]{attr}: %c = %c\n", 0, 1,
57+
"%|%[2]{attr}: %c = %c\n",
58+
0,
59+
1,
6160
),
6261
"async_for_stmt": (
6362
"%|async for %c in %c:\n%+%c%-\n\n",
@@ -89,9 +88,8 @@ def customize_for_version37(self, version):
8988
"attributes37": (
9089
"%[0]{pattr} import %c",
9190
(0, "IMPORT_NAME_ATTR"),
92-
(1, "IMPORT_FROM")
91+
(1, "IMPORT_FROM"),
9392
),
94-
9593
# nested await expressions like:
9694
# return await (await bar())
9795
# need parenthesis.
@@ -126,19 +124,24 @@ def customize_for_version37(self, version):
126124
(0, PRECEDENCE["compare"] - 1),
127125
(-2, PRECEDENCE["compare"] - 1),
128126
),
129-
"compare_chained2a_37": ('%[1]{pattr.replace("-", " ")} %p', (0, PRECEDENCE["compare"] - 1)),
130-
"compare_chained2b_false_37": ('%[1]{pattr.replace("-", " ")} %p', (0, PRECEDENCE["compare"] - 1)),
131-
"compare_chained2a_false_37": ('%[1]{pattr.replace("-", " ")} %p', (0, PRECEDENCE["compare"] - 1)),
127+
"compare_chained2a_37": (
128+
'%[1]{pattr.replace("-", " ")} %p',
129+
(0, PRECEDENCE["compare"] - 1),
130+
),
131+
"compare_chained2b_false_37": (
132+
'%[1]{pattr.replace("-", " ")} %p',
133+
(0, PRECEDENCE["compare"] - 1),
134+
),
135+
"compare_chained2a_false_37": (
136+
'%[1]{pattr.replace("-", " ")} %p',
137+
(0, PRECEDENCE["compare"] - 1),
138+
),
132139
"compare_chained2c_37": (
133140
'%[3]{pattr.replace("-", " ")} %p %p',
134141
(0, PRECEDENCE["compare"] - 1),
135142
(6, PRECEDENCE["compare"] - 1),
136143
),
137-
'if_exp37': (
138-
'%p if %c else %c',
139-
(1, 'expr', 27), 0, 3
140-
),
141-
144+
"if_exp37": ("%p if %c else %c", (1, "expr", 27), 0, 3),
142145
"except_return": ("%|except:\n%+%c%-", 3),
143146
"if_exp_37a": (
144147
"%p if %p else %p",
@@ -153,9 +156,7 @@ def customize_for_version37(self, version):
153156
(5, "expr", 27),
154157
),
155158
"ifstmtl": ("%|if %c:\n%+%c%-", (0, "testexpr"), (1, "_ifstmts_jumpl")),
156-
'import_as37': (
157-
"%|import %c as %c\n", 2, -2
158-
),
159+
"import_as37": ("%|import %c as %c\n", 2, -2),
159160
"import_from37": ("%|from %[2]{pattr} import %c\n", (3, "importlist37")),
160161
"import_from_as37": (
161162
"%|from %c as %c\n",
@@ -178,12 +179,11 @@ def customize_for_version37(self, version):
178179
(0, "get_aiter"),
179180
(3, "list_iter"),
180181
),
181-
182182
"list_if37": (" if %p%c", (0, 27), 1),
183183
"list_if37_not": (" if not %p%c", (0, 27), 1),
184184
"testfalse_not_or": ("not %c or %c", (0, "expr"), (2, "expr")),
185185
"testfalse_not_and": ("not (%c)", 0),
186-
"testfalsel": ("not %c", (0, "expr")),
186+
"testfalsel": ("not %c", (0, "expr")),
187187
"try_except36": ("%|try:\n%+%c%-%c\n\n", 1, -2),
188188
"tryfinally36": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", (1, "returns"), 3),
189189
"dict_unpack": ("{**%C}", (0, -1, ", **")),

uncompyle6/semantics/transform.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019-2022 by Rocky Bernstein
1+
# Copyright (c) 2019-2023 by Rocky Bernstein
22

33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -119,15 +119,10 @@ def n_mkfunc(self, node):
119119

120120
mkfunc_pattr = node[-1].pattr
121121
if isinstance(mkfunc_pattr, tuple):
122-
assert len(mkfunc_pattr, 4) and isinstance(mkfunc_pattr, int)
123-
is_closure = node[-1].pattr[3] != 0
124-
else:
125-
# FIXME: This is what we had before. It is hoaky and probably wrong.
126-
is_closure = mkfunc_pattr == "closure"
122+
assert len(mkfunc_pattr) == 4 and isinstance(mkfunc_pattr, int)
127123

128124
if (
129-
(not is_closure)
130-
and len(code.co_consts) > 0
125+
len(code.co_consts) > 0
131126
and isinstance(code.co_consts[0], str)
132127
):
133128
docstring_node = SyntaxTree(

0 commit comments

Comments
 (0)