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

authorSylvain Thénault <sylvain.thenault@logilab.fr>
changesetf55cc5bc04d3
branchdefault
phasepublic
hiddenno
parent revision#48b033f2b81e Backport fix done by Benedikt Morbach on pylint for python3 install on win32 platforms / cross compilation. Closes #180836
child revision#14436aa69ef0 [modutils] fix support for extend_path based namespace package when module is nested in a sub-package. Closes #177651
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 f55cc5bc04d371e3e7cfa47fd6590835b94c81fd
# Parent 48b033f2b81e1ceb7eb08eadc902ab67f8c3eb39
[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 / cross
14       compilation (#180836)
15 
16  2013-07-26  --  0.60.0
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