# Global objects
.PHONY: regen-global-objects
-regen-global-objects: regen-deepfreeze $(srcdir)/Tools/scripts/generate_global_objects.py
+regen-global-objects: $(srcdir)/Tools/scripts/generate_global_objects.py
+ $(PYTHON_FOR_REGEN) $(srcdir)/Tools/scripts/generate_global_objects.py
+ @# Run one more time after deepfreezing, to catch any globals added
+ @# there. This is necessary because the deep-frozen code isn't
+ @# commited to the repo.
+ $(MAKE) regen-deepfreeze
$(PYTHON_FOR_REGEN) $(srcdir)/Tools/scripts/generate_global_objects.py
############################################################################
import contextlib
import glob
+import io
import os.path
import re
import sys
varname, string = m.groups()
yield varname, string, filename, lno, line
+
def iter_to_marker(lines, marker):
for line in lines:
if line.rstrip() == marker:
self.write("}" + suffix)
+@contextlib.contextmanager
+def open_for_changes(filename, orig):
+ """Like open() but only write to the file if it changed."""
+ outfile = io.StringIO()
+ yield outfile
+ text = outfile.getvalue()
+ if text != orig:
+ with open(filename, 'w', encoding='utf-8') as outfile:
+ outfile.write(text)
+ else:
+ print(f'# not changed: {filename}')
+
+
#######################################
# the global objects
# Read the non-generated part of the file.
with open(filename) as infile:
- before = ''.join(iter_to_marker(infile, START))[:-1]
- for _ in iter_to_marker(infile, END):
- pass
- after = infile.read()[:-1]
+ orig = infile.read()
+ lines = iter(orig.rstrip().splitlines())
+ before = '\n'.join(iter_to_marker(lines, START))
+ for _ in iter_to_marker(lines, END):
+ pass
+ after = '\n'.join(lines)
# Generate the file.
- with open(filename, 'w', encoding='utf-8') as outfile:
+ with open_for_changes(filename, orig) as outfile:
printer = Printer(outfile)
printer.write(before)
printer.write(START)
with printer.block('struct', ' latin1[128];'):
printer.write("PyCompactUnicodeObject _latin1;")
printer.write("uint8_t _data[2];")
-
printer.write(END)
printer.write(after)
# Read the non-generated part of the file.
with open(filename) as infile:
- before = ''.join(iter_to_marker(infile, START))[:-1]
- for _ in iter_to_marker(infile, END):
- pass
- after = infile.read()[:-1]
+ orig = infile.read()
+ lines = iter(orig.rstrip().splitlines())
+ before = '\n'.join(iter_to_marker(lines, START))
+ for _ in iter_to_marker(lines, END):
+ pass
+ after = '\n'.join(lines)
# Generate the file.
- with open(filename, 'w', encoding='utf-8') as outfile:
+ with open_for_changes(filename, orig) as outfile:
printer = Printer(outfile)
printer.write(before)
printer.write(START)
raise ValueError(f'string mismatch for {name!r} ({string!r} != {strings[name]!r}')
return identifiers, strings
+
#######################################
# the script