Skip to content

Commit b02430a

Browse files
[3.12] 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 (cherry picked from commit 9d1d4f9) Co-authored-by: Erlend Aasland <[email protected]>
1 parent 74d84cf commit b02430a

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
@@ -1966,22 +1966,20 @@ def dump(self):
19661966
extensions['py'] = PythonLanguage
19671967

19681968

1969-
def file_changed(filename: str, new_contents: str) -> bool:
1970-
"""Return true if file contents changed (meaning we must update it)"""
1969+
def write_file(filename: str, new_contents: str) -> None:
19711970
try:
1972-
with open(filename, encoding="utf-8") as fp:
1971+
with open(filename, 'r', encoding="utf-8") as fp:
19731972
old_contents = fp.read()
1974-
return old_contents != new_contents
1975-
except FileNotFoundError:
1976-
return True
1977-
19781973

1979-
def write_file(filename: str, new_contents: str):
1974+
if old_contents == new_contents:
1975+
# no change: avoid modifying the file modification time
1976+
return
1977+
except FileNotFoundError:
1978+
pass
19801979
# Atomic write using a temporary file and os.replace()
19811980
filename_new = f"{filename}.new"
19821981
with open(filename_new, "w", encoding="utf-8") as fp:
19831982
fp.write(new_contents)
1984-
19851983
try:
19861984
os.replace(filename_new, filename)
19871985
except:
@@ -2159,16 +2157,13 @@ def parse(self, input):
21592157
traceback.format_exc().rstrip())
21602158
printer.print_block(block)
21612159

2162-
clinic_out = []
2163-
21642160
# these are destinations not buffers
21652161
for name, destination in self.destinations.items():
21662162
if destination.type == 'suppress':
21672163
continue
21682164
output = destination.dump()
21692165

21702166
if output:
2171-
21722167
block = Block("", dsl_name="clinic", output=output)
21732168

21742169
if destination.type == 'buffer':
@@ -2200,11 +2195,10 @@ def parse(self, input):
22002195
block.input = 'preserve\n'
22012196
printer_2 = BlockPrinter(self.language)
22022197
printer_2.print_block(block, core_includes=True)
2203-
pair = destination.filename, printer_2.f.getvalue()
2204-
clinic_out.append(pair)
2198+
write_file(destination.filename, printer_2.f.getvalue())
22052199
continue
22062200

2207-
return printer.f.getvalue(), clinic_out
2201+
return printer.f.getvalue()
22082202

22092203

22102204
def _module_and_class(self, fields):
@@ -2266,14 +2260,9 @@ def parse_file(
22662260

22672261
assert isinstance(language, CLanguage)
22682262
clinic = Clinic(language, verify=verify, filename=filename)
2269-
src_out, clinic_out = clinic.parse(raw)
2270-
2271-
changes = [(fn, data) for fn, data in clinic_out if file_changed(fn, data)]
2272-
if changes:
2273-
# Always (re)write the source file.
2274-
write_file(output, src_out)
2275-
for fn, data in clinic_out:
2276-
write_file(fn, data)
2263+
cooked = clinic.parse(raw)
2264+
2265+
write_file(output, cooked)
22772266

22782267

22792268
def compute_checksum(

0 commit comments

Comments
 (0)