[modutils] ensure file is closed, may cause pb depending on the interpreter (eg pypy). Closes #180876

authorSylvain Thénault <sylvain.thenault@logilab.fr>
changeset92d09fece70e
branchdefault
phasedraft
hiddenyes
parent revision#ab88a0a73cfa Backport fix done by Tobias Rzepka on pylint for python3 install on win32 platforms. Closes #180836
child revision<not specified>
files modified by this revision
ChangeLog
modutils.py
# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1380814303 -7200
# Thu Oct 03 17:31:43 2013 +0200
# Node ID 92d09fece70e5f38a1d09b51912d90941ee38c78
# Parent ab88a0a73cfa018be12c925737cc5d8b63b08f7e
[modutils] ensure file is closed, may cause pb depending on the interpreter (eg pypy). Closes #180876

diff --git a/ChangeLog b/ChangeLog
@@ -1,11 +1,14 @@
1  ChangeLog for logilab.common
2  ============================
3 
4  --
5 -   * modutils: don't propagate IOError when package's __init__.py file doesn't
6 -     exist (#174606)
7 +   * modutils: 
8 +     * don't propagate IOError when package's __init__.py file doesn't
9 +       exist (#174606)
10 +     * ensure file is closed, may cause pb depending on the interpreter, eg
11 +       pypy) (#180876)
12 
13     * fix some cases of failing python3 install on windows platform (#180836)
14 
15  2013-07-26  --  0.60.0
16      * configuration: rename option_name method into option_attrname (#140667)
diff --git a/modutils.py b/modutils.py
@@ -25,10 +25,12 @@
17  :var STD_LIB_DIR: directory where standard modules are located
18 
19  :type BUILTIN_MODULES: dict
20  :var BUILTIN_MODULES: dictionary with builtin module names has key
21  """
22 +from __future__ import with_statement
23 +
24  __docformat__ = "restructuredtext en"
25 
26  import sys
27  import os
28  from os.path import splitext, join, abspath, isdir, dirname, exists, basename
@@ -655,11 +657,12 @@
29                  raise ImportError('No module %s in %s' % ('.'.join(modpath),
30                                                            '.'.join(imported)))
31              # XXX guess if package is using pkgutil.extend_path by looking for
32              # those keywords in the first four Kbytes
33              try:
34 -                data = open(join(mp_filename, '__init__.py')).read(4096)
35 +                with open(join(mp_filename, '__init__.py')) as stream:
36 +                    data = stream.read(4096)
37              except IOError:
38                  path = [mp_filename]
39              else:
40                  if 'pkgutil' in data and 'extend_path' in data:
41                      # extend_path is called, search sys.path for module/packages