mirror of
https://github.com/python/cpython.git
synced 2025-07-19 17:25:54 +00:00
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`.
This commit is contained in:
parent
943ca5e1d6
commit
474fdbe9e4
5 changed files with 73 additions and 46 deletions
|
@ -215,46 +215,3 @@ SRE_FLAG_ASCII = 256 # use ascii "locale"
|
||||||
SRE_INFO_PREFIX = 1 # has prefix
|
SRE_INFO_PREFIX = 1 # has prefix
|
||||||
SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix)
|
SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix)
|
||||||
SRE_INFO_CHARSET = 4 # pattern starts with character from given set
|
SRE_INFO_CHARSET = 4 # pattern starts with character from given set
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
def dump(f, d, prefix):
|
|
||||||
items = sorted(d)
|
|
||||||
for item in items:
|
|
||||||
f.write("#define %s_%s %d\n" % (prefix, item, item))
|
|
||||||
with open("sre_constants.h", "w") as f:
|
|
||||||
f.write("""\
|
|
||||||
/*
|
|
||||||
* Secret Labs' Regular Expression Engine
|
|
||||||
*
|
|
||||||
* regular expression matching engine
|
|
||||||
*
|
|
||||||
* NOTE: This file is generated by Lib/re/_constants.py. If you need
|
|
||||||
* to change anything in here, edit Lib/re/_constants.py and run it.
|
|
||||||
*
|
|
||||||
* Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
|
|
||||||
*
|
|
||||||
* See the sre.c file for information on usage and redistribution.
|
|
||||||
*/
|
|
||||||
|
|
||||||
""")
|
|
||||||
|
|
||||||
f.write("#define SRE_MAGIC %d\n" % MAGIC)
|
|
||||||
|
|
||||||
dump(f, OPCODES, "SRE_OP")
|
|
||||||
dump(f, ATCODES, "SRE")
|
|
||||||
dump(f, CHCODES, "SRE")
|
|
||||||
|
|
||||||
f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE)
|
|
||||||
f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE)
|
|
||||||
f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE)
|
|
||||||
f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL)
|
|
||||||
f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE)
|
|
||||||
f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE)
|
|
||||||
f.write("#define SRE_FLAG_DEBUG %d\n" % SRE_FLAG_DEBUG)
|
|
||||||
f.write("#define SRE_FLAG_ASCII %d\n" % SRE_FLAG_ASCII)
|
|
||||||
|
|
||||||
f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX)
|
|
||||||
f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL)
|
|
||||||
f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET)
|
|
||||||
|
|
||||||
print("done")
|
|
||||||
|
|
|
@ -1197,7 +1197,7 @@ regen-limited-abi: all
|
||||||
# Regenerate all generated files
|
# Regenerate all generated files
|
||||||
|
|
||||||
regen-all: regen-opcode regen-opcode-targets regen-typeslots \
|
regen-all: regen-opcode regen-opcode-targets regen-typeslots \
|
||||||
regen-token regen-ast regen-keyword regen-frozen clinic \
|
regen-token regen-ast regen-keyword regen-sre regen-frozen clinic \
|
||||||
regen-pegen-metaparser regen-pegen regen-test-frozenmain \
|
regen-pegen-metaparser regen-pegen regen-test-frozenmain \
|
||||||
regen-global-objects
|
regen-global-objects
|
||||||
@echo
|
@echo
|
||||||
|
@ -1350,6 +1350,13 @@ regen-stdlib-module-names: build_all Programs/_testembed
|
||||||
> $(srcdir)/Python/stdlib_module_names.h.new
|
> $(srcdir)/Python/stdlib_module_names.h.new
|
||||||
$(UPDATE_FILE) $(srcdir)/Python/stdlib_module_names.h $(srcdir)/Python/stdlib_module_names.h.new
|
$(UPDATE_FILE) $(srcdir)/Python/stdlib_module_names.h $(srcdir)/Python/stdlib_module_names.h.new
|
||||||
|
|
||||||
|
regen-sre:
|
||||||
|
# Regenerate Modules/_sre/sre_constants.h from Lib/re/_constants.py
|
||||||
|
# using Tools/scripts/generate_sre_constants.py
|
||||||
|
$(PYTHON_FOR_REGEN) $(srcdir)/Tools/scripts/generate_sre_constants.py \
|
||||||
|
$(srcdir)/Lib/re/_constants.py \
|
||||||
|
$(srcdir)/Modules/_sre/sre_constants.h
|
||||||
|
|
||||||
Python/compile.o Python/symtable.o Python/ast_unparse.o Python/ast.o Python/future.o: $(srcdir)/Include/internal/pycore_ast.h
|
Python/compile.o Python/symtable.o Python/ast_unparse.o Python/ast.o Python/future.o: $(srcdir)/Include/internal/pycore_ast.h
|
||||||
|
|
||||||
Python/getplatform.o: $(srcdir)/Python/getplatform.c
|
Python/getplatform.o: $(srcdir)/Python/getplatform.c
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Add script and make target for generating ``sre_constants.h``.
|
|
@ -3,8 +3,8 @@
|
||||||
*
|
*
|
||||||
* regular expression matching engine
|
* regular expression matching engine
|
||||||
*
|
*
|
||||||
* NOTE: This file is generated by Lib/re/_constants.py. If you need
|
* Auto-generated by Tools/scripts/generate_sre_constants.py from
|
||||||
* to change anything in here, edit Lib/re/_constants.py and run it.
|
* Lib/re/_constants.py.
|
||||||
*
|
*
|
||||||
* Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
|
* Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
|
||||||
*
|
*
|
||||||
|
|
62
Tools/scripts/generate_sre_constants.py
Executable file
62
Tools/scripts/generate_sre_constants.py
Executable file
|
@ -0,0 +1,62 @@
|
||||||
|
#! /usr/bin/env python3
|
||||||
|
# This script generates Modules/_sre/sre_constants.h from Lib/re/_constants.py.
|
||||||
|
|
||||||
|
|
||||||
|
def update_file(file, content):
|
||||||
|
try:
|
||||||
|
with open(file, 'r') as fobj:
|
||||||
|
if fobj.read() == content:
|
||||||
|
return False
|
||||||
|
except (OSError, ValueError):
|
||||||
|
pass
|
||||||
|
with open(file, 'w') as fobj:
|
||||||
|
fobj.write(content)
|
||||||
|
return True
|
||||||
|
|
||||||
|
sre_constants_header = """\
|
||||||
|
/*
|
||||||
|
* Secret Labs' Regular Expression Engine
|
||||||
|
*
|
||||||
|
* regular expression matching engine
|
||||||
|
*
|
||||||
|
* Auto-generated by Tools/scripts/generate_sre_constants.py from
|
||||||
|
* Lib/re/_constants.py.
|
||||||
|
*
|
||||||
|
* Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
|
||||||
|
*
|
||||||
|
* See the sre.c file for information on usage and redistribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
def main(infile='Lib/re/_constants.py', outfile='Modules/_sre/sre_constants.h'):
|
||||||
|
ns = {}
|
||||||
|
with open(infile) as fp:
|
||||||
|
code = fp.read()
|
||||||
|
exec(code, ns)
|
||||||
|
|
||||||
|
def dump(d, prefix):
|
||||||
|
items = sorted(d)
|
||||||
|
for item in items:
|
||||||
|
yield "#define %s_%s %d\n" % (prefix, item, item)
|
||||||
|
|
||||||
|
def dump2(d, prefix):
|
||||||
|
items = [(value, name) for name, value in d.items()
|
||||||
|
if name.startswith(prefix)]
|
||||||
|
for value, name in sorted(items):
|
||||||
|
yield "#define %s %d\n" % (name, value)
|
||||||
|
|
||||||
|
content = [sre_constants_header]
|
||||||
|
content.append("#define SRE_MAGIC %d\n" % ns["MAGIC"])
|
||||||
|
content.extend(dump(ns["OPCODES"], "SRE_OP"))
|
||||||
|
content.extend(dump(ns["ATCODES"], "SRE"))
|
||||||
|
content.extend(dump(ns["CHCODES"], "SRE"))
|
||||||
|
content.extend(dump2(ns, "SRE_FLAG_"))
|
||||||
|
content.extend(dump2(ns, "SRE_INFO_"))
|
||||||
|
|
||||||
|
update_file(outfile, ''.join(content))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import sys
|
||||||
|
main(*sys.argv[1:])
|
Loading…
Add table
Add a link
Reference in a new issue