Skip to content

Commit a0896e1

Browse files
authored
BUG: to_pickle/read_pickle do not close user-provided file objects (#35686)
1 parent 8145ea6 commit a0896e1

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ I/O
234234

235235
- Bug in :meth:`to_csv` caused a ``ValueError`` when it was called with a filename in combination with ``mode`` containing a ``b`` (:issue:`35058`)
236236
- In :meth:`read_csv` `float_precision='round_trip'` now handles `decimal` and `thousands` parameters (:issue:`35365`)
237-
-
237+
- :meth:`to_pickle` and :meth:`read_pickle` were closing user-provided file objects (:issue:`35679`)
238238

239239
Plotting
240240
^^^^^^^^

pandas/io/pickle.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ def to_pickle(
100100
try:
101101
f.write(pickle.dumps(obj, protocol=protocol))
102102
finally:
103-
f.close()
103+
if f != filepath_or_buffer:
104+
# do not close user-provided file objects GH 35679
105+
f.close()
104106
for _f in fh:
105107
_f.close()
106108
if should_close:
@@ -215,7 +217,9 @@ def read_pickle(
215217
# e.g. can occur for files written in py27; see GH#28645 and GH#31988
216218
return pc.load(f, encoding="latin-1")
217219
finally:
218-
f.close()
220+
if f != filepath_or_buffer:
221+
# do not close user-provided file objects GH 35679
222+
f.close()
219223
for _f in fh:
220224
_f.close()
221225
if should_close:

pandas/tests/io/test_pickle.py

+9
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ def python_unpickler(path):
183183
result = python_unpickler(path)
184184
compare_element(result, expected, typ)
185185

186+
# and the same for file objects (GH 35679)
187+
with open(path, mode="wb") as handle:
188+
writer(expected, path)
189+
handle.seek(0) # shouldn't close file handle
190+
with open(path, mode="rb") as handle:
191+
result = pd.read_pickle(handle)
192+
handle.seek(0) # shouldn't close file handle
193+
compare_element(result, expected, typ)
194+
186195

187196
def test_pickle_path_pathlib():
188197
df = tm.makeDataFrame()

0 commit comments

Comments
 (0)