Skip to content

Commit 9d1d4f9

Browse files
pythongh-64595: Fix regression in file write logic in Argument Clinic (python#106449)
Revert the two commits that introduced the regressions: - pythongh-104152 - pythongh-104507
1 parent a941bd6 commit 9d1d4f9

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

Diff for: Lib/test/test_clinic.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,8 @@ def test_eol(self):
100100
# the last line of the block got corrupted.
101101
c = clinic.Clinic(clinic.CLanguage(None), filename="file")
102102
raw = "/*[clinic]\nfoo\n[clinic]*/"
103-
cooked, _ = c.parse(raw)
104-
lines = cooked.splitlines()
105-
end_line = lines[2].rstrip()
103+
cooked = c.parse(raw).splitlines()
104+
end_line = cooked[2].rstrip()
106105
# this test is redundant, it's just here explicitly to catch
107106
# the regression test so we don't forget what it looked like
108107
self.assertNotEqual(end_line, "[clinic]*/[clinic]*/")
@@ -261,7 +260,7 @@ def _test_clinic(self, input, output):
261260
c = clinic.Clinic(language, filename="file")
262261
c.parsers['inert'] = InertParser(c)
263262
c.parsers['copy'] = CopyParser(c)
264-
computed, _ = c.parse(input)
263+
computed = c.parse(input)
265264
self.assertEqual(output, computed)
266265

267266
def test_clinic_1(self):

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)