8000 finance: Use context manager to close files. · ivanov/matplotlib@87dff9c · GitHub
[go: up one dir, main page]

Skip to content

Commit 87dff9c

Browse files
committed
finance: Use context manager to close files.
Previously, the urllib object opened in fetch_historical_yahoo was not explicitly closed.
1 parent ae3195f commit 87dff9c

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

lib/matplotlib/finance.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
"""
66
from __future__ import division, print_function
7-
import os, sys, warnings
7+
import contextlib, os, sys, warnings
88
from urllib2 import urlopen
99

1010
if sys.version_info[0] < 3:
@@ -185,11 +185,9 @@ def fetch_historical_yahoo(ticker, date1, date2, cachename=None,dividends=False)
185185
verbose.report('Using cachefile %s for %s'%(cachename, ticker))
186186
else:
187187
mkdirs(os.path.abspath(os.path.dirname(cachename)))
188-
urlfh = urlopen(url)
189-
190-
fh = open(cachename, 'wb')
191-
fh.write(urlfh.read())
192-
fh.close()
188+
with contextlib.closing(urlopen(url)) as urlfh:
189+
with open(cachename, 'wb') as fh:
190+
fh.write(urlfh.read())
193191
verbose.report('Saved %s data to cache file %s'%(ticker, cachename))
194192
fh = open(cachename, 'r')
195193

0 commit comments

Comments
 (0)
0