Commit 5421f36b authored by Matti Picus's avatar Matti Picus
Browse files

close files opened during import (upstream issue 21610 created and patch submitted)

parent 94a0df43071c
......@@ -422,7 +422,8 @@ def _compile(pathname, timestamp):
saved back to the filesystem for future imports. The source file's
modification timestamp must be provided as a Long value.
"""
codestring = open(pathname, 'rU').read()
with open(pathname, 'rU') as fp:
codestring = fp.read()
if codestring and codestring[-1] != '\n':
codestring = codestring + '\n'
code = __builtin__.compile(codestring, pathname, 'exec')
......@@ -603,7 +604,7 @@ class DynLoadSuffixImporter:
self.desc = desc
def import_file(self, filename, finfo, fqname):
fp = open(filename, self.desc[1])
with open(filename, self.desc[1]) as fp:
module = imp.load_module(fqname, fp, filename, self.desc)
module.__file__ = filename
return 0, module, { }
......
......@@ -109,14 +109,14 @@ class ModuleFinder:
def run_script(self, pathname):
self.msg(2, "run_script", pathname)
fp = open(pathname, READ_MODE)
with open(pathname, READ_MODE) as fp:
stuff = ("", "r", imp.PY_SOURCE)
self.load_module('__main__', fp, pathname, stuff)
def load_file(self, pathname):
dir, name = os.path.split(pathname)
name, ext = os.path.splitext(name)
fp = open(pathname, READ_MODE)
with open(pathname, READ_MODE) as fp:
stuff = (ext, "r", imp.PY_SOURCE)
self.load_module(name, fp, pathname, stuff)
......@@ -461,6 +461,8 @@ class ModuleFinder:
fp, buf, stuff = self.find_module("__init__", m.__path__)
self.load_module(fqname, fp, buf, stuff)
self.msgout(2, "load_package ->", m)
if fp:
fp.close()
return m
def add_module(self, fqname):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment