]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Handle file open/close with "with"
authorVille Skyttä <ville.skytta@iki.fi>
Tue, 26 Jul 2016 20:31:43 +0000 (23:31 +0300)
committerVille Skyttä <ville.skytta@iki.fi>
Tue, 26 Jul 2016 23:23:13 +0000 (02:23 +0300)
babel/core.py
babel/localedata.py
babel/messages/extract.py
babel/messages/frontend.py
scripts/download_import_cldr.py
scripts/dump_global.py
tests/messages/test_mofile.py

index e6f432c287f93b882fd5b71b5308348e64f65df7..0b7b4edd5b8de9621288fb210b38806477ede46d 100644 (file)
@@ -71,11 +71,8 @@ def get_global(key):
         filename = os.path.join(dirname, 'global.dat')
         if not os.path.isfile(filename):
             _raise_no_data_error()
-        fileobj = open(filename, 'rb')
-        try:
+        with open(filename, 'rb') as fileobj:
             _global_data = pickle.load(fileobj)
-        finally:
-            fileobj.close()
     return _global_data.get(key, {})
 
 
index 87981fe2a574e2c52b7ad687be4aebc657f76588..9e272a279dac20644b5ad5e84485b73adb2fc8d8 100644 (file)
@@ -106,14 +106,11 @@ def load(name, merge_inherited=True):
                         parent = '_'.join(parts[:-1])
                 data = load(parent).copy()
             filename = os.path.join(_dirname, '%s.dat' % name)
-            fileobj = open(filename, 'rb')
-            try:
+            with open(filename, 'rb') as fileobj:
                 if name != 'root' and merge_inherited:
                     merge(data, pickle.load(fileobj))
                 else:
                     data = pickle.load(fileobj)
-            finally:
-                fileobj.close()
             _cache[name] = data
         return data
     finally:
index db1784896e9a3258696b69f10f49324b0f98ebb5..1aade43f94f9a9113da1b916802df1f837485f6b 100644 (file)
@@ -230,12 +230,9 @@ def extract_from_file(method, filename, keywords=DEFAULT_KEYWORDS,
                                tags to be removed from the collected comments.
     :param options: a dictionary of additional options (optional)
     """
-    fileobj = open(filename, 'rb')
-    try:
+    with open(filename, 'rb') as fileobj:
         return list(extract(method, fileobj, keywords, comment_tags, options,
                             strip_comment_tags))
-    finally:
-        fileobj.close()
 
 
 def extract(method, fileobj, keywords=DEFAULT_KEYWORDS, comment_tags=(),
index a4e9c2e577105dd6af7d8e7d999e0482ac068f68..5a1c7417f6fde6de9fd145c0e0ca8cfe2044f964 100644 (file)
@@ -213,11 +213,8 @@ class compile_catalog(Command):
 
         for idx, (locale, po_file) in enumerate(po_files):
             mo_file = mo_files[idx]
-            infile = open(po_file, 'rb')
-            try:
+            with open(po_file, 'rb') as infile:
                 catalog = read_po(infile, locale)
-            finally:
-                infile.close()
 
             if self.statistics:
                 translated = 0
@@ -244,11 +241,8 @@ class compile_catalog(Command):
 
             self.log.info('compiling catalog %s to %s', po_file, mo_file)
 
-            outfile = open(mo_file, 'wb')
-            try:
+            with open(mo_file, 'wb') as outfile:
                 write_mo(outfile, catalog, use_fuzzy=self.use_fuzzy)
-            finally:
-                outfile.close()
 
 
 class extract_messages(Command):
@@ -471,11 +465,8 @@ class extract_messages(Command):
         mappings = []
 
         if self.mapping_file:
-            fileobj = open(self.mapping_file, 'U')
-            try:
+            with open(self.mapping_file, 'U') as fileobj:
                 method_map, options_map = parse_mapping(fileobj)
-            finally:
-                fileobj.close()
             for path in self.input_paths:
                 mappings.append((path, method_map, options_map))
 
@@ -591,23 +582,17 @@ class init_catalog(Command):
             'creating catalog %s based on %s', self.output_file, self.input_file
         )
 
-        infile = open(self.input_file, 'rb')
-        try:
+        with open(self.input_file, 'rb') as infile:
             # Although reading from the catalog template, read_po must be fed
             # the locale in order to correctly calculate plurals
             catalog = read_po(infile, locale=self.locale)
-        finally:
-            infile.close()
 
         catalog.locale = self._locale
         catalog.revision_date = datetime.now(LOCALTZ)
         catalog.fuzzy = False
 
-        outfile = open(self.output_file, 'wb')
-        try:
+        with open(self.output_file, 'wb') as outfile:
             write_po(outfile, catalog, width=self.width)
-        finally:
-            outfile.close()
 
 
 class update_catalog(Command):
@@ -709,22 +694,16 @@ class update_catalog(Command):
         if not domain:
             domain = os.path.splitext(os.path.basename(self.input_file))[0]
 
-        infile = open(self.input_file, 'rb')
-        try:
+        with open(self.input_file, 'rb') as infile:
             template = read_po(infile)
-        finally:
-            infile.close()
 
         if not po_files:
             raise DistutilsOptionError('no message catalogs found')
 
         for locale, filename in po_files:
             self.log.info('updating catalog %s based on %s', filename, self.input_file)
-            infile = open(filename, 'rb')
-            try:
+            with open(filename, 'rb') as infile:
                 catalog = read_po(infile, locale=locale, domain=domain)
-            finally:
-                infile.close()
 
             catalog.update(
                 template, self.no_fuzzy_matching,
@@ -734,14 +713,11 @@ class update_catalog(Command):
             tmpname = os.path.join(os.path.dirname(filename),
                                    tempfile.gettempprefix() +
                                    os.path.basename(filename))
-            tmpfile = open(tmpname, 'wb')
             try:
-                try:
+                with open(tmpname, 'wb') as tmpfile:
                     write_po(tmpfile, catalog,
                              ignore_obsolete=self.ignore_obsolete,
                              include_previous=self.previous, width=self.width)
-                finally:
-                    tmpfile.close()
             except:
                 os.remove(tmpname)
                 raise
index bfcab5f8b12a8143ae7c38c9da15ea03d18c8044..4cc6a4b9b601d7e9c3be3764c1a11e4ab07ebe45 100755 (executable)
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 
+import contextlib
 import os
 import sys
 import shutil
@@ -90,9 +91,8 @@ def main():
             shutil.rmtree(common_path)
 
         log('Extracting CLDR to \'%s\'', cldr_path)
-        z = zipfile.ZipFile(zip_path)
-        z.extractall(cldr_path)
-        z.close()
+        with contextlib.closing(zipfile.ZipFile(zip_path)) as z:
+            z.extractall(cldr_path)
 
     subprocess.check_call([
         sys.executable,
index ad6c15edd583e40c86d40ad11bd7494e7d22fe6b..2970bc2ba54808e3007cfde68995d279627b6a0e 100755 (executable)
@@ -21,11 +21,8 @@ import babel
 
 dirname = os.path.join(os.path.dirname(babel.__file__))
 filename = os.path.join(dirname, 'global.dat')
-fileobj = open(filename, 'rb')
-try:
+with open(filename, 'rb') as fileobj:
     data = pickle.load(fileobj)
-finally:
-    fileobj.close()
 
 if len(sys.argv) > 1:
     pprint(data.get(sys.argv[1]))
index d257c5d7361ffbe5f9e8b59e89efd4b52bf2f251..5fedc600a36f1b4f33bda6e8e997e931856c7653 100644 (file)
@@ -27,8 +27,7 @@ class ReadMoTestCase(unittest.TestCase):
     def test_basics(self):
         mo_path = os.path.join(self.datadir, 'project', 'i18n', 'de',
                                'LC_MESSAGES', 'messages.mo')
-        mo_file = open(mo_path, 'rb')
-        try:
+        with open(mo_path, 'rb') as mo_file:
             catalog = mofile.read_mo(mo_file)
             self.assertEqual(2, len(catalog))
             self.assertEqual('TestProject', catalog.project)
@@ -36,8 +35,6 @@ class ReadMoTestCase(unittest.TestCase):
             self.assertEqual('Stange', catalog['bar'].string)
             self.assertEqual(['Fuhstange', 'Fuhstangen'],
                              catalog['foobar'].string)
-        finally:
-            mo_file.close()
 
 
 class WriteMoTestCase(unittest.TestCase):