From 2f550d8eb3183b76de7b33df2e9c649cc9b29dda Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Wed, 24 Aug 2011 20:29:04 -0700 Subject: [PATCH] python3.2 fixes: ResourceWarning: unclosed file --- pym/_emerge/Scheduler.py | 4 ++++ pym/portage/package/ebuild/doebuild.py | 24 ++++++++++++++----- .../package/ebuild/prepare_build_dirs.py | 2 +- pym/portage/update.py | 7 +++--- .../util/_dyn_libs/PreservedLibsRegistry.py | 10 +++++--- 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/pym/_emerge/Scheduler.py b/pym/_emerge/Scheduler.py index 6412d821d..6115f21e7 100644 --- a/pym/_emerge/Scheduler.py +++ b/pym/_emerge/Scheduler.py @@ -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. diff --git a/pym/portage/package/ebuild/doebuild.py b/pym/portage/package/ebuild/doebuild.py index a710e09e5..aa9218c69 100644 --- a/pym/portage/package/ebuild/doebuild.py +++ b/pym/portage/package/ebuild/doebuild.py @@ -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) diff --git a/pym/portage/package/ebuild/prepare_build_dirs.py b/pym/portage/package/ebuild/prepare_build_dirs.py index 9104d0e63..12c80ed29 100644 --- a/pym/portage/package/ebuild/prepare_build_dirs.py +++ b/pym/portage/package/ebuild/prepare_build_dirs.py @@ -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') diff --git a/pym/portage/update.py b/pym/portage/update.py index 52ab50645..548d19cef 100644 --- a/pym/portage/update.py +++ b/pym/portage/update.py @@ -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 diff --git a/pym/portage/util/_dyn_libs/PreservedLibsRegistry.py b/pym/portage/util/_dyn_libs/PreservedLibsRegistry.py index 602cf870a..b916051a1 100644 --- a/pym/portage/util/_dyn_libs/PreservedLibsRegistry.py +++ b/pym/portage/util/_dyn_libs/PreservedLibsRegistry.py @@ -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() -- 2.26.2