File tree 3 files changed +16
-3
lines changed
3 files changed +16
-3
lines changed Original file line number Diff line number Diff line change 234
234
235
235
- Bug in :meth: `to_csv ` caused a ``ValueError `` when it was called with a filename in combination with ``mode `` containing a ``b `` (:issue: `35058 `)
236
236
- 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 `)
238
238
239
239
Plotting
240
240
^^^^^^^^
Original file line number Diff line number Diff line change @@ -100,7 +100,9 @@ def to_pickle(
100
100
try :
101
101
f .write (pickle .dumps (obj , protocol = protocol ))
102
102
finally :
103
- f .close ()
103
+ if f != filepath_or_buffer :
104
+ # do not close user-provided file objects GH 35679
105
+ f .close ()
104
106
for _f in fh :
105
107
_f .close ()
106
108
if should_close :
@@ -215,7 +217,9 @@ def read_pickle(
215
217
# e.g. can occur for files written in py27; see GH#28645 and GH#31988
216
218
return pc .load (f , encoding = "latin-1" )
217
219
finally :
218
- f .close ()
220
+ if f != filepath_or_buffer :
221
+ # do not close user-provided file objects GH 35679
222
+ f .close ()
219
223
for _f in fh :
220
224
_f .close ()
221
225
if should_close :
Original file line number Diff line number Diff line change @@ -183,6 +183,15 @@ def python_unpickler(path):
183
183
result = python_unpickler (path )
184
184
compare_element (result , expected , typ )
185
185
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
+
186
195
187
196
def test_pickle_path_pathlib ():
188
197
df = tm .makeDataFrame ()
You can’t perform that action at this time.
0 commit comments