Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-64595: Argument Clinic: Touch source file if any output file changed #104152

Merged
merged 4 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 4 additions & 3 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ def test_eol(self):
# the last line of the block got corrupted.
c = clinic.Clinic(clinic.CLanguage(None), filename="file")
raw = "/*[clinic]\nfoo\n[clinic]*/"
cooked = c.parse(raw).splitlines()
end_line = cooked[2].rstrip()
cooked, _ = c.parse(raw)
lines = cooked.splitlines()
end_line = lines[2].rstrip()
# this test is redundant, it's just here explicitly to catch
# the regression test so we don't forget what it looked like
self.assertNotEqual(end_line, "[clinic]*/[clinic]*/")
Expand Down Expand Up @@ -259,7 +260,7 @@ def _test_clinic(self, input, output):
c = clinic.Clinic(language, filename="file")
c.parsers['inert'] = InertParser(c)
c.parsers['copy'] = CopyParser(c)
computed = c.parse(input)
computed, _ = c.parse(input)
self.assertEqual(output, computed)

def test_clinic_1(self):
Expand Down
19 changes: 13 additions & 6 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1943,12 +1943,12 @@ def dump(self):
return_converters = {}


def write_file(filename, new_contents):
def write_file(filename, new_contents, force=False):
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
try:
with open(filename, 'r', encoding="utf-8") as fp:
old_contents = fp.read()

if old_contents == new_contents:
if old_contents == new_contents and not force:
# no change: avoid modifying the file modification time
return
except FileNotFoundError:
Expand Down Expand Up @@ -2112,6 +2112,8 @@ def parse(self, input):
traceback.format_exc().rstrip())
printer.print_block(block)

clinic_out = []
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved

# these are destinations not buffers
for name, destination in self.destinations.items():
if destination.type == 'suppress':
Expand Down Expand Up @@ -2151,10 +2153,11 @@ def parse(self, input):
block.input = 'preserve\n'
printer_2 = BlockPrinter(self.language)
printer_2.print_block(block, core_includes=True)
write_file(destination.filename, printer_2.f.getvalue())
pair = destination.filename, printer_2.f.getvalue()
clinic_out.append(pair)
continue

return printer.f.getvalue()
return printer.f.getvalue(), clinic_out


def _module_and_class(self, fields):
Expand Down Expand Up @@ -2210,9 +2213,13 @@ def parse_file(filename, *, verify=True, output=None):
return

clinic = Clinic(language, verify=verify, filename=filename)
cooked = clinic.parse(raw)
src_out, clinic_out = clinic.parse(raw)

write_file(output, cooked)
# If clinic output changed, force updating the source file as well.
force = bool(clinic_out)
write_file(output, src_out, force=force)
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
for fn, data in clinic_out:
write_file(fn, data)


def compute_checksum(input, length=None):
Expand Down