diff options
author | Benjamin Peterson <benjamin@python.org> | 2009-03-19 03:04:31 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2009-03-19 03:04:31 (GMT) |
commit | fcfa7ead4f66be4363f4e8a2e866c89d872eee22 (patch) | |
tree | 4305beac3be5c38a5d0d83ea82398fc4dd1a09c7 | |
parent | a61ae6922fbd0923c0a44bbed786d49333aeb002 (diff) | |
download | cpython-fcfa7ead4f66be4363f4e8a2e866c89d872eee22.zip cpython-fcfa7ead4f66be4363f4e8a2e866c89d872eee22.tar.gz cpython-fcfa7ead4f66be4363f4e8a2e866c89d872eee22.tar.bz2 |
close files after comparing them
-rw-r--r-- | Lib/filecmp.py | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/Lib/filecmp.py b/Lib/filecmp.py index 0b7ce16..a2266d8 100644 --- a/Lib/filecmp.py +++ b/Lib/filecmp.py @@ -11,6 +11,7 @@ Functions: import os import stat +import contextlib from itertools import ifilter, ifilterfalse, imap, izip __all__ = ["cmp","dircmp","cmpfiles"] @@ -62,15 +63,14 @@ def _sig(st): def _do_cmp(f1, f2): bufsize = BUFSIZE - fp1 = open(f1, 'rb') - fp2 = open(f2, 'rb') - while True: - b1 = fp1.read(bufsize) - b2 = fp2.read(bufsize) - if b1 != b2: - return False - if not b1: - return True + with contextlib.nested(open(f1, 'rb'), open(f2, 'rb')) as (fp1, fp2): + while True: + b1 = fp1.read(bufsize) + b2 = fp2.read(bufsize) + if b1 != b2: + return False + if not b1: + return True # Directory comparison class. # |