Control: retitle -1 ResourceWarnings in dh_python3 and py3versions because of 
unclosed files

On Tue, 18/09/2012 13:59 -0400, Barry Warsaw wrote:
> <...> i think there are better ways to do it, e.g. by using a context
> manager to handle the automatic closing of the
> files, instead of relying on potentially buggy explicit .closes()

Updated patch attached.

The test case for dh_python3 is building any package (that uses it) with
PYTHONWARNINGS=d (like I do it) :)

--
Dmitry Shachnev
Author: Dmitry Shachnev <mity...@gmail.com>
Description: Fix some ResourceWarnings caused by unclosed files
Bug: http://bugs.debian.org/686587
=== modified file 'debian/py3versions.py'
--- debian/py3versions.py	2012-06-30 19:15:05 +0000
+++ debian/py3versions.py	2012-09-19 15:59:31 +0000
@@ -179,8 +179,9 @@
     version = None
     sversion = None
     section = None
-    for line in open(fn, encoding='utf-8'):
-        line = line.strip()
+    with open(fn, encoding='utf-8') as controlfile:
+        lines = [line.strip() for line in controlfile]
+    for line in lines:
         if line == '':
             if pkg == 'Source':
                 break

=== modified file 'debpython/debhelper.py'
--- debpython/debhelper.py	2012-08-02 21:43:51 +0000
+++ debpython/debhelper.py	2012-09-19 15:55:54 +0000
@@ -102,7 +102,8 @@
             for when, templates in autoscripts.items():
                 fn = "debian/%s.%s.debhelper" % (package, when)
                 if exists(fn):
-                    data = open(fn, 'r').read()
+                    with open(fn, 'r') as datafile:
+                        data = datafile.read()
                 else:
                     data = ''
 
@@ -114,7 +115,8 @@
                                      "autoscripts/%s" % tpl_name)
                         if not exists(fpath):
                             fpath = "/usr/share/debhelper/autoscripts/%s" % tpl_name
-                        tpl = open(fpath, 'r').read()
+                        with open(fpath, 'r') as tplfile:
+                            tpl = tplfile.read()
                         if self.options.compile_all and args:
                             # TODO: should args be checked to contain dir name?
                             tpl = tpl.replace('#PACKAGE#', '')
@@ -137,7 +139,8 @@
                 continue
             fn = "debian/%s.substvars" % package
             if exists(fn):
-                data = open(fn, 'r').read()
+                with open(fn, 'r') as datafile:
+                    data = datafile.read()
             else:
                 data = ''
             for name, values in substvars.items():

Reply via email to