Skip to content

Commit 1976bae

Browse files
committed
Retain file order of qstr definitions.
Want common qstrs to be first in the list so they have the lowest ids, so that in the byte code they take up the least room.
1 parent 60aca48 commit 1976bae

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

py/makeqstrdata.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import argparse
22
import re
3-
from htmlentitydefs import codepoint2name
3+
4+
# codepoint2name is different in Python 2 to Python 3
5+
import platform
6+
if platform.python_version_tuple()[0] == '2':
7+
from htmlentitydefs import codepoint2name
8+
elif platform.python_version_tuple()[0] == '3':
9+
from html.entities import codepoint2name
410

511
# this must match the equivalent function in qstr.c
612
def compute_hash(qstr):
@@ -37,13 +43,13 @@ def do_work(infiles):
3743
if ident in qstrs:
3844
continue
3945

40-
# add the qstr to the list
41-
qstrs[ident] = qstr
46+
# add the qstr to the list, with order number to retain original order in file
47+
qstrs[ident] = (len(qstrs), ident, qstr)
4248

4349
# process the qstrs, printing out the generated C header file
4450
print('// This file was automatically generated by makeqstrdata.py')
4551
print('')
46-
for ident, qstr in qstrs.items():
52+
for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
4753
qhash = compute_hash(qstr)
4854
qlen = len(qstr)
4955
print('Q({}, (const byte*)"\\x{:02x}\\x{:02x}\\x{:02x}\\x{:02x}" "{}")'.format(ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qstr))

0 commit comments

Comments
 (0)