[Checkins] SVN: zope.configuration/trunk/src/zope/configuration/ Avoid resource warnings for unclosed files under Py3k.

Tres Seaver cvs-admin at zope.org
Fri May 11 00:44:39 UTC 2012


Log message for revision 125838:
  Avoid resource warnings for unclosed files under Py3k.

Changed:
  U   zope.configuration/trunk/src/zope/configuration/tests/test_xmlconfig.py
  U   zope.configuration/trunk/src/zope/configuration/xmlconfig.py

-=-
Modified: zope.configuration/trunk/src/zope/configuration/tests/test_xmlconfig.py
===================================================================
--- zope.configuration/trunk/src/zope/configuration/tests/test_xmlconfig.py	2012-05-11 00:08:53 UTC (rev 125837)
+++ zope.configuration/trunk/src/zope/configuration/tests/test_xmlconfig.py	2012-05-11 00:44:36 UTC (rev 125838)
@@ -435,13 +435,13 @@
 
     def test_file_present(self):
         import os
-        fp = self._callFUT(self._makeFilename('configure.zcml'))
-        self.assertEqual(os.path.basename(fp.name), 'configure.zcml')
+        with self._callFUT(self._makeFilename('configure.zcml')) as fp:
+            self.assertEqual(os.path.basename(fp.name), 'configure.zcml')
 
     def test_file_missing_but_dot_in_present(self):
         import os
-        fp = self._callFUT(self._makeFilename('foo.zcml'))
-        self.assertEqual(os.path.basename(fp.name), 'foo.zcml.in')
+        with self._callFUT(self._makeFilename('foo.zcml')) as fp:
+            self.assertEqual(os.path.basename(fp.name), 'foo.zcml.in')
 
     def test_file_missing_and_dot_in_not_present(self):
         import errno
@@ -779,7 +779,8 @@
         from zope.configuration._compat import b
         from zope.configuration.tests.samplepackage import foo
         file_name = path("samplepackage", "configure.zcml")
-        xml = open(file_name).read()
+        with open(file_name) as f:
+            xml = f.read()
         context = self._callFUT(xml, execute=False)
         self.assertEqual(len(foo.data), 0)
         self.assertEqual(len(context.actions), 1)
@@ -795,7 +796,8 @@
         context = ConfigurationMachine()
         registerCommonDirectives(context)
         file_name = path("samplepackage", "configure.zcml")
-        xml = open(file_name).read()
+        with open(file_name) as f:
+            xml = f.read()
         ret = self._callFUT(xml, context=context, execute=False)
         self.assertTrue(ret is context)
         self.assertEqual(len(foo.data), 0)
@@ -808,7 +810,8 @@
         from zope.configuration._compat import b
         from zope.configuration.tests.samplepackage import foo
         file_name = path("samplepackage", "configure.zcml")
-        xml = open(file_name).read()
+        with open(file_name) as f:
+            xml = f.read()
         context = self._callFUT(xml)
         data = foo.data.pop()
         self.assertEqual(data.args, (('x', b('blah')), ('y', 0)))

Modified: zope.configuration/trunk/src/zope/configuration/xmlconfig.py
===================================================================
--- zope.configuration/trunk/src/zope/configuration/xmlconfig.py	2012-05-11 00:08:53 UTC (rev 125837)
+++ zope.configuration/trunk/src/zope/configuration/xmlconfig.py	2012-05-11 00:44:36 UTC (rev 125838)
@@ -116,11 +116,11 @@
                                 'tests', 'sample.zcml')
 
         try:
-            f = open(file)
+            with open(file) as f:
+                lines = f.readlines()[self.line-1:self.eline]
         except IOError:
             src = "  Could not read source."
         else:
-            lines = f.readlines()[self.line-1:self.eline]
             ecolumn = self.ecolumn
             if lines[-1][ecolumn:ecolumn+2] == '</': #pragma NO COVER
                 # We're pointing to the start of an end tag. Try to find
@@ -392,15 +392,14 @@
 
     for path in paths:
         if context.processFile(path):
-            f = openInOrPlain(path)
-            logger.debug("include %s" % f.name)
+            with openInOrPlain(path) as f:
+                logger.debug("include %s" % f.name)
 
-            context.basepath = os.path.dirname(path)
-            context.includepath = _context.includepath + (f.name, )
-            _context.stack.append(GroupingStackItem(context))
+                context.basepath = os.path.dirname(path)
+                context.includepath = _context.includepath + (f.name, )
+                _context.stack.append(GroupingStackItem(context))
 
-            processxmlfile(f, context)
-            f.close()
+                processxmlfile(f, context)
             assert _context.stack[-1].context is context
             _context.stack.pop()
 



More information about the checkins mailing list