@@ -1965,17 +1965,17 @@ def dump(self):
1965
1965
return_converters = {}
1966
1966
1967
1967
1968
- def write_file (filename , new_contents , force = False ):
1968
+ def file_changed (filename : str , new_contents : str ) -> bool :
1969
+ """Return true if file contents changed (meaning we must update it)"""
1969
1970
try :
1970
1971
with open (filename , 'r' , encoding = "utf-8" ) as fp :
1971
1972
old_contents = fp .read ()
1972
-
1973
- if old_contents == new_contents and not force :
1974
- # no change: avoid modifying the file modification time
1975
- return
1973
+ return old_contents != new_contents
1976
1974
except FileNotFoundError :
1977
- pass
1975
+ return True
1976
+
1978
1977
1978
+ def write_file (filename : str , new_contents : str ):
1979
1979
# Atomic write using a temporary file and os.replace()
1980
1980
filename_new = f"{ filename } .new"
1981
1981
with open (filename_new , "w" , encoding = "utf-8" ) as fp :
@@ -2237,11 +2237,12 @@ def parse_file(filename, *, verify=True, output=None):
2237
2237
clinic = Clinic (language , verify = verify , filename = filename )
2238
2238
src_out , clinic_out = clinic .parse (raw )
2239
2239
2240
- # If clinic output changed, force updating the source file as well.
2241
- force = bool (clinic_out )
2242
- write_file (output , src_out , force = force )
2243
- for fn , data in clinic_out :
2244
- write_file (fn , data )
2240
+ changes = [(fn , data ) for fn , data in clinic_out if file_changed (fn , data )]
2241
+ if changes :
2242
+ # Always (re)write the source file.
2243
+ write_file (output , src_out )
2244
+ for fn , data in clinic_out :
2245
+ write_file (fn , data )
2245
2246
2246
2247
2247
2248
def compute_checksum (input , length = None ):
0 commit comments