forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bpo-47152: Automatically regenerate sre_constants.h (pythonGH-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`.
- Loading branch information
1 parent
943ca5e
commit 474fdbe
Showing
5 changed files
with
73 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Build/2022-04-10-16-33-31.bpo-47152.TLkxKm.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add script and make target for generating ``sre_constants.h``. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:]) |