Skip to content

Commit

Permalink
Track new handles in _get_handle
Browse files Browse the repository at this point in the history
  • Loading branch information
dhimmel committed Dec 13, 2016
1 parent 376dce3 commit 94aa0cb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
6 changes: 3 additions & 3 deletions pandas/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1455,9 +1455,9 @@ def save(self):
f = self.path_or_buf
close = False
else:
f = _get_handle(self.path_or_buf, self.mode,
encoding=self.encoding,
compression=self.compression)
f, new_handle = _get_handle(self.path_or_buf, self.mode,
encoding=self.encoding,
compression=self.compression)
close = True

try:
Expand Down
14 changes: 11 additions & 3 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,14 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
Returns
-------
A file like object.
f : file-like
A file-like object
new_handle : file-like or None
A file-like object that was openned in this function. Or None if none
were openned.
"""

new_handle = None
f = path_or_buf
is_path = isinstance(path_or_buf, compat.string_types)

Expand Down Expand Up @@ -358,7 +363,8 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
from io import TextIOWrapper
f = TextIOWrapper(f, encoding=encoding)

return f
new_handle = f
return f, new_handle

elif is_path:
if compat.PY2:
Expand All @@ -370,11 +376,13 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
else:
# Python 3 and no explicit encoding
f = open(path_or_buf, mode, errors='replace')
new_handle = f

# in Python 3, convert BytesIO or fileobjects passed with an encoding
if compat.PY3 and isinstance(path_or_buf, compat.BytesIO):
from io import TextIOWrapper
f = TextIOWrapper(f, encoding=encoding)
new_handle = f

if memory_map and hasattr(f, 'fileno'):
try:
Expand All @@ -388,7 +396,7 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
# leave the file handler as is then
pass

return f
return f, new_handle


class MMapWrapper(BaseIterator):
Expand Down
6 changes: 4 additions & 2 deletions pandas/io/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,10 @@ def read_json(path_or_buf=None, orient=None, typ='frame', dtype=True,
exists = False

if exists:
with _get_handle(filepath_or_buffer, 'r', encoding=encoding) as fh:
json = fh.read()
fh, new_handle = _get_handle(filepath_or_buffer, 'r',
encoding=encoding)
json = fh.read()
fh.close()
else:
json = filepath_or_buffer
elif hasattr(filepath_or_buffer, 'read'):
Expand Down
17 changes: 5 additions & 12 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1805,18 +1805,11 @@ def __init__(self, f, **kwds):
self.comment = kwds['comment']
self._comment_lines = []

add_handle = (
isinstance(f, compat.string_types) or
self.compression or
(compat.PY3 and isinstance(f, compat.BytesIO))
)

f = _get_handle(f, 'r', encoding=self.encoding,
compression=self.compression,
memory_map=self.memory_map)

if add_handle:
self.handles.append(f)
f, new_handle = _get_handle(f, 'r', encoding=self.encoding,
compression=self.compression,
memory_map=self.memory_map)
if new_handle is not None:
self.handles.append(new_handle)

# Set self.data to something that can read lines.
if hasattr(f, 'readline'):
Expand Down

0 comments on commit 94aa0cb

Please sign in to comment.