Skip to content

Commit cca90b6

Browse files
gh-64595: Fix write file logic in Argument Clinic (#104507)
Check if any clinic output actually changes any of the output files before deciding if we should touch the source file.
1 parent 9084e1b commit cca90b6

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

Tools/clinic/clinic.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,17 +1965,17 @@ def dump(self):
19651965
return_converters = {}
19661966

19671967

1968-
def write_file(filename, new_contents, force=False):
1968+
def file_changed(filename: str, new_contents: str) -> bool:
1969+
"""Return true if file contents changed (meaning we must update it)"""
19691970
try:
19701971
with open(filename, 'r', encoding="utf-8") as fp:
19711972
old_contents = fp.read()
1972-
1973-
if old_contents == new_contents and not force:
1974-
# no change: avoid modifying the file modification time
1975-
return
1973+
return old_contents != new_contents
19761974
except FileNotFoundError:
1977-
pass
1975+
return True
1976+
19781977

1978+
def write_file(filename: str, new_contents: str):
19791979
# Atomic write using a temporary file and os.replace()
19801980
filename_new = f"{filename}.new"
19811981
with open(filename_new, "w", encoding="utf-8") as fp:
@@ -2237,11 +2237,12 @@ def parse_file(filename, *, verify=True, output=None):
22372237
clinic = Clinic(language, verify=verify, filename=filename)
22382238
src_out, clinic_out = clinic.parse(raw)
22392239

2240-
# If clinic output changed, force updating the source file as well.
2241-
force = bool(clinic_out)
2242-
write_file(output, src_out, force=force)
2243-
for fn, data in clinic_out:
2244-
write_file(fn, data)
2240+
changes = [(fn, data) for fn, data in clinic_out if file_changed(fn, data)]
2241+
if changes:
2242+
# Always (re)write the source file.
2243+
write_file(output, src_out)
2244+
for fn, data in clinic_out:
2245+
write_file(fn, data)
22452246

22462247

22472248
def compute_checksum(input, length=None):

0 commit comments

Comments
 (0)