bpo-46712: Share global string identifiers in deepfreeze (GH-31261) · python/cpython@31fa802

GitHub

File tree

Misc/NEWS.d/next/Core and Builtins

Tools/scripts

Original file line numberDiff line numberDiff line change@@ -0,0 +1 @@

1+Share global string identifiers in deep-frozen modules.

Original file line numberDiff line numberDiff line change@@ -15,9 +15,10 @@

1515fromtypingimportDict, FrozenSet, TextIO, Tuple

16161717importumarshal

18+fromgenerate_global_objectsimportget_identifiers_and_strings

18191920verbose=False

20-21+identifiers=get_identifiers_and_strings()[0]

21222223defisprintable(b: bytes) ->bool:

2324returnall(0x20<=c<0x7fforcinb)

@@ -167,6 +168,8 @@ def generate_bytes(self, name: str, b: bytes) -> str:

167168returnf"& {name}.ob_base.ob_base"

168169169170defgenerate_unicode(self, name: str, s: str) ->str:

171+ifsinidentifiers:

172+returnf"&_Py_ID({s})"

170173kind, ascii=analyze_character_width(s)

171174ifkind==PyUnicode_1BYTE_KIND:

172175datatype="uint8_t"

Original file line numberDiff line numberDiff line change@@ -256,13 +256,10 @@ def generate_runtime_init(identifiers, strings):

256256printer.write(after)

257257258258259-#######################################

260-# the script

261-262-defmain() ->None:

259+defget_identifiers_and_strings() ->tuple[set[str], dict[str, str]]:

263260identifiers=set(IDENTIFIERS)

264261strings=dict(STRING_LITERALS)

265-forname, string, filename, lno, _initer_global_strings():

262+forname, string, *_initer_global_strings():

266263ifstringisNone:

267264ifnamenotinIGNORED:

268265identifiers.add(name)

@@ -271,6 +268,13 @@ def main() -> None:

271268strings[name] =string

272269elifstring!=strings[name]:

273270raiseValueError(f'string mismatch for {name!r} ({string!r} != {strings[name]!r}')

271+returnidentifiers, strings

272+273+#######################################

274+# the script

275+276+defmain() ->None:

277+identifiers, strings=get_identifiers_and_strings()

274278275279generate_global_strings(identifiers, strings)

276280generate_runtime_init(identifiers, strings)