Skip to content

Commit

Permalink
Prefer using double quote for strings
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Feb 17, 2024
1 parent 60ca6f4 commit 3ef4ab4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 19 deletions.
23 changes: 6 additions & 17 deletions uncompyle6/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,25 +597,14 @@ def setTokenClass(self, tokenClass: Token) -> Token:
return self.Token


# TODO: after the next xdis release, use from there instead.
def parse_fn_counts_30_35(argc: int) -> Tuple[int, int, int]:
def prefer_double_quote(string: str) -> str:
"""
In Python 3.0 to 3.5 MAKE_CLOSURE and MAKE_FUNCTION encode
arguments counts of positional, default + named, and annotation
arguments a particular kind of encoding where each of
the entry a a packed byted value of the lower 24 bits
of ``argc``. The high bits of argc may have come from
an EXTENDED_ARG instruction. Here, we unpack the values
from the ``argc`` int and return a triple of the
positional args, named_args, and annotation args.
Prefer a double quoted string over a
single quoted string when possible
"""
annotate_count = (argc >> 16) & 0x7FFF
# For some reason that I don't understand, annotate_args is off by one
# when there is an EXENDED_ARG instruction from what is documented in
# https://docs.python.org/3.4/library/dis.html#opcode-MAKE_CLOSURE
if annotate_count > 1:
annotate_count -= 1
return ((argc & 0xFF), (argc >> 8) & 0xFF, annotate_count)
if string.find("'") == -1:
return f'"{string}"'
return str(string)


def get_scanner(version: Union[str, tuple], is_pypy=False, show_asm=None) -> Scanner:
Expand Down
4 changes: 3 additions & 1 deletion uncompyle6/scanners/scanner3.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
import xdis.opcodes.opcode_33 as op3
from xdis import Instruction, instruction_size, iscode
from xdis.bytecode import _get_const_info
from xdis.opcodes.opcode_3x import parse_fn_counts_30_35

from uncompyle6.scanner import CONST_COLLECTIONS, Scanner, parse_fn_counts_30_35
from uncompyle6.scanner import CONST_COLLECTIONS, Scanner, prefer_double_quote
from uncompyle6.scanners.tok import Token
from uncompyle6.util import get_code_name

Expand Down Expand Up @@ -611,6 +612,7 @@ def ingest(
pattr = "<code_object " + co_name + ">"
elif isinstance(const, str):
opname = "LOAD_STR"
pattr = prefer_double_quote(inst.argval)
else:
if isinstance(inst.arg, int) and inst.arg < len(co.co_consts):
argval, _ = _get_const_info(inst.arg, co.co_consts)
Expand Down
3 changes: 2 additions & 1 deletion uncompyle6/scanners/scanner37base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from xdis import Instruction, instruction_size, iscode
from xdis.bytecode import _get_const_info

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

globals().update(op3.opmap)

Expand Down Expand Up @@ -386,6 +386,7 @@ def tokens_append(j, token):
pattr = "<code_object " + const.co_name + ">"
elif isinstance(const, str):
opname = "LOAD_STR"
pattr = prefer_double_quote(inst.argval)
else:
if isinstance(inst.arg, int) and inst.arg < len(co.co_consts):
argval, _ = _get_const_info(inst.arg, co.co_consts)
Expand Down
1 change: 1 addition & 0 deletions uncompyle6/semantics/n_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self):
# parenthesis surrounding it. A high value indicates no
# parenthesis are needed.
self.prec = 1000
self.in_format_string = False

def n_alias(self, node):
if self.version <= (2, 1):
Expand Down

0 comments on commit 3ef4ab4

Please sign in to comment.