Prefer using double quote for strings

This commit is contained in:
rocky 2024-02-17 12:57:48 -05:00
parent 60ca6f485b
commit 3ef4ab4944
4 changed files with 12 additions and 19 deletions

View File

@ -597,25 +597,14 @@ class Scanner:
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:

View File

@ -44,8 +44,9 @@ import xdis
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
@ -611,6 +612,7 @@ class Scanner3(Scanner):
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)

View File

@ -39,7 +39,7 @@ import xdis.opcodes.opcode_37 as op3
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)
@ -386,6 +386,7 @@ class Scanner37Base(Scanner):
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)

View File

@ -39,6 +39,7 @@ class NonterminalActions:
# 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):