]> granicus.if.org Git - python/commitdiff
Silence ResourceWarnings in modulefinder by using file context managers.
authorBrett Cannon <bcannon@gmail.com>
Sat, 30 Oct 2010 00:26:48 +0000 (00:26 +0000)
committerBrett Cannon <bcannon@gmail.com>
Sat, 30 Oct 2010 00:26:48 +0000 (00:26 +0000)
Lib/modulefinder.py
Misc/NEWS

index 478c74912d2a38349cd8b607d3a320aa43ab31be..646a7852ba9d4abd617c24bde23448dab5b6cb08 100644 (file)
@@ -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:
index 795b3d51ac0c1e9f65d12ce4d7bb6a1ad71bc6a6..b1e701d8f208c217ede68fc90dab2c23bfbc07a3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -54,6 +54,8 @@ Core and Builtins
 Library
 -------
 
+- Close file objects in modulefinder in a timely manner.
+
 - Close a io.TextIOWrapper object in email.parser in a timely manner.
 
 - Close a file object in distutils.sysconfig in a timely manner.