Skip to content

Commit d3192a2

Browse files
erlend-aaslandjbower-fb
authored andcommitted
pythongh-64595: Argument Clinic: Touch source file if any output file changed (python#104152)
1 parent 78dc058 commit d3192a2

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

Diff for: Lib/test/test_clinic.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ def test_eol(self):
9999
# the last line of the block got corrupted.
100100
c = clinic.Clinic(clinic.CLanguage(None), filename="file")
101101
raw = "/*[clinic]\nfoo\n[clinic]*/"
102-
cooked = c.parse(raw).splitlines()
103-
end_line = cooked[2].rstrip()
102+
cooked, _ = c.parse(raw)
103+
lines = cooked.splitlines()
104+
end_line = lines[2].rstrip()
104105
# this test is redundant, it's just here explicitly to catch
105106
# the regression test so we don't forget what it looked like
106107
self.assertNotEqual(end_line, "[clinic]*/[clinic]*/")
@@ -259,7 +260,7 @@ def _test_clinic(self, input, output):
259260
c = clinic.Clinic(language, filename="file")
260261
c.parsers['inert'] = InertParser(c)
261262
c.parsers['copy'] = CopyParser(c)
262-
computed = c.parse(input)
263+
computed, _ = c.parse(input)
263264
self.assertEqual(output, computed)
264265

265266
def test_clinic_1(self):

Diff for: Tools/clinic/clinic.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -1954,12 +1954,12 @@ def dump(self):
19541954
return_converters = {}
19551955

19561956

1957-
def write_file(filename, new_contents):
1957+
def write_file(filename, new_contents, force=False):
19581958
try:
19591959
with open(filename, 'r', encoding="utf-8") as fp:
19601960
old_contents = fp.read()
19611961

1962-
if old_contents == new_contents:
1962+
if old_contents == new_contents and not force:
19631963
# no change: avoid modifying the file modification time
19641964
return
19651965
except FileNotFoundError:
@@ -2123,6 +2123,8 @@ def parse(self, input):
21232123
traceback.format_exc().rstrip())
21242124
printer.print_block(block)
21252125

2126+
clinic_out = []
2127+
21262128
# these are destinations not buffers
21272129
for name, destination in self.destinations.items():
21282130
if destination.type == 'suppress':
@@ -2162,10 +2164,11 @@ def parse(self, input):
21622164
block.input = 'preserve\n'
21632165
printer_2 = BlockPrinter(self.language)
21642166
printer_2.print_block(block, core_includes=True)
2165-
write_file(destination.filename, printer_2.f.getvalue())
2167+
pair = destination.filename, printer_2.f.getvalue()
2168+
clinic_out.append(pair)
21662169
continue
21672170

2168-
return printer.f.getvalue()
2171+
return printer.f.getvalue(), clinic_out
21692172

21702173

21712174
def _module_and_class(self, fields):
@@ -2221,9 +2224,13 @@ def parse_file(filename, *, verify=True, output=None):
22212224
return
22222225

22232226
clinic = Clinic(language, verify=verify, filename=filename)
2224-
cooked = clinic.parse(raw)
2227+
src_out, clinic_out = clinic.parse(raw)
22252228

2226-
write_file(output, cooked)
2229+
# If clinic output changed, force updating the source file as well.
2230+
force = bool(clinic_out)
2231+
write_file(output, src_out, force=force)
2232+
for fn, data in clinic_out:
2233+
write_file(fn, data)
22272234

22282235

22292236
def compute_checksum(input, length=None):

0 commit comments

Comments
 (0)