← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/txpkgupload:py3-close-log-file into txpkgupload:master

 

Colin Watson has proposed merging ~cjwatson/txpkgupload:py3-close-log-file into txpkgupload:master.

Commit message:
Silence ResourceWarnings due to an open log file

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/txpkgupload/+git/txpkgupload/+merge/393262

We need to explicitly close the service's log file if we opened one.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/txpkgupload:py3-close-log-file into txpkgupload:master.
diff --git a/src/txpkgupload/services.py b/src/txpkgupload/services.py
index 89e9e4d..c8971b8 100644
--- a/src/txpkgupload/services.py
+++ b/src/txpkgupload/services.py
@@ -67,6 +67,7 @@ class PkgUploadServices(MultiService):
         self.oops_dir = oops_dir
         self.oops_reporter = oops_reporter
         self.server_argv = server_argv
+        self._log_file = None
 
     def _getLogObserver(self):
         # We unfortunately have to clone-and-hack part of
@@ -88,7 +89,7 @@ class PkgUploadServices(MultiService):
         else:
             if not logfilename:
                 logfilename = "txpkgupload.log"
-            logFile = LogFile.fromFullPath(
+            self._log_file = logFile = LogFile.fromFullPath(
                 logfilename, rotateLength=None, defaultMode=0o644)
             # Override if signal is set to None or SIG_DFL (0)
             if not signal.getsignal(signal.SIGUSR1):
@@ -115,3 +116,9 @@ class PkgUploadServices(MultiService):
             # Customise the application's logging.  We don't get much of an
             # opportunity to do this otherwise.
             parent.setComponent(log.ILogObserver, self._getLogObserver())
+
+    def disownServiceParent(self):
+        if self._log_file is not None:
+            self._log_file.close()
+            self._log_file = None
+        MultiService.disownServiceParent(self)
diff --git a/src/txpkgupload/tests/test_plugin.py b/src/txpkgupload/tests/test_plugin.py
index 162c289..619f830 100644
--- a/src/txpkgupload/tests/test_plugin.py
+++ b/src/txpkgupload/tests/test_plugin.py
@@ -230,6 +230,7 @@ class PkgUploadFixture(DeferringFixture):
         # runner would, and start the service.
         application = Application(self.service_maker.tapname)
         self.service.setServiceParent(application)
+        self.addCleanup(self.service.disownServiceParent)
         self.observer = application.getComponent(log.ILogObserver, None)
         log.addObserver(self.observer)
         self.addCleanup(log.removeObserver, self.observer)