Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/hgweb/common.py @ 31792:161a87ed456e
hgweb: use context manager for file I/O
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Fri, 31 Mar 2017 22:30:38 -0700 |
parents | 2daeab02b4b1 |
children | 62f9679df1f2 |
comparison
equal
deleted
inserted
replaced
31791:417363736c11 | 31792:161a87ed456e |
---|---|
158 if os.path.exists(path): | 158 if os.path.exists(path): |
159 break | 159 break |
160 try: | 160 try: |
161 os.stat(path) | 161 os.stat(path) |
162 ct = mimetypes.guess_type(path)[0] or "text/plain" | 162 ct = mimetypes.guess_type(path)[0] or "text/plain" |
163 fp = open(path, 'rb') | 163 with open(path, 'rb') as fh: |
164 data = fp.read() | 164 data = fh.read() |
165 fp.close() | 165 |
166 req.respond(HTTP_OK, ct, body=data) | 166 req.respond(HTTP_OK, ct, body=data) |
167 except TypeError: | 167 except TypeError: |
168 raise ErrorResponse(HTTP_SERVER_ERROR, 'illegal filename') | 168 raise ErrorResponse(HTTP_SERVER_ERROR, 'illegal filename') |
169 except OSError as err: | 169 except OSError as err: |
170 if err.errno == errno.ENOENT: | 170 if err.errno == errno.ENOENT: |