summaryrefslogtreecommitdiffstats
path: root/Lib/modulefinder.py
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2010-10-30 00:26:48 (GMT)
committerBrett Cannon <bcannon@gmail.com>2010-10-30 00:26:48 (GMT)
commit028011f9f098d37d9dc81df7737fc38126fbdc76 (patch)
tree3c0caf1271dd40ec6ebb777f3908e6e4608cab5d /Lib/modulefinder.py
parent011057854d86346fd19ba845a0a9759eae22c39c (diff)
downloadcpython-028011f9f098d37d9dc81df7737fc38126fbdc76.zip
cpython-028011f9f098d37d9dc81df7737fc38126fbdc76.tar.gz
cpython-028011f9f098d37d9dc81df7737fc38126fbdc76.tar.bz2
Silence ResourceWarnings in modulefinder by using file context managers.
Diffstat (limited to 'Lib/modulefinder.py')
-rw-r--r--Lib/modulefinder.py22
1 files changed, 13 insertions, 9 deletions
diff --git a/Lib/modulefinder.py b/Lib/modulefinder.py
index 478c749..646a785 100644
--- a/Lib/modulefinder.py
+++ b/Lib/modulefinder.py
@@ -105,16 +105,16 @@ class ModuleFinder:
def run_script(self, pathname):
self.msg(2, "run_script", pathname)
- fp = open(pathname, READ_MODE)
- stuff = ("", "r", imp.PY_SOURCE)
- self.load_module('__main__', fp, pathname, stuff)
+ 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)
- stuff = (ext, "r", imp.PY_SOURCE)
- self.load_module(name, fp, pathname, stuff)
+ with open(pathname, READ_MODE) as fp:
+ stuff = (ext, "r", imp.PY_SOURCE)
+ self.load_module(name, fp, pathname, stuff)
def import_hook(self, name, caller=None, fromlist=None, level=-1):
self.msg(3, "import_hook", name, caller, fromlist, level)
@@ -451,9 +451,13 @@ class ModuleFinder:
m.__path__ = m.__path__ + packagePathMap.get(fqname, [])
fp, buf, stuff = self.find_module("__init__", m.__path__)
- self.load_module(fqname, fp, buf, stuff)
- self.msgout(2, "load_package ->", m)
- return m
+ try:
+ self.load_module(fqname, fp, buf, stuff)
+ self.msgout(2, "load_package ->", m)
+ return m
+ finally:
+ if fp:
+ fp.close()
def add_module(self, fqname):
if fqname in self.modules: