Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab0d35d

Browse files
bpo-46712: share more global strings in deepfreeze (gh-32152)
(for gh-90868)
1 parent 3c43806 commit ab0d35d

File tree

6 files changed

+15
-15
lines changed

6 files changed

+15
-15
lines changed

Modules/_io/textio.c

+1
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,7 @@ _io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n)
19591959
if (chunks != NULL) {
19601960
if (result != NULL && PyList_Append(chunks, result) < 0)
19611961
goto fail;
1962+
_Py_DECLARE_STR(empty, "");
19621963
Py_XSETREF(result, PyUnicode_Join(&_Py_STR(empty), chunks));
19631964
if (result == NULL)
19641965
goto fail;

Modules/_pickle.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,7 @@ get_dotted_path(PyObject *obj, PyObject *name)
18121812
{
18131813
PyObject *dotted_path;
18141814
Py_ssize_t i, n;
1815-
1815+
_Py_DECLARE_STR(dot, ".");
18161816
dotted_path = PyUnicode_Split(name, &_Py_STR(dot), -1);
18171817
if (dotted_path == NULL)
18181818
return NULL;

Objects/unicodeobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ static int unicode_is_singleton(PyObject *unicode);
258258
// Return a borrowed reference to the empty string singleton.
259259
static inline PyObject* unicode_get_empty(void)
260260
{
261+
_Py_DECLARE_STR(empty, "");
261262
return &_Py_STR(empty);
262263
}
263264

Python/compile.c

+3
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ compiler_set_qualname(struct compiler *c)
782782
}
783783

784784
if (base != NULL) {
785+
_Py_DECLARE_STR(dot, ".");
785786
name = PyUnicode_Concat(base, &_Py_STR(dot));
786787
Py_DECREF(base);
787788
if (name == NULL)
@@ -3945,6 +3946,7 @@ compiler_from_import(struct compiler *c, stmt_ty s)
39453946
ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
39463947
}
39473948
else {
3949+
_Py_DECLARE_STR(empty, "");
39483950
ADDOP_NAME(c, IMPORT_NAME, &_Py_STR(empty), names);
39493951
}
39503952
for (i = 0; i < n; i++) {
@@ -4885,6 +4887,7 @@ compiler_joined_str(struct compiler *c, expr_ty e)
48854887

48864888
Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
48874889
if (value_count > STACK_USE_GUIDELINE) {
4890+
_Py_DECLARE_STR(empty, "");
48884891
ADDOP_LOAD_CONST_NEW(c, &_Py_STR(empty));
48894892
ADDOP_NAME(c, LOAD_METHOD, &_Py_ID(join), names);
48904893
ADDOP_I(c, BUILD_LIST, 0);

Tools/scripts/deepfreeze.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from generate_global_objects import get_identifiers_and_strings
1919

2020
verbose = False
21-
identifiers = get_identifiers_and_strings()[0]
21+
identifiers, strings = get_identifiers_and_strings()
2222

2323
def isprintable(b: bytes) -> bool:
2424
return all(0x20 <= c < 0x7f for c in b)
@@ -168,6 +168,8 @@ def generate_bytes(self, name: str, b: bytes) -> str:
168168
return f"& {name}.ob_base.ob_base"
169169

170170
def generate_unicode(self, name: str, s: str) -> str:
171+
if s in strings:
172+
return f"&_Py_STR({strings[s]})"
171173
if s in identifiers:
172174
return f"&_Py_ID({s})"
173175
if re.match(r'\A[A-Za-z0-9_]+\Z', s):

Tools/scripts/generate_global_objects.py

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
import contextlib
2-
import glob
32
import io
43
import os.path
54
import re
6-
import sys
7-
85

96
__file__ = os.path.abspath(__file__)
107
ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
118
INTERNAL = os.path.join(ROOT, 'Include', 'internal')
129

1310

14-
STRING_LITERALS = {
15-
'empty': '',
16-
'dot': '.',
17-
}
1811
IGNORED = {
1912
'ACTION', # Python/_warnings.c
2013
'ATTR', # Python/_warnings.c and Objects/funcobject.c
@@ -211,7 +204,7 @@ def generate_global_strings(identifiers, strings):
211204
printer.write(START)
212205
with printer.block('struct _Py_global_strings', ';'):
213206
with printer.block('struct', ' literals;'):
214-
for name, literal in sorted(strings.items()):
207+
for literal, name in sorted(strings.items(), key=lambda x: x[1]):
215208
printer.write(f'STRUCT_FOR_STR({name}, "{literal}")')
216209
outfile.write('\n')
217210
with printer.block('struct', ' identifiers;'):
@@ -276,7 +269,7 @@ def generate_runtime_init(identifiers, strings):
276269
# Global strings.
277270
with printer.block('.strings =', ','):
278271
with printer.block('.literals =', ','):
279-
for name, literal in sorted(strings.items()):
272+
for literal, name in sorted(strings.items(), key=lambda x: x[1]):
280273
printer.write(f'INIT_STR({name}, "{literal}"),')
281274
with printer.block('.identifiers =', ','):
282275
for name in sorted(identifiers):
@@ -297,15 +290,15 @@ def generate_runtime_init(identifiers, strings):
297290

298291
def get_identifiers_and_strings() -> 'tuple[set[str], dict[str, str]]':
299292
identifiers = set(IDENTIFIERS)
300-
strings = dict(STRING_LITERALS)
293+
strings = {}
301294
for name, string, *_ in iter_global_strings():
302295
if string is None:
303296
if name not in IGNORED:
304297
identifiers.add(name)
305298
else:
306-
if name not in strings:
307-
strings[name] = string
308-
elif string != strings[name]:
299+
if string not in strings:
300+
strings[string] = name
301+
elif name != strings[string]:
309302
raise ValueError(f'string mismatch for {name!r} ({string!r} != {strings[name]!r}')
310303
return identifiers, strings
311304

0 commit comments

Comments
 (0)