Skip to content

Commit 6f5936a

Browse files
pythongh-64595: Fix regression in file write logic in Argument Clinic
Revert the two commits that introduced the regressions: - pythongh-104152 - pythongh-104507
1 parent a941bd6 commit 6f5936a

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

Diff for: Tools/clinic/clinic.py

+12-23
Original file line numberDiff line numberDiff line change
@@ -2021,22 +2021,20 @@ def dump(self):
20212021
extensions['py'] = PythonLanguage
20222022

20232023

2024-
def file_changed(filename: str, new_contents: str) -> bool:
2025-
"""Return true if file contents changed (meaning we must update it)"""
2024+
def write_file(filename: str, new_contents: str) -> None:
20262025
try:
2027-
with open(filename, encoding="utf-8") as fp:
2026+
with open(filename, 'r', encoding="utf-8") as fp:
20282027
old_contents = fp.read()
2029-
return old_contents != new_contents
2030-
except FileNotFoundError:
2031-
return True
20322028

2033-
2034-
def write_file(filename: str, new_contents: str) -> None:
2029+
if old_contents == new_contents:
2030+
# no change: avoid modifying the file modification time
2031+
return
2032+
except FileNotFoundError:
2033+
pass
20352034
# Atomic write using a temporary file and os.replace()
20362035
filename_new = f"{filename}.new"
20372036
with open(filename_new, "w", encoding="utf-8") as fp:
20382037
fp.write(new_contents)
2039-
20402038
try:
20412039
os.replace(filename_new, filename)
20422040
except:
@@ -2214,16 +2212,13 @@ def parse(self, input):
22142212
traceback.format_exc().rstrip())
22152213
printer.print_block(block)
22162214

2217-
clinic_out = []
2218-
22192215
# these are destinations not buffers
22202216
for name, destination in self.destinations.items():
22212217
if destination.type == 'suppress':
22222218
continue
22232219
output = destination.dump()
22242220

22252221
if output:
2226-
22272222
block = Block("", dsl_name="clinic", output=output)
22282223

22292224
if destination.type == 'buffer':
@@ -2255,11 +2250,10 @@ def parse(self, input):
22552250
block.input = 'preserve\n'
22562251
printer_2 = BlockPrinter(self.language)
22572252
printer_2.print_block(block, core_includes=True)
2258-
pair = destination.filename, printer_2.f.getvalue()
2259-
clinic_out.append(pair)
2253+
write_file(destination.filename, printer_2.f.getvalue())
22602254
continue
22612255

2262-
return printer.f.getvalue(), clinic_out
2256+
return printer.f.getvalue()
22632257

22642258

22652259
def _module_and_class(self, fields):
@@ -2321,14 +2315,9 @@ def parse_file(
23212315

23222316
assert isinstance(language, CLanguage)
23232317
clinic = Clinic(language, verify=verify, filename=filename)
2324-
src_out, clinic_out = clinic.parse(raw)
2325-
2326-
changes = [(fn, data) for fn, data in clinic_out if file_changed(fn, data)]
2327-
if changes:
2328-
# Always (re)write the source file.
2329-
write_file(output, src_out)
2330-
for fn, data in clinic_out:
2331-
write_file(fn, data)
2318+
cooked = clinic.parse(raw)
2319+
2320+
write_file(output, cooked)
23322321

23332322

23342323
def compute_checksum(

0 commit comments

Comments
 (0)