11import contextlib
2- import glob
32import io
43import os .path
54import re
6- import sys
7-
85
96__file__ = os .path .abspath (__file__ )
107ROOT = os .path .dirname (os .path .dirname (os .path .dirname (__file__ )))
118INTERNAL = os .path .join (ROOT , 'Include' , 'internal' )
129
1310
14- STRING_LITERALS = {
15- 'empty' : '' ,
16- 'dot' : '.' ,
17- }
1811IGNORED = {
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
298291def 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