Skip to content

Commit 4774b75

Browse files
committed
Use list to store new handles
1 parent 94aa0cb commit 4774b75

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

pandas/formats/format.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1455,9 +1455,9 @@ def save(self):
14551455
f = self.path_or_buf
14561456
close = False
14571457
else:
1458-
f, new_handle = _get_handle(self.path_or_buf, self.mode,
1459-
encoding=self.encoding,
1460-
compression=self.compression)
1458+
f, handles = _get_handle(self.path_or_buf, self.mode,
1459+
encoding=self.encoding,
1460+
compression=self.compression)
14611461
close = True
14621462

14631463
try:

pandas/io/common.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,11 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
297297
-------
298298
f : file-like
299299
A file-like object
300-
new_handle : file-like or None
301-
A file-like object that was openned in this function. Or None if none
302-
were openned.
300+
handles : list of file-like objects
301+
A list of file-like object that were openned in this function.
303302
"""
304303

305-
new_handle = None
304+
handles = list()
306305
f = path_or_buf
307306
is_path = isinstance(path_or_buf, compat.string_types)
308307

@@ -358,13 +357,15 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
358357
msg = 'Unrecognized compression type: {}'.format(compression)
359358
raise ValueError(msg)
360359

360+
handles.append(f)
361+
361362
# In Python 3
362363
if compat.PY3:
363364
from io import TextIOWrapper
364365
f = TextIOWrapper(f, encoding=encoding)
366+
handles.append(f)
365367

366-
new_handle = f
367-
return f, new_handle
368+
return f, handles
368369

369370
elif is_path:
370371
if compat.PY2:
@@ -376,13 +377,13 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
376377
else:
377378
# Python 3 and no explicit encoding
378379
f = open(path_or_buf, mode, errors='replace')
379-
new_handle = f
380+
handles.append(f)
380381

381382
# in Python 3, convert BytesIO or fileobjects passed with an encoding
382383
if compat.PY3 and isinstance(path_or_buf, compat.BytesIO):
383384
from io import TextIOWrapper
384385
f = TextIOWrapper(f, encoding=encoding)
385-
new_handle = f
386+
handles.append(f)
386387

387388
if memory_map and hasattr(f, 'fileno'):
388389
try:
@@ -396,7 +397,7 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
396397
# leave the file handler as is then
397398
pass
398399

399-
return f, new_handle
400+
return f, handles
400401

401402

402403
class MMapWrapper(BaseIterator):

pandas/io/json.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ def read_json(path_or_buf=None, orient=None, typ='frame', dtype=True,
259259
exists = False
260260

261261
if exists:
262-
fh, new_handle = _get_handle(filepath_or_buffer, 'r',
263-
encoding=encoding)
262+
fh, handles = _get_handle(filepath_or_buffer, 'r',
263+
encoding=encoding)
264264
json = fh.read()
265265
fh.close()
266266
else:

pandas/io/parsers.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1805,11 +1805,10 @@ def __init__(self, f, **kwds):
18051805
self.comment = kwds['comment']
18061806
self._comment_lines = []
18071807

1808-
f, new_handle = _get_handle(f, 'r', encoding=self.encoding,
1809-
compression=self.compression,
1810-
memory_map=self.memory_map)
1811-
if new_handle is not None:
1812-
self.handles.append(new_handle)
1808+
f, handles = _get_handle(f, 'r', encoding=self.encoding,
1809+
compression=self.compression,
1810+
memory_map=self.memory_map)
1811+
self.handles.extend(handles)
18131812

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

0 commit comments

Comments
 (0)