Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 87dff9c

Browse filesBrowse 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
Copy full SHA for 87dff9c

File tree

Expand file treeCollapse file tree

1 file changed

+4
-6
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+4
-6
lines changed

‎lib/matplotlib/finance.py

Copy file name to clipboardExpand all lines: lib/matplotlib/finance.py
+4-6Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.