You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 09:22:40 +08:00
Preserve docstring in closures..
This change synchronized from decompyle3
This commit is contained in:
@@ -170,6 +170,7 @@ def customize_for_version36(self, version):
|
|||||||
class_name = node[1][1].attr
|
class_name = node[1][1].attr
|
||||||
if self.is_pypy and class_name.find("<locals>") > 0:
|
if self.is_pypy and class_name.find("<locals>") > 0:
|
||||||
class_name = class_name.split(".")[-1]
|
class_name = class_name.split(".")[-1]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
class_name = node[1][2].attr
|
class_name = node[1][2].attr
|
||||||
build_class = node
|
build_class = node
|
||||||
@@ -206,7 +207,9 @@ def customize_for_version36(self, version):
|
|||||||
elif build_class[1][0] == "load_closure":
|
elif build_class[1][0] == "load_closure":
|
||||||
# Python 3 with closures not functions
|
# Python 3 with closures not functions
|
||||||
load_closure = build_class[1]
|
load_closure = build_class[1]
|
||||||
if hasattr(load_closure[-3], "attr"):
|
if load_closure[-4] == "LOAD_CODE":
|
||||||
|
subclass_code = load_closure[-4].attr
|
||||||
|
elif hasattr(load_closure[-3], "attr"):
|
||||||
# Python 3.3 classes with closures work like this.
|
# Python 3.3 classes with closures work like this.
|
||||||
# Note have to test before 3.2 case because
|
# Note have to test before 3.2 case because
|
||||||
# index -2 also has an attr.
|
# index -2 also has an attr.
|
||||||
@@ -215,14 +218,18 @@ def customize_for_version36(self, version):
|
|||||||
# Python 3.2 works like this
|
# Python 3.2 works like this
|
||||||
subclass_code = load_closure[-2].attr
|
subclass_code = load_closure[-2].attr
|
||||||
else:
|
else:
|
||||||
raise "Internal Error n_classdef: cannot find class body"
|
raise RuntimeError(
|
||||||
|
"Internal Error n_classdef: cannot find " "class body"
|
||||||
|
)
|
||||||
if hasattr(build_class[3], "__len__"):
|
if hasattr(build_class[3], "__len__"):
|
||||||
if not subclass_info:
|
if not subclass_info:
|
||||||
subclass_info = build_class[3]
|
subclass_info = build_class[3]
|
||||||
elif hasattr(build_class[2], "__len__"):
|
elif hasattr(build_class[2], "__len__"):
|
||||||
subclass_info = build_class[2]
|
subclass_info = build_class[2]
|
||||||
else:
|
else:
|
||||||
raise "Internal Error n_classdef: cannot superclass name"
|
raise RuntimeError(
|
||||||
|
"Internal Error n_classdef: cannot " "superclass name"
|
||||||
|
)
|
||||||
elif node == "classdefdeco2":
|
elif node == "classdefdeco2":
|
||||||
subclass_info = node
|
subclass_info = node
|
||||||
subclass_code = build_class[1][0].attr
|
subclass_code = build_class[1][0].attr
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
# Copyright (c) 2019-2022 by Rocky Bernstein
|
# Copyright (c) 2019-2023 by Rocky Bernstein
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
@@ -16,12 +16,8 @@
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
from uncompyle6.semantics.consts import (
|
|
||||||
PRECEDENCE,
|
|
||||||
TABLE_DIRECT,
|
|
||||||
INDENT_PER_LEVEL,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
from uncompyle6.semantics.consts import INDENT_PER_LEVEL, PRECEDENCE, TABLE_DIRECT
|
||||||
from uncompyle6.semantics.helper import flatten_list
|
from uncompyle6.semantics.helper import flatten_list
|
||||||
|
|
||||||
FSTRING_CONVERSION_MAP = {1: "!s", 2: "!r", 3: "!a", "X": ":X"}
|
FSTRING_CONVERSION_MAP = {1: "!s", 2: "!r", 3: "!a", "X": ":X"}
|
||||||
@@ -54,10 +50,13 @@ def customize_for_version37(self, version):
|
|||||||
{
|
{
|
||||||
"and_not": ("%c and not %c", (0, "expr"), (2, "expr")),
|
"and_not": ("%c and not %c", (0, "expr"), (2, "expr")),
|
||||||
"ann_assign": (
|
"ann_assign": (
|
||||||
"%|%[2]{attr}: %c\n", 0,
|
"%|%[2]{attr}: %c\n",
|
||||||
|
0,
|
||||||
),
|
),
|
||||||
"ann_assign_init": (
|
"ann_assign_init": (
|
||||||
"%|%[2]{attr}: %c = %c\n", 0, 1,
|
"%|%[2]{attr}: %c = %c\n",
|
||||||
|
0,
|
||||||
|
1,
|
||||||
),
|
),
|
||||||
"async_for_stmt": (
|
"async_for_stmt": (
|
||||||
"%|async for %c in %c:\n%+%c%-\n\n",
|
"%|async for %c in %c:\n%+%c%-\n\n",
|
||||||
@@ -89,9 +88,8 @@ def customize_for_version37(self, version):
|
|||||||
"attributes37": (
|
"attributes37": (
|
||||||
"%[0]{pattr} import %c",
|
"%[0]{pattr} import %c",
|
||||||
(0, "IMPORT_NAME_ATTR"),
|
(0, "IMPORT_NAME_ATTR"),
|
||||||
(1, "IMPORT_FROM")
|
(1, "IMPORT_FROM"),
|
||||||
),
|
),
|
||||||
|
|
||||||
# nested await expressions like:
|
# nested await expressions like:
|
||||||
# return await (await bar())
|
# return await (await bar())
|
||||||
# need parenthesis.
|
# need parenthesis.
|
||||||
@@ -126,19 +124,24 @@ def customize_for_version37(self, version):
|
|||||||
(0, PRECEDENCE["compare"] - 1),
|
(0, PRECEDENCE["compare"] - 1),
|
||||||
(-2, PRECEDENCE["compare"] - 1),
|
(-2, PRECEDENCE["compare"] - 1),
|
||||||
),
|
),
|
||||||
"compare_chained2a_37": ('%[1]{pattr.replace("-", " ")} %p', (0, PRECEDENCE["compare"] - 1)),
|
"compare_chained2a_37": (
|
||||||
"compare_chained2b_false_37": ('%[1]{pattr.replace("-", " ")} %p', (0, PRECEDENCE["compare"] - 1)),
|
'%[1]{pattr.replace("-", " ")} %p',
|
||||||
"compare_chained2a_false_37": ('%[1]{pattr.replace("-", " ")} %p', (0, PRECEDENCE["compare"] - 1)),
|
(0, PRECEDENCE["compare"] - 1),
|
||||||
|
),
|
||||||
|
"compare_chained2b_false_37": (
|
||||||
|
'%[1]{pattr.replace("-", " ")} %p',
|
||||||
|
(0, PRECEDENCE["compare"] - 1),
|
||||||
|
),
|
||||||
|
"compare_chained2a_false_37": (
|
||||||
|
'%[1]{pattr.replace("-", " ")} %p',
|
||||||
|
(0, PRECEDENCE["compare"] - 1),
|
||||||
|
),
|
||||||
"compare_chained2c_37": (
|
"compare_chained2c_37": (
|
||||||
'%[3]{pattr.replace("-", " ")} %p %p',
|
'%[3]{pattr.replace("-", " ")} %p %p',
|
||||||
(0, PRECEDENCE["compare"] - 1),
|
(0, PRECEDENCE["compare"] - 1),
|
||||||
(6, PRECEDENCE["compare"] - 1),
|
(6, PRECEDENCE["compare"] - 1),
|
||||||
),
|
),
|
||||||
'if_exp37': (
|
"if_exp37": ("%p if %c else %c", (1, "expr", 27), 0, 3),
|
||||||
'%p if %c else %c',
|
|
||||||
(1, 'expr', 27), 0, 3
|
|
||||||
),
|
|
||||||
|
|
||||||
"except_return": ("%|except:\n%+%c%-", 3),
|
"except_return": ("%|except:\n%+%c%-", 3),
|
||||||
"if_exp_37a": (
|
"if_exp_37a": (
|
||||||
"%p if %p else %p",
|
"%p if %p else %p",
|
||||||
@@ -153,9 +156,7 @@ def customize_for_version37(self, version):
|
|||||||
(5, "expr", 27),
|
(5, "expr", 27),
|
||||||
),
|
),
|
||||||
"ifstmtl": ("%|if %c:\n%+%c%-", (0, "testexpr"), (1, "_ifstmts_jumpl")),
|
"ifstmtl": ("%|if %c:\n%+%c%-", (0, "testexpr"), (1, "_ifstmts_jumpl")),
|
||||||
'import_as37': (
|
"import_as37": ("%|import %c as %c\n", 2, -2),
|
||||||
"%|import %c as %c\n", 2, -2
|
|
||||||
),
|
|
||||||
"import_from37": ("%|from %[2]{pattr} import %c\n", (3, "importlist37")),
|
"import_from37": ("%|from %[2]{pattr} import %c\n", (3, "importlist37")),
|
||||||
"import_from_as37": (
|
"import_from_as37": (
|
||||||
"%|from %c as %c\n",
|
"%|from %c as %c\n",
|
||||||
@@ -178,12 +179,11 @@ def customize_for_version37(self, version):
|
|||||||
(0, "get_aiter"),
|
(0, "get_aiter"),
|
||||||
(3, "list_iter"),
|
(3, "list_iter"),
|
||||||
),
|
),
|
||||||
|
|
||||||
"list_if37": (" if %p%c", (0, 27), 1),
|
"list_if37": (" if %p%c", (0, 27), 1),
|
||||||
"list_if37_not": (" if not %p%c", (0, 27), 1),
|
"list_if37_not": (" if not %p%c", (0, 27), 1),
|
||||||
"testfalse_not_or": ("not %c or %c", (0, "expr"), (2, "expr")),
|
"testfalse_not_or": ("not %c or %c", (0, "expr"), (2, "expr")),
|
||||||
"testfalse_not_and": ("not (%c)", 0),
|
"testfalse_not_and": ("not (%c)", 0),
|
||||||
"testfalsel": ("not %c", (0, "expr")),
|
"testfalsel": ("not %c", (0, "expr")),
|
||||||
"try_except36": ("%|try:\n%+%c%-%c\n\n", 1, -2),
|
"try_except36": ("%|try:\n%+%c%-%c\n\n", 1, -2),
|
||||||
"tryfinally36": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", (1, "returns"), 3),
|
"tryfinally36": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", (1, "returns"), 3),
|
||||||
"dict_unpack": ("{**%C}", (0, -1, ", **")),
|
"dict_unpack": ("{**%C}", (0, -1, ", **")),
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
# Copyright (c) 2019-2022 by Rocky Bernstein
|
# Copyright (c) 2019-2023 by Rocky Bernstein
|
||||||
|
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
@@ -119,15 +119,10 @@ class TreeTransform(GenericASTTraversal, object):
|
|||||||
|
|
||||||
mkfunc_pattr = node[-1].pattr
|
mkfunc_pattr = node[-1].pattr
|
||||||
if isinstance(mkfunc_pattr, tuple):
|
if isinstance(mkfunc_pattr, tuple):
|
||||||
assert len(mkfunc_pattr, 4) and isinstance(mkfunc_pattr, int)
|
assert len(mkfunc_pattr) == 4 and isinstance(mkfunc_pattr, int)
|
||||||
is_closure = node[-1].pattr[3] != 0
|
|
||||||
else:
|
|
||||||
# FIXME: This is what we had before. It is hoaky and probably wrong.
|
|
||||||
is_closure = mkfunc_pattr == "closure"
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(not is_closure)
|
len(code.co_consts) > 0
|
||||||
and len(code.co_consts) > 0
|
|
||||||
and isinstance(code.co_consts[0], str)
|
and isinstance(code.co_consts[0], str)
|
||||||
):
|
):
|
||||||
docstring_node = SyntaxTree(
|
docstring_node = SyntaxTree(
|
||||||
|
Reference in New Issue
Block a user