8000 Merge pull request #1834 from mgiuca-google/finance-cachename · ivanov/matplotlib@0c417e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c417e0

Browse files
committed
Merge pull request matplotlib#1834 from mgiuca-google/finance-cachename
finance: Fixed making directories for explicit cachename
2 parents 2b2fa46 + 87dff9c commit 0c417e0

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

lib/matplotlib/finance.py

Lines changed: 5 additions & 7 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:
@@ -184,12 +184,10 @@ def fetch_historical_yahoo(ticker, date1, date2, cachename=None,dividends=False)
184184
fh = open(cachename)
185185
verbose.report('Using cachefile %s for %s'%(cachename, ticker))
186186
else:
187-
mkdirs(cachedir)
188-
urlfh = urlopen(url)
189-
190-
fh = open(cachename, 'wb')
191-
fh.write(urlfh.read())
192-
fh.close()
187+
mkdirs(os.path.abspath(os.path.dirname(cachename)))
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