Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Catch up with main
  • Loading branch information
brandtbucher committed Apr 13, 2022
commit 24fa5a18df1c1ed5a268739625e292e5ccd87ce8
51 changes: 0 additions & 51 deletions Lib/re/_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,54 +215,3 @@ def _makecodes(names):
SRE_INFO_PREFIX = 1 # has prefix
SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix)
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)

with open("sre_targets.h", "w") as f:
f.write("// NOTE: This file is generated by Lib/re/_constants.py!\n\n")
f.write(f"static void *sre_targets[{len(OPCODES)}] = {{\n")
for i, item in enumerate(sorted(OPCODES)):
assert i == item
f.write(f" &&TARGET_SRE_OP_{item},\n")
f.write("};\n")

print("done")
7 changes: 4 additions & 3 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -1351,11 +1351,12 @@ regen-stdlib-module-names: build_all Programs/_testembed
$(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
# Regenerate Modules/_sre/sre_constants.h and Modules/_sre/sre_targets.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
$(srcdir)/Modules/_sre/sre_constants.h \
$(srcdir)/Modules/_sre/sre_targets.h \

Python/compile.o Python/symtable.o Python/ast_unparse.o Python/ast.o Python/future.o: $(srcdir)/Include/internal/pycore_ast.h

Expand Down
13 changes: 12 additions & 1 deletion Modules/_sre/sre_targets.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
// NOTE: This file is generated by Lib/re/_constants.py!
/*
* 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.
*/

static void *sre_targets[44] = {
&&TARGET_SRE_OP_FAILURE,
Expand Down
20 changes: 18 additions & 2 deletions Tools/scripts/generate_sre_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ def update_file(file, content):

"""

def main(infile='Lib/re/_constants.py', outfile='Modules/_sre/sre_constants.h'):
def main(
infile="Lib/re/_constants.py",
outfile_constants="Modules/_sre/sre_constants.h",
outfile_targets="Modules/_sre/sre_targets.h",
):
ns = {}
with open(infile) as fp:
code = fp.read()
Expand All @@ -46,6 +50,11 @@ def dump2(d, prefix):
for value, name in sorted(items):
yield "#define %s %d\n" % (name, value)

def dump_gotos(d, prefix):
for i, item in enumerate(sorted(d)):
assert i == item
yield f" &&{prefix}_{item},\n"

content = [sre_constants_header]
content.append("#define SRE_MAGIC %d\n" % ns["MAGIC"])
content.extend(dump(ns["OPCODES"], "SRE_OP"))
Expand All @@ -54,7 +63,14 @@ def dump2(d, prefix):
content.extend(dump2(ns, "SRE_FLAG_"))
content.extend(dump2(ns, "SRE_INFO_"))

update_file(outfile, ''.join(content))
update_file(outfile_constants, ''.join(content))

content = [sre_constants_header]
content.append(f"static void *sre_targets[{len(ns['OPCODES'])}] = {{\n")
content.extend(dump_gotos(ns["OPCODES"], "TARGET_SRE_OP"))
content.append("};\n")

update_file(outfile_targets, ''.join(content))


if __name__ == '__main__':
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.