[PATCH 1 of 6] hgweb: use context manager for file I/O

Gregory Szorc gregory.szorc at gmail.com
Sat Apr 1 03:29:05 EDT 2017


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1491024638 25200
#      Fri Mar 31 22:30:38 2017 -0700
# Node ID f8b9edde1bf2a725d521cbe3deb00b33d088eb6e
# Parent  be7965e3afe82be35d258a2cff12b389a857ef88
hgweb: use context manager for file I/O

diff --git a/mercurial/hgweb/common.py b/mercurial/hgweb/common.py
--- a/mercurial/hgweb/common.py
+++ b/mercurial/hgweb/common.py
@@ -160,9 +160,9 @@ def staticfile(directory, fname, req):
     try:
         os.stat(path)
         ct = mimetypes.guess_type(path)[0] or "text/plain"
-        fp = open(path, 'rb')
-        data = fp.read()
-        fp.close()
+        with open(path, 'rb') as fh:
+            data = fh.read()
+
         req.respond(HTTP_OK, ct, body=data)
     except TypeError:
         raise ErrorResponse(HTTP_SERVER_ERROR, 'illegal filename')


More information about the Mercurial-devel mailing list