[Python-checkins] r86240 - python/branches/py3k/Lib/test/test_gettext.py

eric.araujo python-checkins at python.org
Sat Nov 6 05:11:59 CET 2010


Author: eric.araujo
Date: Sat Nov  6 05:11:59 2010
New Revision: 86240

Log:
Prevent ResourceWarnings in test_gettext


Modified:
   python/branches/py3k/Lib/test/test_gettext.py

Modified: python/branches/py3k/Lib/test/test_gettext.py
==============================================================================
--- python/branches/py3k/Lib/test/test_gettext.py	(original)
+++ python/branches/py3k/Lib/test/test_gettext.py	Sat Nov  6 05:11:59 2010
@@ -64,15 +64,12 @@
     def setUp(self):
         if not os.path.isdir(LOCALEDIR):
             os.makedirs(LOCALEDIR)
-        fp = open(MOFILE, 'wb')
-        fp.write(base64.decodebytes(GNU_MO_DATA))
-        fp.close()
-        fp = open(UMOFILE, 'wb')
-        fp.write(base64.decodebytes(UMO_DATA))
-        fp.close()
-        fp = open(MMOFILE, 'wb')
-        fp.write(base64.decodebytes(MMO_DATA))
-        fp.close()
+        with open(MOFILE, 'wb') as fp:
+            fp.write(base64.decodebytes(GNU_MO_DATA))
+        with open(UMOFILE, 'wb') as fp:
+            fp.write(base64.decodebytes(UMO_DATA))
+        with open(MMOFILE, 'wb') as fp:
+            fp.write(base64.decodebytes(MMO_DATA))
         self.env = support.EnvironmentVarGuard()
         self.env['LANGUAGE'] = 'xx'
         gettext._translations.clear()
@@ -135,9 +132,8 @@
     def test_the_alternative_interface(self):
         eq = self.assertEqual
         # test the alternative interface
-        fp = open(self.mofile, 'rb')
-        t = gettext.GNUTranslations(fp)
-        fp.close()
+        with open(self.mofile, 'rb') as fp:
+            t = gettext.GNUTranslations(fp)
         # Install the translation object
         t.install()
         eq(_('nudge nudge'), 'wink wink')
@@ -227,9 +223,8 @@
 
     def test_plural_forms2(self):
         eq = self.assertEqual
-        fp = open(self.mofile, 'rb')
-        t = gettext.GNUTranslations(fp)
-        fp.close()
+        with open(self.mofile, 'rb') as fp:
+            t = gettext.GNUTranslations(fp)
         x = t.ngettext('There is %s file', 'There are %s files', 1)
         eq(x, 'Hay %s fichero')
         x = t.ngettext('There is %s file', 'There are %s files', 2)
@@ -299,11 +294,8 @@
 class UnicodeTranslationsTest(GettextBaseTest):
     def setUp(self):
         GettextBaseTest.setUp(self)
-        fp = open(UMOFILE, 'rb')
-        try:
+        with open(UMOFILE, 'rb') as fp:
             self.t = gettext.GNUTranslations(fp)
-        finally:
-            fp.close()
         self._ = self.t.gettext
 
     def test_unicode_msgid(self):
@@ -319,15 +311,12 @@
 class WeirdMetadataTest(GettextBaseTest):
     def setUp(self):
         GettextBaseTest.setUp(self)
-        fp = open(MMOFILE, 'rb')
-        try:
+        with open(MMOFILE, 'rb') as fp:
             try:
                 self.t = gettext.GNUTranslations(fp)
             except:
                 self.tearDown()
                 raise
-        finally:
-            fp.close()
 
     def test_weird_metadata(self):
         info = self.t.info()


More information about the Python-checkins mailing list