Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/sphinx-doc/sphinx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2016-05-25 18:27:07 +0300
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2016-05-25 18:50:57 +0300
commit698a715d0c905079b215034d04cb3f1d8e809dd5 (patch)
treeb78c56a74ce3960214c6e51d775846c09e0d288f /sphinx/util/png.py
parentebc888d709ec8812bf6e158f919f6da23d38595c (diff)
Refactor code using ``with`` syntax
Diffstat (limited to 'sphinx/util/png.py')
-rw-r--r--sphinx/util/png.py15
1 files changed, 4 insertions, 11 deletions
diff --git a/sphinx/util/png.py b/sphinx/util/png.py
index e28445a42..476d45ccd 100644
--- a/sphinx/util/png.py
+++ b/sphinx/util/png.py
@@ -23,18 +23,14 @@ IEND_CHUNK = b'\x00\x00\x00\x00IEND\xAE\x42\x60\x82'
def read_png_depth(filename):
"""Read the special tEXt chunk indicating the depth from a PNG file."""
- result = None
- f = open(filename, 'rb')
- try:
+ with open(filename, 'rb') as f:
f.seek(- (LEN_IEND + LEN_DEPTH), 2)
depthchunk = f.read(LEN_DEPTH)
if not depthchunk.startswith(DEPTH_CHUNK_LEN + DEPTH_CHUNK_START):
# either not a PNG file or not containing the depth chunk
return None
- result = struct.unpack('!i', depthchunk[14:18])[0]
- finally:
- f.close()
- return result
+ else:
+ return struct.unpack('!i', depthchunk[14:18])[0]
def write_png_depth(filename, depth):
@@ -43,8 +39,7 @@ def write_png_depth(filename, depth):
The chunk is placed immediately before the special IEND chunk.
"""
data = struct.pack('!i', depth)
- f = open(filename, 'r+b')
- try:
+ with open(filename, 'r+b') as f:
# seek to the beginning of the IEND chunk
f.seek(-LEN_IEND, 2)
# overwrite it with the depth chunk
@@ -54,5 +49,3 @@ def write_png_depth(filename, depth):
f.write(struct.pack('!I', crc))
# replace the IEND chunk
f.write(IEND_CHUNK)
- finally:
- f.close()