Skip to content

Commit 1d7087d

Browse files
committed
JsonReader should only close if it opened
1 parent 55170dd commit 1d7087d

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

pandas/io/json/json.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,13 @@ def __init__(self, filepath_or_buffer, orient, typ, dtype, convert_axes,
377377
self.lines = lines
378378
self.chunksize = chunksize
379379
self.nrows_seen = 0
380+
self.should_close = False
380381

381382
if self.chunksize is not None:
382383
self.chunksize = _validate_integer("chunksize", self.chunksize, 1)
383384
if not self.lines:
384385
raise ValueError("chunksize can only be passed if lines=True")
385386

386-
self.fp_or_buf = filepath_or_buffer
387387
data = self._get_data_from_filepath(filepath_or_buffer)
388388
self.data = self._preprocess_data(data)
389389

@@ -427,6 +427,8 @@ def _get_data_from_filepath(self, filepath_or_buffer):
427427
if exists:
428428
data, _ = _get_handle(filepath_or_buffer, 'r',
429429
encoding=self.encoding)
430+
self.should_close = True
431+
self.open_stream = data
430432

431433
return data
432434

@@ -472,18 +474,14 @@ def _get_object_parser(self, json):
472474

473475
def close(self):
474476
"""
475-
If self.chunksize, self.data may need closing.
476-
If not, self.fp_or_buff may need closing.
477+
If we opened a stream earlier, in _get_data_from_filepath, we should
478+
close it. If an open stream or file was passed, we leave it open.
477479
"""
478-
try:
479-
self.data.close()
480-
except (IOError, AttributeError):
481-
pass
482-
483-
try:
484-
self.fp_or_buf.close()
485-
except(IOError, AttributeError):
486-
pass
480+
if self.should_close:
481+
try:
482+
self.open_stream.close()
483+
except (IOError, AttributeError):
484+
pass
487485

488486
def __next__(self):
489487
lines = list(islice(self.data, self.chunksize))

pandas/tests/io/json/test_pandas.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
read_json, compat)
1111
from datetime import timedelta
1212
import pandas as pd
13+
from pandas.io.json.json import JsonReader
1314

1415
from pandas.util.testing import (assert_almost_equal, assert_frame_equal,
1516
assert_series_equal, network,
@@ -1148,13 +1149,14 @@ def test_readjson_chunks_closes(self, chunksize):
11481149
with ensure_clean('test.json') as path:
11491150
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
11501151
df.to_json(path, lines=True, orient="records")
1151-
f = open(path, 'r')
1152-
if chunksize is not None:
1153-
pd.concat(pd.read_json(f, lines=True, chunksize=chunksize))
1154-
else:
1155-
pd.read_json(f, lines=True)
1156-
assert f.closed, \
1157-
"didn't close file with chunksize = %s" % chunksize
1152+
reader = JsonReader(
1153+
path, orient=None, typ="frame", dtype=True, convert_axes=True,
1154+
convert_dates=True, keep_default_dates=True, numpy=False,
1155+
precise_float=False, date_unit=None, encoding=None,
1156+
lines=True, chunksize=chunksize)
1157+
reader.read()
1158+
assert reader.open_stream.closed, "didn't close stream with \
1159+
chunksize = %s" % chunksize
11581160

11591161
@pytest.mark.parametrize("chunksize", [0, -1, 2.2, "foo"])
11601162
def test_readjson_invalid_chunksize(self, lines_json_df, chunksize):

0 commit comments

Comments
 (0)