1
1
import contextlib
2
- import glob
3
2
import io
4
3
import os .path
5
4
import re
6
- import sys
7
-
8
5
9
6
__file__ = os .path .abspath (__file__ )
10
7
ROOT = os .path .dirname (os .path .dirname (os .path .dirname (__file__ )))
11
8
INTERNAL = os .path .join (ROOT , 'Include' , 'internal' )
12
9
13
10
14
- STRING_LITERALS = {
15
- 'empty' : '' ,
16
- 'dot' : '.' ,
17
- }
18
11
IGNORED = {
19
12
'ACTION' , # Python/_warnings.c
20
13
'ATTR' , # Python/_warnings.c and Objects/funcobject.c
@@ -211,7 +204,7 @@ def generate_global_strings(identifiers, strings):
211
204
printer .write (START )
212
205
with printer .block ('struct _Py_global_strings' , ';' ):
213
206
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 ] ):
215
208
printer .write (f'STRUCT_FOR_STR({ name } , "{ literal } ")' )
216
209
outfile .write ('\n ' )
217
210
with printer .block ('struct' , ' identifiers;' ):
@@ -276,7 +269,7 @@ def generate_runtime_init(identifiers, strings):
276
269
# Global strings.
277
270
with printer .block ('.strings =' , ',' ):
278
271
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 ] ):
280
273
printer .write (f'INIT_STR({ name } , "{ literal } "),' )
281
274
with printer .block ('.identifiers =' , ',' ):
282
275
for name in sorted (identifiers ):
@@ -297,15 +290,15 @@ def generate_runtime_init(identifiers, strings):
297
290
298
291
def get_identifiers_and_strings () -> 'tuple[set[str], dict[str, str]]' :
299
292
identifiers = set (IDENTIFIERS )
300
- strings = dict ( STRING_LITERALS )
293
+ strings = {}
301
294
for name , string , * _ in iter_global_strings ():
302
295
if string is None :
303
296
if name not in IGNORED :
304
297
identifiers .add (name )
305
298
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 ]:
309
302
raise ValueError (f'string mismatch for { name !r} ({ string !r} != { strings [name ]!r} ' )
310
303
return identifiers , strings
311
304
0 commit comments