@@ -946,21 +946,20 @@ def git_get_keywords(versionfile_abs):
946
946
# _version.py.
947
947
keywords = {}
948
948
try :
949
- f = open (versionfile_abs , "r" )
950
- for line in f .readlines ():
951
- if line .strip ().startswith ("git_refnames =" ):
952
- mo = re .search (r'=\s*"(.*)"' , line )
953
- if mo :
954
- keywords ["refnames" ] = mo .group (1 )
955
- if line .strip ().startswith ("git_full =" ):
956
- mo = re .search (r'=\s*"(.*)"' , line )
957
- if mo :
958
- keywords ["full" ] = mo .group (1 )
959
- if line .strip ().startswith ("git_date =" ):
960
- mo = re .search (r'=\s*"(.*)"' , line )
961
- if mo :
962
- keywords ["date" ] = mo .group (1 )
963
- f .close ()
949
+ with open (versionfile_abs , "r" ) as f :
950
+ for line in f .readlines ():
951
+ if line .strip ().startswith ("git_refnames =" ):
952
+ mo = re .search (r'=\s*"(.*)"' , line )
953
+ if mo :
954
+ keywords ["refnames" ] = mo .group (1 )
955
+ if line .strip ().startswith ("git_full =" ):
956
+ mo = re .search (r'=\s*"(.*)"' , line )
957
+ if mo :
958
+ keywords ["full" ] = mo .group (1 )
959
+ if line .strip ().startswith ("git_date =" ):
960
+ mo = re .search (r'=\s*"(.*)"' , line )
961
+ if mo :
962
+ keywords ["date" ] = mo .group (1 )
964
963
except EnvironmentError :
965
964
pass
966
965
return keywords
@@ -1142,18 +1141,16 @@ def do_vcs_install(manifest_in, versionfile_source, ipy):
1142
1141
files .append (versioneer_file )
1143
1142
present = False
1144
1143
try :
1145
- f = open (".gitattributes" , "r" )
1146
- for line in f .readlines ():
1147
- if line .strip ().startswith (versionfile_source ):
1148
- if "export-subst" in line .strip ().split ()[1 :]:
1149
- present = True
1150
- f .close ()
1144
+ with open (".gitattributes" , "r" ) as f :
1145
+ for line in f .readlines ():
1146
+ if line .strip ().startswith (versionfile_source ):
1147
+ if "export-subst" in line .strip ().split ()[1 :]:
1148
+ present = True
1151
1149
except EnvironmentError :
1152
1150
pass
1153
1151
if not present :
1154
- f = open (".gitattributes" , "a+" )
1155
- f .write ("%s export-subst\n " % versionfile_source )
1156
- f .close ()
1152
+ with open (".gitattributes" , "a+" ) as f :
1153
+ f .write ("%s export-subst\n " % versionfile_source )
1157
1154
files .append (".gitattributes" )
1158
1155
run_command (GITS , ["add" , "--" ] + files )
1159
1156
0 commit comments