Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-64595: Fix regression in file write logic in Argument Clinic
Revert the two commits that introduced the regressions:

 - gh-104152
 - gh-104507
  • Loading branch information
erlend-aasland committed Jul 5, 2023
commit 6f5936a2919e61c4f4445a8c5b716682baf4efd3
35 changes: 12 additions & 23 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2021,22 +2021,20 @@ def dump(self):
extensions['py'] = PythonLanguage


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


def write_file(filename: str, new_contents: str) -> None:
if old_contents == new_contents:
# no change: avoid modifying the file modification time
return
except FileNotFoundError:
pass
# Atomic write using a temporary file and os.replace()
filename_new = f"{filename}.new"
with open(filename_new, "w", encoding="utf-8") as fp:
fp.write(new_contents)

try:
os.replace(filename_new, filename)
except:
Expand Down Expand Up @@ -2214,16 +2212,13 @@ def parse(self, input):
traceback.format_exc().rstrip())
printer.print_block(block)

clinic_out = []

# these are destinations not buffers
for name, destination in self.destinations.items():
if destination.type == 'suppress':
continue
output = destination.dump()

if output:

block = Block("", dsl_name="clinic", output=output)

if destination.type == 'buffer':
Expand Down Expand Up @@ -2255,11 +2250,10 @@ def parse(self, input):
block.input = 'preserve\n'
printer_2 = BlockPrinter(self.language)
printer_2.print_block(block, core_includes=True)
pair = destination.filename, printer_2.f.getvalue()
clinic_out.append(pair)
write_file(destination.filename, printer_2.f.getvalue())
continue

return printer.f.getvalue(), clinic_out
return printer.f.getvalue()


def _module_and_class(self, fields):
Expand Down Expand Up @@ -2321,14 +2315,9 @@ def parse_file(

assert isinstance(language, CLanguage)
clinic = Clinic(language, verify=verify, filename=filename)
src_out, clinic_out = clinic.parse(raw)

changes = [(fn, data) for fn, data in clinic_out if file_changed(fn, data)]
if changes:
# Always (re)write the source file.
write_file(output, src_out)
for fn, data in clinic_out:
write_file(fn, data)
cooked = clinic.parse(raw)

write_file(output, cooked)


def compute_checksum(
Expand Down