Skip to content

Commit 474fdbe

Browse files
bpo-47152: Automatically regenerate sre_constants.h (GH-91439)
* Move the code for generating Modules/_sre/sre_constants.h from Lib/re/_constants.py into a separate script Tools/scripts/generate_sre_constants.py. * Add target `regen-sre` in the makefile. * Make target `regen-all` depending on `regen-sre`.
1 parent 943ca5e commit 474fdbe

File tree

5 files changed

+73
-46
lines changed

5 files changed

+73
-46
lines changed

Lib/re/_constants.py

-43
Original file line numberDiff line numberDiff line change
@@ -215,46 +215,3 @@ def _makecodes(names):
215215
SRE_INFO_PREFIX = 1 # has prefix
216216
SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix)
217217
SRE_INFO_CHARSET = 4 # pattern starts with character from given set
218-
219-
if __name__ == "__main__":
220-
def dump(f, d, prefix):
221-
items = sorted(d)
222-
for item in items:
223-
f.write("#define %s_%s %d\n" % (prefix, item, item))
224-
with open("sre_constants.h", "w") as f:
225-
f.write("""\
226-
/*
227-
* Secret Labs' Regular Expression Engine
228-
*
229-
* regular expression matching engine
230-
*
231-
* NOTE: This file is generated by Lib/re/_constants.py. If you need
232-
* to change anything in here, edit Lib/re/_constants.py and run it.
233-
*
234-
* Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
235-
*
236-
* See the sre.c file for information on usage and redistribution.
237-
*/
238-
239-
""")
240-
241-
f.write("#define SRE_MAGIC %d\n" % MAGIC)
242-
243-
dump(f, OPCODES, "SRE_OP")
244-
dump(f, ATCODES, "SRE")
245-
dump(f, CHCODES, "SRE")
246-
247-
f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE)
248-
f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE)
249-
f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE)
250-
f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL)
251-
f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE)
252-
f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE)
253-
f.write("#define SRE_FLAG_DEBUG %d\n" % SRE_FLAG_DEBUG)
254-
f.write("#define SRE_FLAG_ASCII %d\n" % SRE_FLAG_ASCII)
255-
256-
f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX)
257-
f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL)
258-
f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET)
259-
260-
print("done")

Makefile.pre.in

+8-1
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ regen-limited-abi: all
11971197
# Regenerate all generated files
11981198

11991199
regen-all: regen-opcode regen-opcode-targets regen-typeslots \
1200-
regen-token regen-ast regen-keyword regen-frozen clinic \
1200+
regen-token regen-ast regen-keyword regen-sre regen-frozen clinic \
12011201
regen-pegen-metaparser regen-pegen regen-test-frozenmain \
12021202
regen-global-objects
12031203
@echo
@@ -1350,6 +1350,13 @@ regen-stdlib-module-names: build_all Programs/_testembed
13501350
> $(srcdir)/Python/stdlib_module_names.h.new
13511351
$(UPDATE_FILE) $(srcdir)/Python/stdlib_module_names.h $(srcdir)/Python/stdlib_module_names.h.new
13521352

1353+
regen-sre:
1354+
# Regenerate Modules/_sre/sre_constants.h from Lib/re/_constants.py
1355+
# using Tools/scripts/generate_sre_constants.py
1356+
$(PYTHON_FOR_REGEN) $(srcdir)/Tools/scripts/generate_sre_constants.py \
1357+
$(srcdir)/Lib/re/_constants.py \
1358+
$(srcdir)/Modules/_sre/sre_constants.h
1359+
13531360
Python/compile.o Python/symtable.o Python/ast_unparse.o Python/ast.o Python/future.o: $(srcdir)/Include/internal/pycore_ast.h
13541361

13551362
Python/getplatform.o: $(srcdir)/Python/getplatform.c
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add script and make target for generating ``sre_constants.h``.

Modules/_sre/sre_constants.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* regular expression matching engine
55
*
6-
* NOTE: This file is generated by Lib/re/_constants.py. If you need
7-
* to change anything in here, edit Lib/re/_constants.py and run it.
6+
* Auto-generated by Tools/scripts/generate_sre_constants.py from
7+
* Lib/re/_constants.py.
88
*
99
* Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
1010
*
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#! /usr/bin/env python3
2+
# This script generates Modules/_sre/sre_constants.h from Lib/re/_constants.py.
3+
4+
5+
def update_file(file, content):
6+
try:
7+
with open(file, 'r') as fobj:
8+
if fobj.read() == content:
9+
return False
10+
except (OSError, ValueError):
11+
pass
12+
with open(file, 'w') as fobj:
13+
fobj.write(content)
14+
return True
15+
16+
sre_constants_header = """\
17+
/*
18+
* Secret Labs' Regular Expression Engine
19+
*
20+
* regular expression matching engine
21+
*
22+
* Auto-generated by Tools/scripts/generate_sre_constants.py from
23+
* Lib/re/_constants.py.
24+
*
25+
* Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
26+
*
27+
* See the sre.c file for information on usage and redistribution.
28+
*/
29+
30+
"""
31+
32+
def main(infile='Lib/re/_constants.py', outfile='Modules/_sre/sre_constants.h'):
33+
ns = {}
34+
with open(infile) as fp:
35+
code = fp.read()
36+
exec(code, ns)
37+
38+
def dump(d, prefix):
39+
items = sorted(d)
40+
for item in items:
41+
yield "#define %s_%s %d\n" % (prefix, item, item)
42+
43+
def dump2(d, prefix):
44+
items = [(value, name) for name, value in d.items()
45+
if name.startswith(prefix)]
46+
for value, name in sorted(items):
47+
yield "#define %s %d\n" % (name, value)
48+
49+
content = [sre_constants_header]
50+
content.append("#define SRE_MAGIC %d\n" % ns["MAGIC"])
51+
content.extend(dump(ns["OPCODES"], "SRE_OP"))
52+
content.extend(dump(ns["ATCODES"], "SRE"))
53+
content.extend(dump(ns["CHCODES"], "SRE"))
54+
content.extend(dump2(ns, "SRE_FLAG_"))
55+
content.extend(dump2(ns, "SRE_INFO_"))
56+
57+
update_file(outfile, ''.join(content))
58+
59+
60+
if __name__ == '__main__':
61+
import sys
62+
main(*sys.argv[1:])

0 commit comments

Comments
 (0)