Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-46712: Share global string identifiers in deepfreeze #31261

Merged
merged 3 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Share global string identifiers in deep-frozen modules.
5 changes: 4 additions & 1 deletion Tools/scripts/deepfreeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
from typing import Dict, FrozenSet, TextIO, Tuple

kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
import umarshal
from generate_global_objects import get_identifiers_and_strings

verbose = False

identifiers = get_identifiers_and_strings()[0]

def isprintable(b: bytes) -> bool:
return all(0x20 <= c < 0x7f for c in b)
Expand Down Expand Up @@ -166,6 +167,8 @@ def generate_bytes(self, name: str, b: bytes) -> str:
return f"& {name}.ob_base.ob_base"

def generate_unicode(self, name: str, s: str) -> str:
if s in identifiers:
return f"&_Py_ID({s})"
kind, ascii = analyze_character_width(s)
if kind == PyUnicode_1BYTE_KIND:
datatype = "uint8_t"
Expand Down
14 changes: 9 additions & 5 deletions Tools/scripts/generate_global_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,10 @@ def generate_runtime_init(identifiers, strings):
printer.write(after)


#######################################
# the script

def main() -> None:
def get_identifiers_and_strings() -> tuple[set[str], dict[str, str]]:
identifiers = set(IDENTIFIERS)
strings = dict(STRING_LITERALS)
for name, string, filename, lno, _ in iter_global_strings():
for name, string, *_ in iter_global_strings():
if string is None:
if name not in IGNORED:
identifiers.add(name)
Expand All @@ -271,6 +268,13 @@ def main() -> None:
strings[name] = string
elif string != strings[name]:
raise ValueError(f'string mismatch for {name!r} ({string!r} != {strings[name]!r}')
return identifiers, strings

#######################################
# the script

def main() -> None:
identifiers, strings = get_identifiers_and_strings()

generate_global_strings(identifiers, strings)
generate_runtime_init(identifiers, strings)
Expand Down