Skip to content

Commit bddad97

Browse files
committed
Track new handles in _get_handle
#14576 (comment) #14576 (comment)
1 parent 376dce3 commit bddad97

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

pandas/formats/format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@ def save(self):
14551455
f = self.path_or_buf
14561456
close = False
14571457
else:
1458-
f = _get_handle(self.path_or_buf, self.mode,
1458+
f, new_handle = _get_handle(self.path_or_buf, self.mode,
14591459
encoding=self.encoding,
14601460
compression=self.compression)
14611461
close = True

pandas/io/common.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,14 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
295295
296296
Returns
297297
-------
298-
A file like object.
298+
f : file-like
299+
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.
299303
"""
300304

305+
new_handle = None
301306
f = path_or_buf
302307
is_path = isinstance(path_or_buf, compat.string_types)
303308

@@ -358,7 +363,8 @@ def _get_handle(path_or_buf, mode, encoding=None, compression=None,
358363
from io import TextIOWrapper
359364
f = TextIOWrapper(f, encoding=encoding)
360365

361-
return f
366+
new_handle = f
367+
return f, new_handle
362368

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

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

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

391-
return f
399+
return f, new_handle
392400

393401

394402
class MMapWrapper(BaseIterator):

pandas/io/json.py

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

261261
if exists:
262-
with _get_handle(filepath_or_buffer, 'r', encoding=encoding) as fh:
263-
json = fh.read()
262+
fh, new_handle = _get_handle(filepath_or_buffer, 'r',
263+
encoding=encoding)
264+
json = fh.read()
265+
fh.close()
264266
else:
265267
json = filepath_or_buffer
266268
elif hasattr(filepath_or_buffer, 'read'):

pandas/io/parsers.py

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

1808-
add_handle = (
1809-
isinstance(f, compat.string_types) or
1810-
self.compression or
1811-
(compat.PY3 and isinstance(f, compat.BytesIO))
1812-
)
1813-
1814-
f = _get_handle(f, 'r', encoding=self.encoding,
1815-
compression=self.compression,
1816-
memory_map=self.memory_map)
1817-
1818-
if add_handle:
1819-
self.handles.append(f)
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)
18201813

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

0 commit comments

Comments
 (0)