mirror of
https://github.com/python/cpython.git
synced 2025-08-03 00:23:06 +00:00
bpo-46712: share more global strings in deepfreeze (gh-32152)
(for gh-90868)
This commit is contained in:
parent
3c43806513
commit
ab0d35d70d
6 changed files with 15 additions and 15 deletions
|
@ -18,7 +18,7 @@ import umarshal
|
|||
from generate_global_objects import get_identifiers_and_strings
|
||||
|
||||
verbose = False
|
||||
identifiers = get_identifiers_and_strings()[0]
|
||||
identifiers, strings = get_identifiers_and_strings()
|
||||
|
||||
def isprintable(b: bytes) -> bool:
|
||||
return all(0x20 <= c < 0x7f for c in b)
|
||||
|
@ -168,6 +168,8 @@ class Printer:
|
|||
return f"& {name}.ob_base.ob_base"
|
||||
|
||||
def generate_unicode(self, name: str, s: str) -> str:
|
||||
if s in strings:
|
||||
return f"&_Py_STR({strings[s]})"
|
||||
if s in identifiers:
|
||||
return f"&_Py_ID({s})"
|
||||
if re.match(r'\A[A-Za-z0-9_]+\Z', s):
|
||||
|
|
|
@ -1,20 +1,13 @@
|
|||
import contextlib
|
||||
import glob
|
||||
import io
|
||||
import os.path
|
||||
import re
|
||||
import sys
|
||||
|
||||
|
||||
__file__ = os.path.abspath(__file__)
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
|
||||
INTERNAL = os.path.join(ROOT, 'Include', 'internal')
|
||||
|
||||
|
||||
STRING_LITERALS = {
|
||||
'empty': '',
|
||||
'dot': '.',
|
||||
}
|
||||
IGNORED = {
|
||||
'ACTION', # Python/_warnings.c
|
||||
'ATTR', # Python/_warnings.c and Objects/funcobject.c
|
||||
|
@ -211,7 +204,7 @@ def generate_global_strings(identifiers, strings):
|
|||
printer.write(START)
|
||||
with printer.block('struct _Py_global_strings', ';'):
|
||||
with printer.block('struct', ' literals;'):
|
||||
for name, literal in sorted(strings.items()):
|
||||
for literal, name in sorted(strings.items(), key=lambda x: x[1]):
|
||||
printer.write(f'STRUCT_FOR_STR({name}, "{literal}")')
|
||||
outfile.write('\n')
|
||||
with printer.block('struct', ' identifiers;'):
|
||||
|
@ -276,7 +269,7 @@ def generate_runtime_init(identifiers, strings):
|
|||
# Global strings.
|
||||
with printer.block('.strings =', ','):
|
||||
with printer.block('.literals =', ','):
|
||||
for name, literal in sorted(strings.items()):
|
||||
for literal, name in sorted(strings.items(), key=lambda x: x[1]):
|
||||
printer.write(f'INIT_STR({name}, "{literal}"),')
|
||||
with printer.block('.identifiers =', ','):
|
||||
for name in sorted(identifiers):
|
||||
|
@ -297,15 +290,15 @@ def generate_runtime_init(identifiers, strings):
|
|||
|
||||
def get_identifiers_and_strings() -> 'tuple[set[str], dict[str, str]]':
|
||||
identifiers = set(IDENTIFIERS)
|
||||
strings = dict(STRING_LITERALS)
|
||||
strings = {}
|
||||
for name, string, *_ in iter_global_strings():
|
||||
if string is None:
|
||||
if name not in IGNORED:
|
||||
identifiers.add(name)
|
||||
else:
|
||||
if name not in strings:
|
||||
strings[name] = string
|
||||
elif string != strings[name]:
|
||||
if string not in strings:
|
||||
strings[string] = name
|
||||
elif name != strings[string]:
|
||||
raise ValueError(f'string mismatch for {name!r} ({string!r} != {strings[name]!r}')
|
||||
return identifiers, strings
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue