Skip to content

Commit ac89b71

Browse files
committed
TST: more data.py cleanup
1 parent 07403ca commit ac89b71

File tree

8 files changed

+364
-340
lines changed

8 files changed

+364
-340
lines changed

pandas/io/common.py

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
""" Common api utilities """
1+
"""Common IO api utilities"""
22

3+
import sys
34
import urlparse
4-
from pandas.util import py3compat
5+
import urllib2
6+
import zipfile
7+
from contextlib import contextmanager, closing
58
from StringIO import StringIO
69

10+
from pandas.util import py3compat
11+
712
_VALID_URLS = set(urlparse.uses_relative + urlparse.uses_netloc +
813
urlparse.uses_params)
914
_VALID_URLS.discard('')
@@ -84,3 +89,24 @@ def get_filepath_or_buffer(filepath_or_buffer, encoding=None):
8489
return filepath_or_buffer, None
8590

8691
return filepath_or_buffer, None
92+
93+
94+
# ----------------------
95+
# Prevent double closing
96+
if py3compat.PY3:
97+
urlopen = urllib2.urlopen
98+
else:
99+
@contextmanager
100+
def urlopen(*args, **kwargs):
101+
with closing(urllib2.urlopen(*args, **kwargs)) as f:
102+
yield f
103+
104+
# ZipFile is not a context manager for <= 2.6
105+
# must be tuple index here since 2.6 doesn't use namedtuple for version_info
106+
if sys.version_info[1] <= 6:
107+
@contextmanager
108+
def ZipFile(*args, **kwargs):
109+
with closing(zipfile.ZipFile(*args, **kwargs)) as zf:
110+
yield zf
111+
else:
112+
ZipFile = zipfile.ZipFile

0 commit comments

Comments
 (0)