Always close all filehandles

This commit is contained in:
Peter Melnichenko 2016-05-01 21:59:00 +03:00
parent 1d184a0c14
commit fa0639f640

View File

@ -267,9 +267,9 @@ def objext():
return ".obj" if using_cl() else ".o" return ".obj" if using_cl() else ".o"
def sha256_of_file(filename): def sha256_of_file(filename):
fileobj = open(filename, "rb") with open(filename, "rb") as handler:
contents = fileobj.read() contents = handler.read()
fileobj.close()
return hashlib.sha256(contents).hexdigest() return hashlib.sha256(contents).hexdigest()
class Program(object): class Program(object):
@ -459,8 +459,7 @@ class Lua(Program):
@staticmethod @staticmethod
def major_version_from_source(): def major_version_from_source():
lua_h = open(os.path.join("src", "lua.h")) with open(os.path.join("src", "lua.h")) as lua_h:
for line in lua_h: for line in lua_h:
match = re.match(r"^\s*#define\s+LUA_VERSION_NUM\s+50(\d)\s*$", line) match = re.match(r"^\s*#define\s+LUA_VERSION_NUM\s+50(\d)\s*$", line)
@ -545,18 +544,16 @@ class Lua(Program):
def patch_redefines(self): def patch_redefines(self):
redefines = "\n".join(self.redefines) redefines = "\n".join(self.redefines)
luaconf_h = open(os.path.join("src", "luaconf.h"), "rb") with open(os.path.join("src", "luaconf.h"), "rb") as luaconf_h:
luaconf_src = luaconf_h.read() luaconf_src = luaconf_h.read()
luaconf_h.close()
body, _, tail = luaconf_src.rpartition(b"#endif") body, _, tail = luaconf_src.rpartition(b"#endif")
luaconf_h = open(os.path.join("src", "luaconf.h"), "wb") with open(os.path.join("src", "luaconf.h"), "wb") as luaconf_h:
luaconf_h.write(body) luaconf_h.write(body)
luaconf_h.write(redefines.encode("UTF-8")) luaconf_h.write(redefines.encode("UTF-8"))
luaconf_h.write(b"\n#endif") luaconf_h.write(b"\n#endif")
luaconf_h.write(tail) luaconf_h.write(tail)
luaconf_h.close()
def build(self): def build(self):
if opts.builds and self.source != "local": if opts.builds and self.source != "local":
@ -910,8 +907,7 @@ class RioLua(Lua):
r'^\s*#define LUA_VERSION_RELEASE\s+"(\d)"\s*$' r'^\s*#define LUA_VERSION_RELEASE\s+"(\d)"\s*$'
] ]
lua_h = open(os.path.join("lua.h")) with open(os.path.join("lua.h")) as lua_h:
for line in lua_h: for line in lua_h:
for regexp in regexps: for regexp in regexps:
match = re.match(regexp, line) match = re.match(regexp, line)
@ -1132,18 +1128,16 @@ class LuaJIT(Lua):
@staticmethod @staticmethod
def add_cflags_to_msvcbuild(cflags): def add_cflags_to_msvcbuild(cflags):
msvcbuild_file = open("msvcbuild.bat", "rb") with open("msvcbuild.bat", "rb") as msvcbuild_file:
msvcbuild_src = msvcbuild_file.read() msvcbuild_src = msvcbuild_file.read()
msvcbuild_file.close()
start, assignment, value_and_rest = msvcbuild_src.partition(b"@set LJCOMPILE") start, assignment, value_and_rest = msvcbuild_src.partition(b"@set LJCOMPILE")
value_and_rest = value_and_rest.decode("UTF-8") value_and_rest = value_and_rest.decode("UTF-8")
msvcbuild_file = open("msvcbuild.bat", "wb") with open("msvcbuild.bat", "wb") as msvcbuild_file:
msvcbuild_file.write(start) msvcbuild_file.write(start)
msvcbuild_file.write(assignment) msvcbuild_file.write(assignment)
msvcbuild_file.write(value_and_rest.replace("\r\n", " {}\r\n".format(cflags), 1).encode("UTF-8")) msvcbuild_file.write(value_and_rest.replace("\r\n", " {}\r\n".format(cflags), 1).encode("UTF-8"))
msvcbuild_file.close()
def make(self): def make(self):
cflags = list(self.compat_cflags) cflags = list(self.compat_cflags)
@ -1260,8 +1254,7 @@ class LuaRocks(Program):
if self.source == "release": if self.source == "release":
return self.versions.index(self.version) < self.versions.index("2.1.0") return self.versions.index(self.version) < self.versions.index("2.1.0")
makefile = open("Makefile") with open("Makefile") as makefile:
for line in makefile: for line in makefile:
if re.match(r"^\s*all:\s+built\s*$", line): if re.match(r"^\s*all:\s+built\s*$", line):
return True return True
@ -1327,9 +1320,9 @@ class LuaRocks(Program):
if cmake_generator is not None: if cmake_generator is not None:
config_path = os.path.join( config_path = os.path.join(
opts.location, "luarocks", "config-{}.lua".format(lua_identifiers["major version"])) opts.location, "luarocks", "config-{}.lua".format(lua_identifiers["major version"]))
config_h = open(config_path, "ab")
with open(config_path, "ab") as config_h:
config_h.write('\r\ncmake_generator = "{}"\r\n'.format(cmake_generator).encode("UTF-8")) config_h.write('\r\ncmake_generator = "{}"\r\n'.format(cmake_generator).encode("UTF-8"))
config_h.close()
else: else:
print("Building LuaRocks" + self.version_suffix) print("Building LuaRocks" + self.version_suffix)
@ -1488,13 +1481,11 @@ def setup_vs_and_rerun(vs_version, arch):
")" ")"
]) ])
bat_h = open(bat_name, "wb") with open(bat_name, "wb") as bat_h:
bat_h.write("\r\n".join(bat_lines).encode("UTF-8")) bat_h.write("\r\n".join(bat_lines).encode("UTF-8"))
bat_h.close()
argv_h = open(argv_name, "wb") with open(argv_name, "wb") as argv_h:
argv_h.write("\r\n".join(sys.argv).encode("UTF-8")) argv_h.write("\r\n".join(sys.argv).encode("UTF-8"))
argv_h.close()
exit_code = subprocess.call([bat_name]) exit_code = subprocess.call([bat_name])
shutil.rmtree(temp_dir) shutil.rmtree(temp_dir)
@ -1529,9 +1520,9 @@ def setup_vs(target):
class UseActualArgsFileAction(argparse.Action): class UseActualArgsFileAction(argparse.Action):
def __call__(self, parser, namespace, fname, option_string=None): def __call__(self, parser, namespace, fname, option_string=None):
args_h = open(fname, "rb") with open(fname, "rb") as args_h:
args_content = args_h.read().decode("UTF-8") args_content = args_h.read().decode("UTF-8")
args_h.close()
main(args_content.split("\r\n")[1:]) main(args_content.split("\r\n")[1:])
def main(argv=None): def main(argv=None):