Skip to content

Commit 3ef4ab4

Browse files
committed
Prefer using double quote for strings
1 parent 60ca6f4 commit 3ef4ab4

File tree

4 files changed

+12
-19
lines changed

4 files changed

+12
-19
lines changed

uncompyle6/scanner.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -597,25 +597,14 @@ def setTokenClass(self, tokenClass: Token) -> Token:
597597
return self.Token
598598

599599

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

620609

621610
def get_scanner(version: Union[str, tuple], is_pypy=False, show_asm=None) -> Scanner:

uncompyle6/scanners/scanner3.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@
4444
import xdis.opcodes.opcode_33 as op3
4545
from xdis import Instruction, instruction_size, iscode
4646
from xdis.bytecode import _get_const_info
47+
from xdis.opcodes.opcode_3x import parse_fn_counts_30_35
4748

48-
from uncompyle6.scanner import CONST_COLLECTIONS, Scanner, parse_fn_counts_30_35
49+
from uncompyle6.scanner import CONST_COLLECTIONS, Scanner, prefer_double_quote
4950
from uncompyle6.scanners.tok import Token
5051
from uncompyle6.util import get_code_name
5152

@@ -611,6 +612,7 @@ def ingest(
611612
pattr = "<code_object " + co_name + ">"
612613
elif isinstance(const, str):
613614
opname = "LOAD_STR"
615+
pattr = prefer_double_quote(inst.argval)
614616
else:
615617
if isinstance(inst.arg, int) and inst.arg < len(co.co_consts):
616618
argval, _ = _get_const_info(inst.arg, co.co_consts)

uncompyle6/scanners/scanner37base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from xdis import Instruction, instruction_size, iscode
4040
from xdis.bytecode import _get_const_info
4141

42-
from uncompyle6.scanner import Scanner, Token
42+
from uncompyle6.scanner import Scanner, Token, prefer_double_quote
4343

4444
globals().update(op3.opmap)
4545

@@ -386,6 +386,7 @@ def tokens_append(j, token):
386386
pattr = "<code_object " + const.co_name + ">"
387387
elif isinstance(const, str):
388388
opname = "LOAD_STR"
389+
pattr = prefer_double_quote(inst.argval)
389390
else:
390391
if isinstance(inst.arg, int) and inst.arg < len(co.co_consts):
391392
argval, _ = _get_const_info(inst.arg, co.co_consts)

uncompyle6/semantics/n_actions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def __init__(self):
3939
# parenthesis surrounding it. A high value indicates no
4040
# parenthesis are needed.
4141
self.prec = 1000
42+
self.in_format_string = False
4243

4344
def n_alias(self, node):
4445
if self.version <= (2, 1):

0 commit comments

Comments
 (0)