Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Pandas closes user-provided file handles that it doesn't own. #36980

Closed
2 of 3 tasks
tpllaha opened this issue Oct 8, 2020 · 3 comments · Fixed by #36997
Closed
2 of 3 tasks

BUG: Pandas closes user-provided file handles that it doesn't own. #36980

tpllaha opened this issue Oct 8, 2020 · 3 comments · Fixed by #36997
Labels
Bug IO CSV read_csv, to_csv
Milestone

Comments

@tpllaha
Copy link

tpllaha commented Oct 8, 2020

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


read_csv closes user-provided file handles in specific cases. In particular, if we pass a BytesIO or a file opened in binary mode, and pass an encoding kwarg. If the encoding kwarg is not passed, pandas does not close the file handle (as expected).
Much earlier (in 2016) a related issue has been reported and fixed. The test cases that were mentioned there still pass today, but only in case the file handle is opened in binary mode & the encoding kwarg is passed. (version 1.1.2 & python 3.8.5)

Code Sample, a copy-pastable example

import sys
import pandas
from io import BytesIO, StringIO

print(f'Python version: {sys.version}')
print(f'pandas version: {pandas.__version__}')

string_io = StringIO('a,b\n1,2')
bytes_io_1 = BytesIO(b'a,b\n1,2')
bytes_io_2 = BytesIO(b'a,b\n1,2')

pandas.read_csv(string_io)
print(f'Was StringIO closed? - {string_io.closed}')

pandas.read_csv(bytes_io_1)
print(f'Was BytesIO closed when encoding is NOT passed? - {bytes_io_1.closed}')

pandas.read_csv(bytes_io_2, encoding='utf-8')
print(f'Was BytesIO closed when encoding is passed? - {bytes_io_2.closed}')

prints:

Python version: 3.8.5 (v3.8.5:580fbb018f, Jul 20 2020, 12:11:27) 
[Clang 6.0 (clang-600.0.57)]
pandas version: 1.1.2
Was StringIO closed? - False
Was BytesIO closed when encoding is NOT passed? - False
Was BytesIO closed when encoding is passed? - True
                                              ^

Problem description

Expectation is that pandas should never close user-provided handles. If pandas didn't open the file, then it doesn't own it & therefore shouldn't close it.

Expected Output

I would expect the script above to output:

Python version: 3.8.5 (v3.8.5:580fbb018f, Jul 20 2020, 12:11:27) 
[Clang 6.0 (clang-600.0.57)]
pandas version: 1.1.2
Was StringIO closed? - False
Was BytesIO closed when encoding is NOT passed? - False
Was BytesIO closed when encoding is passed? - False

Output of pd.show_versions()

INSTALLED VERSIONS

commit : d9fff27
python : 3.8.5.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Thu Jun 18 20:49:00 PDT 2020; root:xnu-6153.141.1~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.0
numpy : 1.17.2
pytz : 2020.1
dateutil : 2.7.3
pip : 20.2.3
setuptools : 40.7.1
Cython : 0.29.7
pytest : 4.6.5
hypothesis : 4.55.2
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.18.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 0.8.2
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : 0.3.5
scipy : 1.5.2
sqlalchemy : 1.3.10
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@tpllaha tpllaha added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 8, 2020
@tpllaha tpllaha changed the title BUG: BUG: Pandas closes user-provided file handles that it doesn't own. Oct 8, 2020
@twoertwein
Copy link
Member

twoertwein commented Oct 8, 2020

@tpllaha thank you for the report! I thought master wouldn't have that problem anymore (I was thinking about to_csv), but pandas.read_csv(bytes_io_2, encoding='utf-8') still seems to close that file handle

@twoertwein twoertwein added IO CSV read_csv, to_csv and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 8, 2020
@twoertwein
Copy link
Member

@tpllaha if you need a quick workaround, only the c engine is affected. Until it is fixed, you can use engine='python'

@tpllaha
Copy link
Author

tpllaha commented Oct 20, 2020

@twoertwein thanks. For now, I've worked around it by simply not passing the encoding parameter. It looks like the default encoding is utf-8 and my files are utf-8 encoded, but the bug only surfaces if the encoding is passed explicitly.

@jreback jreback added this to the 1.2 milestone Nov 2, 2020
alubbock added a commit to alubbock/thunor that referenced this issue Feb 6, 2023
read_csv closes a file handle when passed an open one
with an encoding set
(pandas-dev/pandas#36980)
in pandas <1.2. Update pandas dependency to fix this
bug.
alubbock added a commit to alubbock/thunor-web that referenced this issue Feb 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO CSV read_csv, to_csv
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants