From 5a24743e55acedcbae06b97401c6e39b0d5faa98 Mon Sep 17 00:00:00 2001
From: Zac Medico <zmedico@gentoo.org>
Date: Thu, 25 Aug 2011 14:48:45 -0700
Subject: [PATCH] python3.2 fixes: ResourceWarning: unclosed file

---
 bin/repoman                                      | 9 +++++++--
 pym/_emerge/EbuildPhase.py                       | 7 +++++--
 pym/_emerge/PollScheduler.py                     | 3 +++
 pym/_emerge/SpawnProcess.py                      | 9 ++++++++-
 pym/portage/elog/messages.py                     | 6 ++++--
 pym/portage/package/ebuild/prepare_build_dirs.py | 4 ++++
 pym/portage/util/ExtractKernelVersion.py         | 2 ++
 7 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/bin/repoman b/bin/repoman
index 10f603ea2..e0494d95e 100755
--- a/bin/repoman
+++ b/bin/repoman
@@ -1228,11 +1228,13 @@ for x in scanlist:
 
 		if not (y in ("ChangeLog", "metadata.xml") or y.endswith(".ebuild")):
 			continue
+		f = None
 		try:
 			line = 1
-			for l in io.open(_unicode_encode(os.path.join(checkdir, y),
+			f = io.open(_unicode_encode(os.path.join(checkdir, y),
 				encoding=_encodings['fs'], errors='strict'),
-				mode='r', encoding=_encodings['repo.content']):
+				mode='r', encoding=_encodings['repo.content'])
+			for l in f:
 				line +=1
 		except UnicodeDecodeError as ue:
 			stats["file.UTF8"] += 1
@@ -1242,6 +1244,9 @@ for x in scanlist:
 			if l2 != 0:
 				s = s[s.rfind("\n") + 1:]
 			fails["file.UTF8"].append("%s/%s: line %i, just after: '%s'" % (checkdir, y, line, s))
+		finally:
+			if f is not None:
+				f.close()
 
 	if vcs in ("git", "hg") and check_ebuild_notadded:
 		if vcs == "git":
diff --git a/pym/_emerge/EbuildPhase.py b/pym/_emerge/EbuildPhase.py
index f53570a5b..ed0be0b0e 100644
--- a/pym/_emerge/EbuildPhase.py
+++ b/pym/_emerge/EbuildPhase.py
@@ -273,13 +273,15 @@ class EbuildPhase(CompositeTask):
 		temp_file = open(_unicode_encode(temp_log,
 			encoding=_encodings['fs'], errors='strict'), 'rb')
 
-		log_file = self._open_log(log_path)
+		log_file, log_file_real = self._open_log(log_path)
 
 		for line in temp_file:
 			log_file.write(line)
 
 		temp_file.close()
 		log_file.close()
+		if log_file_real is not log_file:
+			log_file_real.close()
 		os.unlink(temp_log)
 
 	def _open_log(self, log_path):
@@ -287,11 +289,12 @@ class EbuildPhase(CompositeTask):
 		f = open(_unicode_encode(log_path,
 			encoding=_encodings['fs'], errors='strict'),
 			mode='ab')
+		f_real = f
 
 		if log_path.endswith('.gz'):
 			f =  gzip.GzipFile(filename='', mode='ab', fileobj=f)
 
-		return f
+		return (f, f_real)
 
 	def _die_hooks(self):
 		self.returncode = None
diff --git a/pym/_emerge/PollScheduler.py b/pym/_emerge/PollScheduler.py
index a2b5c2466..fd9dfc0af 100644
--- a/pym/_emerge/PollScheduler.py
+++ b/pym/_emerge/PollScheduler.py
@@ -333,6 +333,7 @@ class PollScheduler(object):
 				f = open(_unicode_encode(log_path,
 					encoding=_encodings['fs'], errors='strict'),
 					mode='ab')
+				f_real = f
 			except IOError as e:
 				if e.errno not in (errno.ENOENT, errno.ESTALE):
 					raise
@@ -349,6 +350,8 @@ class PollScheduler(object):
 
 				f.write(_unicode_encode(msg))
 				f.close()
+				if f_real is not f:
+					f_real.close()
 
 _can_poll_device = None
 
diff --git a/pym/_emerge/SpawnProcess.py b/pym/_emerge/SpawnProcess.py
index 099407eda..065146c75 100644
--- a/pym/_emerge/SpawnProcess.py
+++ b/pym/_emerge/SpawnProcess.py
@@ -26,7 +26,7 @@ class SpawnProcess(SubProcess):
 		"path_lookup", "pre_exec")
 
 	__slots__ = ("args",) + \
-		_spawn_kwarg_names + ("_selinux_type",)
+		_spawn_kwarg_names + ("_log_file_real", "_selinux_type",)
 
 	_file_names = ("log", "process", "stdout")
 	_files_dict = slot_dict_class(_file_names, prefix="")
@@ -84,6 +84,7 @@ class SpawnProcess(SubProcess):
 			files.log = open(_unicode_encode(logfile,
 				encoding=_encodings['fs'], errors='strict'), mode='ab')
 			if logfile.endswith('.gz'):
+				self._log_file_real = files.log
 				files.log = gzip.GzipFile(filename='', mode='ab',
 					fileobj=files.log)
 
@@ -238,3 +239,9 @@ class SpawnProcess(SubProcess):
 
 		self._unregister_if_appropriate(event)
 
+	def _unregister(self):
+		super(SpawnProcess, self)._unregister()
+		if self._log_file_real is not None:
+			# Avoid "ResourceWarning: unclosed file" since python 3.2.
+			self._log_file_real.close()
+			self._log_file_real = None
diff --git a/pym/portage/elog/messages.py b/pym/portage/elog/messages.py
index 6c1580a37..50e0ab10a 100644
--- a/pym/portage/elog/messages.py
+++ b/pym/portage/elog/messages.py
@@ -43,9 +43,10 @@ def collect_ebuild_messages(path):
 			logentries[msgfunction] = []
 		lastmsgtype = None
 		msgcontent = []
-		for l in io.open(_unicode_encode(filename,
+		f = io.open(_unicode_encode(filename,
 			encoding=_encodings['fs'], errors='strict'),
-			mode='r', encoding=_encodings['repo.content'], errors='replace'):
+			mode='r', encoding=_encodings['repo.content'], errors='replace')
+		for l in f:
 			if not l:
 				continue
 			try:
@@ -65,6 +66,7 @@ def collect_ebuild_messages(path):
 					logentries[msgfunction].append((lastmsgtype, msgcontent))
 				msgcontent = [msg]
 			lastmsgtype = msgtype
+		f.close()
 		if msgcontent:
 			logentries[msgfunction].append((lastmsgtype, msgcontent))
 
diff --git a/pym/portage/package/ebuild/prepare_build_dirs.py b/pym/portage/package/ebuild/prepare_build_dirs.py
index 12c80ed29..6de486acf 100644
--- a/pym/portage/package/ebuild/prepare_build_dirs.py
+++ b/pym/portage/package/ebuild/prepare_build_dirs.py
@@ -119,11 +119,13 @@ def _adjust_perms_msg(settings, msg):
 	background = settings.get("PORTAGE_BACKGROUND") == "1"
 	log_path = settings.get("PORTAGE_LOG_FILE")
 	log_file = None
+	log_file_real = None
 
 	if background and log_path is not None:
 		try:
 			log_file = open(_unicode_encode(log_path,
 				encoding=_encodings['fs'], errors='strict'), mode='ab')
+			log_file_real = log_file
 		except IOError:
 			def write(msg):
 				pass
@@ -140,6 +142,8 @@ def _adjust_perms_msg(settings, msg):
 	finally:
 		if log_file is not None:
 			log_file.close()
+			if log_file_real is not log_file:
+				log_file_real.close()
 
 def _prepare_features_dirs(mysettings):
 
diff --git a/pym/portage/util/ExtractKernelVersion.py b/pym/portage/util/ExtractKernelVersion.py
index 5cb9747e6..49957f5fb 100644
--- a/pym/portage/util/ExtractKernelVersion.py
+++ b/pym/portage/util/ExtractKernelVersion.py
@@ -37,6 +37,8 @@ def ExtractKernelVersion(base_dir):
 		return (None, str(details))
 	except IOError as details:
 		return (None, str(details))
+	finally:
+		f.close()
 
 	lines = [l.strip() for l in lines]
 
-- 
2.26.2