python3.2 fixes: ResourceWarning: unclosed file
authorZac Medico <zmedico@gentoo.org>
Thu, 25 Aug 2011 03:29:04 +0000 (20:29 -0700)
committerZac Medico <zmedico@gentoo.org>
Thu, 25 Aug 2011 03:29:04 +0000 (20:29 -0700)
pym/_emerge/Scheduler.py
pym/portage/package/ebuild/doebuild.py
pym/portage/package/ebuild/prepare_build_dirs.py
pym/portage/update.py
pym/portage/util/_dyn_libs/PreservedLibsRegistry.py

index 6412d821db53bbf943ad047e0808b3a206f92cb2..6115f21e7048db37021c93e3a7da31beb88aab78 100644 (file)
@@ -1133,6 +1133,7 @@ class Scheduler(PollScheduler):
                        failed_pkg = self._failed_pkgs_all[-1]
                        build_dir = failed_pkg.build_dir
                        log_file = None
+                       log_file_real = None
 
                        log_paths = [failed_pkg.build_log]
 
@@ -1145,6 +1146,7 @@ class Scheduler(PollScheduler):
                                        pass
                                else:
                                        if log_path.endswith('.gz'):
+                                               log_file_real = log_file
                                                log_file =  gzip.GzipFile(filename='',
                                                        mode='rb', fileobj=log_file)
 
@@ -1157,6 +1159,8 @@ class Scheduler(PollScheduler):
                                                noiselevel=-1)
                                finally:
                                        log_file.close()
+                                       if log_file_real is not None:
+                                               log_file_real.close()
                                failure_log_shown = True
 
                # Dump mod_echo output now since it tends to flood the terminal.
index a710e09e594c939b74a384cb0290acbff6ca2011..aa9218c69c631ca5a3a981bd89dd8290fe62e261 100644 (file)
@@ -1335,7 +1335,9 @@ def _check_build_log(mysettings, out=None):
        except EnvironmentError:
                return
 
+       f_real = None
        if logfile.endswith('.gz'):
+               f_real = f
                f =  gzip.GzipFile(filename='', mode='rb', fileobj=f)
 
        am_maintainer_mode = []
@@ -1440,6 +1442,10 @@ def _check_build_log(mysettings, out=None):
                msg.extend("\t" + line for line in make_jobserver)
                _eqawarn(msg)
 
+       f.close()
+       if f_real is not None:
+               f_real.close()
+
 def _post_src_install_chost_fix(settings):
        """
        It's possible that the ebuild has changed the
@@ -1617,15 +1623,19 @@ def _post_src_install_uid_fix(mysettings, out):
        build_info_dir = os.path.join(mysettings['PORTAGE_BUILDDIR'],
                'build-info')
 
-       io.open(_unicode_encode(os.path.join(build_info_dir,
+       f = io.open(_unicode_encode(os.path.join(build_info_dir,
                'SIZE'), encoding=_encodings['fs'], errors='strict'),
                mode='w', encoding=_encodings['repo.content'],
-               errors='strict').write(_unicode_decode(str(size) + '\n'))
+               errors='strict')
+       f.write(_unicode_decode(str(size) + '\n'))
+       f.close()
 
-       io.open(_unicode_encode(os.path.join(build_info_dir,
+       f = io.open(_unicode_encode(os.path.join(build_info_dir,
                'BUILD_TIME'), encoding=_encodings['fs'], errors='strict'),
                mode='w', encoding=_encodings['repo.content'],
-               errors='strict').write(_unicode_decode("%.0f\n" % (time.time(),)))
+               errors='strict')
+       f.write(_unicode_decode("%.0f\n" % (time.time(),)))
+       f.close()
 
        use = frozenset(mysettings['PORTAGE_USE'].split())
        for k in _vdb_use_conditional_keys:
@@ -1651,10 +1661,12 @@ def _post_src_install_uid_fix(mysettings, out):
                        except OSError:
                                pass
                        continue
-               io.open(_unicode_encode(os.path.join(build_info_dir,
+               f = io.open(_unicode_encode(os.path.join(build_info_dir,
                        k), encoding=_encodings['fs'], errors='strict'),
                        mode='w', encoding=_encodings['repo.content'],
-                       errors='strict').write(_unicode_decode(v + '\n'))
+                       errors='strict')
+               f.write(_unicode_decode(v + '\n'))
+               f.close()
 
        _reapply_bsdflags_to_image(mysettings)
 
index 9104d0e63955321ba9a52b5c06918e21f23cdcf6..12c80ed293499b9f8c3c67b61b5ea2fb06e1926a 100644 (file)
@@ -312,7 +312,7 @@ def _prepare_workdir(mysettings):
                logdir = normalize_path(mysettings["PORT_LOGDIR"])
                logid_path = os.path.join(mysettings["PORTAGE_BUILDDIR"], ".logid")
                if not os.path.exists(logid_path):
-                       open(_unicode_encode(logid_path), 'w')
+                       open(_unicode_encode(logid_path), 'w').close()
                logid_time = _unicode_decode(time.strftime("%Y%m%d-%H%M%S",
                        time.gmtime(os.stat(logid_path).st_mtime)),
                        encoding=_encodings['content'], errors='replace')
index 52ab50645c5c7d88e5d9e749ec62548cc412024f..548d19cef268ba3c0ad345caf8628daf7182f593 100644 (file)
@@ -132,10 +132,11 @@ def grab_updates(updpath, prev_mtimes=None):
                if update_data or \
                        file_path not in prev_mtimes or \
                        long(prev_mtimes[file_path]) != mystat[stat.ST_MTIME]:
-                       content = io.open(_unicode_encode(file_path,
+                       f = io.open(_unicode_encode(file_path,
                                encoding=_encodings['fs'], errors='strict'),
-                               mode='r', encoding=_encodings['repo.content'], errors='replace'
-                               ).read()
+                               mode='r', encoding=_encodings['repo.content'], errors='replace')
+                       content = f.read()
+                       f.close()
                        update_data.append((file_path, mystat, content))
        return update_data
 
index 602cf870aee20d4b41e3b8ee7ca04b66449a19c9..b916051a1f5dfd4483f7f5fd30a2e7460c2baec1 100644 (file)
@@ -55,10 +55,11 @@ class PreservedLibsRegistry(object):
        def load(self):
                """ Reload the registry data from file """
                self._data = None
+               f = None
                try:
-                       self._data = pickle.load(
-                               open(_unicode_encode(self._filename,
-                                       encoding=_encodings['fs'], errors='strict'), 'rb'))
+                       f = open(_unicode_encode(self._filename,
+                                       encoding=_encodings['fs'], errors='strict'), 'rb')
+                       self._data = pickle.load(f)
                except (ValueError, pickle.UnpicklingError) as e:
                        writemsg_level(_("!!! Error loading '%s': %s\n") % \
                                (self._filename, e), level=logging.ERROR, noiselevel=-1)
@@ -69,6 +70,9 @@ class PreservedLibsRegistry(object):
                                raise PermissionDenied(self._filename)
                        else:
                                raise
+               finally:
+                       if f is not None:
+                               f.close()
                if self._data is None:
                        self._data = {}
                self._data_orig = self._data.copy()