| 598 | return self._patch(content)[0] |
| 599 | |
| 600 | def patch_file(self, filename): |
| 601 | if os.path.basename(filename) == PYTHONCAPI_COMPAT_H: |
| 602 | self.log(f"Skip {filename}") |
| 603 | return |
| 604 | |
| 605 | encoding = "utf-8" |
| 606 | errors = "surrogateescape" |
| 607 | |
| 608 | with open(filename, encoding=encoding, errors=errors, newline="") as fp: |
| 609 | old_contents = fp.read() |
| 610 | |
| 611 | new_contents, operations = self._patch(old_contents) |
| 612 | |
| 613 | if self.args.to_stdout: |
| 614 | print(new_contents, end="") |
| 615 | return (new_contents != old_contents) |
| 616 | |
| 617 | # Don't rewrite if the filename for in-place replacement, |
| 618 | # to avoid changing the file modification time. |
| 619 | if new_contents == old_contents: |
| 620 | return False |
| 621 | |
| 622 | if not self.args.no_backup: |
| 623 | old_filename = filename + ".old" |
| 624 | # If old_filename already exists, replace it |
| 625 | os.replace(filename, old_filename) |
| 626 | |
| 627 | with open(filename, "w", encoding=encoding, errors=errors, newline="") as fp: |
| 628 | fp.write(new_contents) |
| 629 | |
| 630 | self.applied_operations |= set(operations) |
| 631 | operations = ', '.join(operations) |
| 632 | self.log(f"Patched file: {filename} ({operations})") |
| 633 | return True |
| 634 | |
| 635 | def _walk_dir(self, path): |
| 636 | empty = True |