bpo-46712: share more global strings in deepfreeze (gh-32152)

(for gh-90868)
This commit is contained in:
Kumar Aditya 2022-04-19 23:11:36 +05:30 committed by GitHub
parent 3c43806513
commit ab0d35d70d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 15 additions and 15 deletions

View File

@ -1959,6 +1959,7 @@ _io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n)
if (chunks != NULL) { if (chunks != NULL) {
if (result != NULL && PyList_Append(chunks, result) < 0) if (result != NULL && PyList_Append(chunks, result) < 0)
goto fail; goto fail;
_Py_DECLARE_STR(empty, "");
Py_XSETREF(result, PyUnicode_Join(&_Py_STR(empty), chunks)); Py_XSETREF(result, PyUnicode_Join(&_Py_STR(empty), chunks));
if (result == NULL) if (result == NULL)
goto fail; goto fail;

View File

@ -1812,7 +1812,7 @@ get_dotted_path(PyObject *obj, PyObject *name)
{ {
PyObject *dotted_path; PyObject *dotted_path;
Py_ssize_t i, n; Py_ssize_t i, n;
_Py_DECLARE_STR(dot, ".");
dotted_path = PyUnicode_Split(name, &_Py_STR(dot), -1); dotted_path = PyUnicode_Split(name, &_Py_STR(dot), -1);
if (dotted_path == NULL) if (dotted_path == NULL)
return NULL; return NULL;

View File

@ -258,6 +258,7 @@ static int unicode_is_singleton(PyObject *unicode);
// Return a borrowed reference to the empty string singleton. // Return a borrowed reference to the empty string singleton.
static inline PyObject* unicode_get_empty(void) static inline PyObject* unicode_get_empty(void)
{ {
_Py_DECLARE_STR(empty, "");
return &_Py_STR(empty); return &_Py_STR(empty);
} }

View File

@ -782,6 +782,7 @@ compiler_set_qualname(struct compiler *c)
} }
if (base != NULL) { if (base != NULL) {
_Py_DECLARE_STR(dot, ".");
name = PyUnicode_Concat(base, &_Py_STR(dot)); name = PyUnicode_Concat(base, &_Py_STR(dot));
Py_DECREF(base); Py_DECREF(base);
if (name == NULL) if (name == NULL)
@ -3945,6 +3946,7 @@ compiler_from_import(struct compiler *c, stmt_ty s)
ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
} }
else { else {
_Py_DECLARE_STR(empty, "");
ADDOP_NAME(c, IMPORT_NAME, &_Py_STR(empty), names); ADDOP_NAME(c, IMPORT_NAME, &_Py_STR(empty), names);
} }
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
@ -4885,6 +4887,7 @@ compiler_joined_str(struct compiler *c, expr_ty e)
Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values); Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
if (value_count > STACK_USE_GUIDELINE) { if (value_count > STACK_USE_GUIDELINE) {
_Py_DECLARE_STR(empty, "");
ADDOP_LOAD_CONST_NEW(c, &_Py_STR(empty)); ADDOP_LOAD_CONST_NEW(c, &_Py_STR(empty));
ADDOP_NAME(c, LOAD_METHOD, &_Py_ID(join), names); ADDOP_NAME(c, LOAD_METHOD, &_Py_ID(join), names);
ADDOP_I(c, BUILD_LIST, 0); ADDOP_I(c, BUILD_LIST, 0);

View File

@ -18,7 +18,7 @@
from generate_global_objects import get_identifiers_and_strings from generate_global_objects import get_identifiers_and_strings
verbose = False verbose = False
identifiers = get_identifiers_and_strings()[0] identifiers, strings = get_identifiers_and_strings()
def isprintable(b: bytes) -> bool: def isprintable(b: bytes) -> bool:
return all(0x20 <= c < 0x7f for c in b) return all(0x20 <= c < 0x7f for c in b)
@ -168,6 +168,8 @@ def generate_bytes(self, name: str, b: bytes) -> str:
return f"& {name}.ob_base.ob_base" return f"& {name}.ob_base.ob_base"
def generate_unicode(self, name: str, s: str) -> str: def generate_unicode(self, name: str, s: str) -> str:
if s in strings:
return f"&_Py_STR({strings[s]})"
if s in identifiers: if s in identifiers:
return f"&_Py_ID({s})" return f"&_Py_ID({s})"
if re.match(r'\A[A-Za-z0-9_]+\Z', s): if re.match(r'\A[A-Za-z0-9_]+\Z', s):

View File

@ -1,20 +1,13 @@
import contextlib import contextlib
import glob
import io import io
import os.path import os.path
import re import re
import sys
__file__ = os.path.abspath(__file__) __file__ = os.path.abspath(__file__)
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
INTERNAL = os.path.join(ROOT, 'Include', 'internal') INTERNAL = os.path.join(ROOT, 'Include', 'internal')
STRING_LITERALS = {
'empty': '',
'dot': '.',
}
IGNORED = { IGNORED = {
'ACTION', # Python/_warnings.c 'ACTION', # Python/_warnings.c
'ATTR', # Python/_warnings.c and Objects/funcobject.c 'ATTR', # Python/_warnings.c and Objects/funcobject.c
@ -211,7 +204,7 @@ def generate_global_strings(identifiers, strings):
printer.write(START) printer.write(START)
with printer.block('struct _Py_global_strings', ';'): with printer.block('struct _Py_global_strings', ';'):
with printer.block('struct', ' literals;'): 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}")') printer.write(f'STRUCT_FOR_STR({name}, "{literal}")')
outfile.write('\n') outfile.write('\n')
with printer.block('struct', ' identifiers;'): with printer.block('struct', ' identifiers;'):
@ -276,7 +269,7 @@ def generate_runtime_init(identifiers, strings):
# Global strings. # Global strings.
with printer.block('.strings =', ','): with printer.block('.strings =', ','):
with printer.block('.literals =', ','): 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}"),') printer.write(f'INIT_STR({name}, "{literal}"),')
with printer.block('.identifiers =', ','): with printer.block('.identifiers =', ','):
for name in sorted(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]]': def get_identifiers_and_strings() -> 'tuple[set[str], dict[str, str]]':
identifiers = set(IDENTIFIERS) identifiers = set(IDENTIFIERS)
strings = dict(STRING_LITERALS) strings = {}
for name, string, *_ in iter_global_strings(): for name, string, *_ in iter_global_strings():
if string is None: if string is None:
if name not in IGNORED: if name not in IGNORED:
identifiers.add(name) identifiers.add(name)
else: else:
if name not in strings: if string not in strings:
strings[name] = string strings[string] = name
elif string != strings[name]: elif name != strings[string]:
raise ValueError(f'string mismatch for {name!r} ({string!r} != {strings[name]!r}') raise ValueError(f'string mismatch for {name!r} ({string!r} != {strings[name]!r}')
return identifiers, strings return identifiers, strings