Skip to content

Commit 15bf463

Browse files
pythongh-64595: Fix write file logic in Argument Clinic
Check if any clinic output actually changes any of the output files before deciding if we should touch the source file. This needs to be done before we eventually update the source file.
1 parent b378d99 commit 15bf463

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

Diff for: Tools/clinic/clinic.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -1965,16 +1965,20 @@ 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+
1977+
1978+
def write_file(filename: str, new_contents: str, force=False):
1979+
if not force and not file_changed(filename, new_contents):
1980+
# no change: avoid modifying the file modification time
1981+
return
19781982

19791983
# Atomic write using a temporary file and os.replace()
19801984
filename_new = f"{filename}.new"
@@ -2238,7 +2242,7 @@ def parse_file(filename, *, verify=True, output=None):
22382242
src_out, clinic_out = clinic.parse(raw)
22392243

22402244
# If clinic output changed, force updating the source file as well.
2241-
force = bool(clinic_out)
2245+
force = any(file_changed(fn, data) for fn, data in clinic_out)
22422246
write_file(output, src_out, force=force)
22432247
for fn, data in clinic_out:
22442248
write_file(fn, data)

0 commit comments

Comments
 (0)