Skip to content

Commit

Permalink
Fix ResourceWarning: unclosed file
Browse files Browse the repository at this point in the history
Signed-off-by: Mickaël Schoentgen <[email protected]>
  • Loading branch information
BoboTiG committed Jan 5, 2019
1 parent fc6dd23 commit 3ee09d1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
10 changes: 3 additions & 7 deletions demos/s3server/s3server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down
41 changes: 20 additions & 21 deletions tornado/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit 3ee09d1

Please sign in to comment.