Simplify double quote preference in string

This commit is contained in:
rocky
2024-02-17 19:46:57 -05:00
parent 9f9f6de983
commit 0a08b8d3fc
3 changed files with 6 additions and 6 deletions

View File

@@ -24,7 +24,7 @@ scanners, e.g. for Python 2.7 or 3.4.
from array import array
from collections import namedtuple
from types import ModuleType
from typing import Optional, Tuple, Union
from typing import Optional, Union
import xdis
from xdis import (
@@ -654,9 +654,9 @@ def prefer_double_quote(string: str) -> str:
Prefer a double quoted string over a
single quoted string when possible
"""
if string.find("'") == -1:
return f'"{string}"'
return repr(string)
if string.find("'") == -1 and not string.startswith("'''"):
return f'"{string[1:-2]}"'
return string
if __name__ == "__main__":

View File

@@ -612,7 +612,7 @@ class Scanner3(Scanner):
pattr = "<code_object " + co_name + ">"
elif isinstance(const, str):
opname = "LOAD_STR"
pattr = prefer_double_quote(inst.argval)
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)

View File

@@ -386,7 +386,7 @@ class Scanner37Base(Scanner):
pattr = "<code_object " + const.co_name + ">"
elif isinstance(const, str):
opname = "LOAD_STR"
pattr = prefer_double_quote(inst.argval)
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)