From 3ee09d195618e54643fe3ff4e0e06fe66eb18d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Sat, 5 Jan 2019 22:15:01 +0100 Subject: [PATCH] Fix ResourceWarning: unclosed file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mickaƫl Schoentgen --- demos/s3server/s3server.py | 10 +++------- tornado/locale.py | 41 +++++++++++++++++++------------------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/demos/s3server/s3server.py b/demos/s3server/s3server.py index 9b1e418aef..11be1c2c24 100644 --- a/demos/s3server/s3server.py +++ b/demos/s3server/s3server.py @@ -233,11 +233,8 @@ def get(self, bucket, object_name): 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 @@ def put(self, bucket, object_name): 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): diff --git a/tornado/locale.py b/tornado/locale.py index 85b0b2f08e..b72d81bc7f 100644 --- a/tornado/locale.py +++ b/tornado/locale.py @@ -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))