Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6dc61b1

Browse files
committed
Add try-finally to close the file after loading it in
ModuleLoader.load_module! (Thanks to Daniel Larsson who complained about this.)
1 parent 6af4abd commit 6dc61b1

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

Lib/ihooks.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -252,19 +252,22 @@ def find_module_in_dir(self, name, dir):
252252

253253
def load_module(self, name, stuff):
254254
file, filename, (suff, mode, type) = stuff
255-
if type == BUILTIN_MODULE:
256-
return self.hooks.init_builtin(name)
257-
if type == FROZEN_MODULE:
258-
return self.hooks.init_frozen(name)
259-
if type == C_EXTENSION:
260-
m = self.hooks.load_dynamic(name, filename, file)
261-
elif type == PY_SOURCE:
262-
m = self.hooks.load_source(name, filename, file)
263-
elif type == PY_COMPILED:
264-
m = self.hooks.load_compiled(name, filename, file)
265-
else:
266-
raise ImportError, "Unrecognized module type (%s) for %s" % \
267-
(`type`, name)
255+
try:
256+
if type == BUILTIN_MODULE:
257+
return self.hooks.init_builtin(name)
258+
if type == FROZEN_MODULE:
259+
return self.hooks.init_frozen(name)
260+
if type == C_EXTENSION:
261+
m = self.hooks.load_dynamic(name, filename, file)
262+
elif type == PY_SOURCE:
263+
m = self.hooks.load_source(name, filename, file)
264+
elif type == PY_COMPILED:
265+
m = self.hooks.load_compiled(name, filename, file)
266+
else:
267+
raise ImportError, "Unrecognized module type (%s) for %s" % \
268+
(`type`, name)
269+
finally:
270+
if file: file.close()
268271
m.__file__ = filename
269272
return m
270273

0 commit comments

Comments
 (0)