summaryrefslogtreecommitdiff
path: root/Lib/filecmp.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/filecmp.py')
-rw-r--r--Lib/filecmp.py28
1 files changed, 14 insertions, 14 deletions
diff --git a/Lib/filecmp.py b/Lib/filecmp.py
index 0b7ce16dfe..4728317fce 100644
--- a/Lib/filecmp.py
+++ b/Lib/filecmp.py
@@ -48,11 +48,12 @@ def cmp(f1, f2, shallow=1):
if s1[1] != s2[1]:
return False
- result = _cache.get((f1, f2))
- if result and (s1, s2) == result[:2]:
- return result[2]
- outcome = _do_cmp(f1, f2)
- _cache[f1, f2] = s1, s2, outcome
+ outcome = _cache.get((f1, f2, s1, s2))
+ if outcome is None:
+ outcome = _do_cmp(f1, f2)
+ if len(_cache) > 100: # limit the maximum size of the cache
+ _cache.clear()
+ _cache[f1, f2, s1, s2] = outcome
return outcome
def _sig(st):
@@ -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 open(f1, 'rb') as fp1, open(f2, 'rb') as fp2:
+ while True:
+ b1 = fp1.read(bufsize)
+ b2 = fp2.read(bufsize)
+ if b1 != b2:
+ return False
+ if not b1:
+ return True
# Directory comparison class.
#