Skip to content

Commit

Permalink
Prefer double quotes in string repr
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Feb 18, 2024
1 parent 041175a commit 7f63738
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 10 additions & 0 deletions decompyle3/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,16 @@ def setTokenClass(self, tokenClass: Token) -> Token:
return self.Token


def prefer_double_quote(string: str) -> str:
"""
Prefer a double quoted string over a
single quoted string when possible
"""
if string.find("'") == -1 and not string.startswith("'''"):
return f'"{string[1:-2]}"'
return string


def parse_fn_counts(argc: int):
return ((argc & 0xFF), (argc >> 8) & 0xFF, (argc >> 16) & 0x7FFF)

Expand Down
7 changes: 4 additions & 3 deletions decompyle3/scanners/scanner37base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2015-2023 by Rocky Bernstein
# Copyright (c) 2015-2020, 2022-2024 by Rocky Bernstein
# Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org>
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
#
Expand All @@ -15,7 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Python 37 bytecode scanner/deparser base.
Python 3.7 bytecode scanner/deparser base.
Also we *modify* the instruction sequence to assist deparsing code.
For example:
Expand All @@ -39,7 +39,7 @@
from xdis import Instruction, instruction_size, iscode
from xdis.bytecode import _get_const_info

from decompyle3.scanner import Scanner, Token
from decompyle3.scanner import Scanner, Token, prefer_double_quote

globals().update(op3.opmap)

Expand Down Expand Up @@ -471,6 +471,7 @@ def tokens_append(j, token):
pattr = "<code_object " + const.co_name + ">"
elif isinstance(const, str):
opname = "LOAD_STR"
pattr = prefer_double_quote(inst.argrepr)
else:
if isinstance(inst.arg, int) and inst.arg < len(co.co_consts):
argval, _ = _get_const_info(inst.arg, co.co_consts)
Expand Down

0 comments on commit 7f63738

Please sign in to comment.