]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Add try-finally to close the file after loading it in
authorGuido van Rossum <guido@python.org>
Wed, 13 Aug 1997 14:48:36 +0000 (14:48 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 13 Aug 1997 14:48:36 +0000 (14:48 +0000)
ModuleLoader.load_module!  (Thanks to Daniel Larsson who complained
about this.)

Lib/ihooks.py

index dfe29cfea961c548788ffaaca0e7fe82acb292b3..8f64957dd733a3c4f247366a28046207d2cbd9ba 100644 (file)
@@ -252,19 +252,22 @@ class ModuleLoader(BasicModuleLoader):
 
     def load_module(self, name, stuff):
        file, filename, (suff, mode, type) = stuff
-       if type == BUILTIN_MODULE:
-           return self.hooks.init_builtin(name)
-       if type == FROZEN_MODULE:
-           return self.hooks.init_frozen(name)
-       if type == C_EXTENSION:
-           m = self.hooks.load_dynamic(name, filename, file)
-       elif type == PY_SOURCE:
-           m = self.hooks.load_source(name, filename, file)
-       elif type == PY_COMPILED:
-           m = self.hooks.load_compiled(name, filename, file)
-       else:
-           raise ImportError, "Unrecognized module type (%s) for %s" % \
-                 (`type`, name)
+       try:
+           if type == BUILTIN_MODULE:
+               return self.hooks.init_builtin(name)
+           if type == FROZEN_MODULE:
+               return self.hooks.init_frozen(name)
+           if type == C_EXTENSION:
+               m = self.hooks.load_dynamic(name, filename, file)
+           elif type == PY_SOURCE:
+               m = self.hooks.load_source(name, filename, file)
+           elif type == PY_COMPILED:
+               m = self.hooks.load_compiled(name, filename, file)
+           else:
+               raise ImportError, "Unrecognized module type (%s) for %s" % \
+                     (`type`, name)
+       finally:
+           if file: file.close()
        m.__file__ = filename
        return m