]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Fix ResourceWarning: unclosed file 2566/head
authorMickaël Schoentgen <contact@tiger-222.fr>
Sat, 5 Jan 2019 21:15:01 +0000 (22:15 +0100)
committerMickaël Schoentgen <contact@tiger-222.fr>
Sat, 5 Jan 2019 21:15:01 +0000 (22:15 +0100)
Signed-off-by: Mickaël Schoentgen <contact@tiger-222.fr>
demos/s3server/s3server.py
tornado/locale.py

index 9b1e418aeff3c8892d7b65e49b7a4bdcb10592b8..11be1c2c2474a4c7ee9102c0113b74cd671cb179 100644 (file)
@@ -233,11 +233,8 @@ class ObjectHandler(BaseRequestHandler):
         self.set_header(
             "Last-Modified", datetime.datetime.utcfromtimestamp(info.st_mtime)
         )
-        object_file = open(path, "rb")
-        try:
+        with open(path, "rb") as object_file:
             self.finish(object_file.read())
-        finally:
-            object_file.close()
 
     def put(self, bucket, object_name):
         object_name = urllib.unquote(object_name)
@@ -252,9 +249,8 @@ class ObjectHandler(BaseRequestHandler):
         directory = os.path.dirname(path)
         if not os.path.exists(directory):
             os.makedirs(directory)
-        object_file = open(path, "w")
-        object_file.write(self.request.body)
-        object_file.close()
+        with open(path, "w") as object_file:
+            object_file.write(self.request.body)
         self.finish()
 
     def delete(self, bucket, object_name):
index 85b0b2f08ee7bbd91ff550da94d55037490293d1..b72d81bc7f5eb64d097c12585fae572e92b5a96c 100644 (file)
@@ -151,27 +151,26 @@ def load_translations(directory: str, encoding: str = None) -> None:
                 encoding = "utf-8-sig"
         # python 3: csv.reader requires a file open in text mode.
         # Specify an encoding to avoid dependence on $LANG environment variable.
-        f = open(full_path, "r", encoding=encoding)
-        _translations[locale] = {}
-        for i, row in enumerate(csv.reader(f)):
-            if not row or len(row) < 2:
-                continue
-            row = [escape.to_unicode(c).strip() for c in row]
-            english, translation = row[:2]
-            if len(row) > 2:
-                plural = row[2] or "unknown"
-            else:
-                plural = "unknown"
-            if plural not in ("plural", "singular", "unknown"):
-                gen_log.error(
-                    "Unrecognized plural indicator %r in %s line %d",
-                    plural,
-                    path,
-                    i + 1,
-                )
-                continue
-            _translations[locale].setdefault(plural, {})[english] = translation
-        f.close()
+        with open(full_path, encoding=encoding) as f:
+            _translations[locale] = {}
+            for i, row in enumerate(csv.reader(f)):
+                if not row or len(row) < 2:
+                    continue
+                row = [escape.to_unicode(c).strip() for c in row]
+                english, translation = row[:2]
+                if len(row) > 2:
+                    plural = row[2] or "unknown"
+                else:
+                    plural = "unknown"
+                if plural not in ("plural", "singular", "unknown"):
+                    gen_log.error(
+                        "Unrecognized plural indicator %r in %s line %d",
+                        plural,
+                        path,
+                        i + 1,
+                    )
+                    continue
+                _translations[locale].setdefault(plural, {})[english] = translation
     _supported_locales = frozenset(list(_translations.keys()) + [_default_locale])
     gen_log.debug("Supported locales: %s", sorted(_supported_locales))