Skip to content

Commit 345dd14

Browse files
committed
py: Support escaped special characters in autogenerated QSTRs.
1 parent 7fd84e9 commit 345dd14

1 file changed

Lines changed: 49 additions & 1 deletion

File tree

py/makeqstrdefs.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,50 @@
99
import argparse
1010
import os
1111

12+
# Python 2/3 compatibility:
13+
# - iterating through bytes is different
14+
# - codepoint2name lives in a different module
15+
import platform
16+
if platform.python_version_tuple()[0] == '2':
17+
bytes_cons = lambda val, enc=None: bytearray(val)
18+
from htmlentitydefs import name2codepoint
19+
elif platform.python_version_tuple()[0] == '3':
20+
bytes_cons = bytes
21+
from html.entities import name2codepoint
22+
# end compatibility code
23+
1224
# Blacklist of qstrings that are specially handled in further
1325
# processing and should be ignored
1426
QSTRING_BLACK_LIST = {'NULL', 'number_of', }
1527

28+
# add some custom names to map characters that aren't in HTML
29+
name2codepoint['hyphen'] = ord('-')
30+
name2codepoint['space'] = ord(' ')
31+
name2codepoint['squot'] = ord('\'')
32+
name2codepoint['comma'] = ord(',')
33+
name2codepoint['dot'] = ord('.')
34+
name2codepoint['colon'] = ord(':')
35+
name2codepoint['semicolon'] = ord(';')
36+
name2codepoint['slash'] = ord('/')
37+
name2codepoint['percent'] = ord('%')
38+
name2codepoint['hash'] = ord('#')
39+
name2codepoint['paren_open'] = ord('(')
40+
name2codepoint['paren_close'] = ord(')')
41+
name2codepoint['bracket_open'] = ord('[')
42+
name2codepoint['bracket_close'] = ord(']')
43+
name2codepoint['brace_open'] = ord('{')
44+
name2codepoint['brace_close'] = ord('}')
45+
name2codepoint['star'] = ord('*')
46+
name2codepoint['bang'] = ord('!')
47+
name2codepoint['backslash'] = ord('\\')
48+
name2codepoint['plus'] = ord('+')
49+
name2codepoint['dollar'] = ord('$')
50+
name2codepoint['equals'] = ord('=')
51+
name2codepoint['question'] = ord('?')
52+
name2codepoint['at_sign'] = ord('@')
53+
name2codepoint['caret'] = ord('^')
54+
name2codepoint['pipe'] = ord('|')
55+
name2codepoint['tilde'] = ord('~')
1656

1757
def write_out(fname, output):
1858
if output:
@@ -21,6 +61,14 @@ def write_out(fname, output):
2161
with open(args.output_dir + "/" + fname + ".qstr", "w") as f:
2262
f.write("\n".join(output) + "\n")
2363

64+
def qstr_unescape(qstr):
65+
for name in name2codepoint:
66+
if "__" + name + "__" in qstr:
67+
continue
68+
if "_" + name + "_" in qstr:
69+
qstr = qstr.replace("_" + name + "_", str(unichr(name2codepoint[name])))
70+
return qstr
71+
2472
def process_file(f):
2573
output = []
2674
last_fname = None
@@ -40,7 +88,7 @@ def process_file(f):
4088
for match in re.findall(r'MP_QSTR_[_a-zA-Z0-9]+', line):
4189
name = match.replace('MP_QSTR_', '')
4290
if name not in QSTRING_BLACK_LIST:
43-
output.append('Q(' + name + ')')
91+
output.append('Q(' + qstr_unescape(name) + ')')
4492

4593
write_out(last_fname, output)
4694
return ""

0 commit comments

Comments
 (0)