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

fix(python): Fix handling of TextIOWrapper in write_csv #17328

Merged
merged 4 commits into from
Jul 5, 2024

Conversation

ruihe774
Copy link
Contributor

@ruihe774 ruihe774 commented Jul 1, 2024

Fixes "write to closed file" in write_csv:

>>> df.write_csv(open("test.csv", "w"))
OSError: write to closed file

@github-actions github-actions bot added the fix Bug fix label Jul 1, 2024
@ruihe774 ruihe774 changed the title fix(Python): fix writing to closed file in write_csv fix(python): fix writing to closed file in write_csv Jul 1, 2024
@github-actions github-actions bot added the python Related to Python Polars label Jul 1, 2024
Copy link

codecov bot commented Jul 1, 2024

Codecov Report

Attention: Patch coverage is 79.16667% with 10 lines in your changes missing coverage. Please review.

Project coverage is 80.69%. Comparing base (3a3600c) to head (43eedd2).
Report is 5 commits behind head on main.

Files Patch % Lines
py-polars/src/file.rs 78.72% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17328      +/-   ##
==========================================
- Coverage   80.72%   80.69%   -0.03%     
==========================================
  Files        1475     1476       +1     
  Lines      193388   193497     +109     
  Branches     2760     2759       -1     
==========================================
+ Hits       156117   156151      +34     
- Misses      36761    36836      +75     
  Partials      510      510              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ritchie46
Copy link
Member

Thanks, can you add a test that showed the issue before this fix?

@ruihe774
Copy link
Contributor Author

ruihe774 commented Jul 2, 2024

Thanks, can you add a test that showed the issue before this fix?

Added

@ruihe774
Copy link
Contributor Author

ruihe774 commented Jul 3, 2024

It is not necessary to extract .buffer from TextIOWrapper after #17315 - TextIOWrapper is also handled. We can simply remove these lines.

@ruihe774 ruihe774 force-pushed the fix-write-closed branch 3 times, most recently from 14f102b to dcfea28 Compare July 3, 2024 16:18
@ruihe774
Copy link
Contributor Author

ruihe774 commented Jul 3, 2024

I'm sorry that there was a bug in #17315, namely the handling of TextIOWrapper in Windows and with non-UTF8 encoding. I've fixed it in 784241c.

Now TextIOWrapper with non-UTF8 encoding is denied to avoid incorrect encoding. Previously write_csv does not check that.

@ruihe774
Copy link
Contributor Author

ruihe774 commented Jul 3, 2024

Because the requirement of encoding has become more strict, PYTHONUTF8=1 is added to CI to fix tests on Windows. UTF-8 mode will become the default with PEP 686.

@ruihe774 ruihe774 changed the title fix(python): fix writing to closed file in write_csv fix(python): fix handling of TextIOWrapper in write_csv Jul 5, 2024
@ruihe774 ruihe774 changed the title fix(python): fix handling of TextIOWrapper in write_csv fix(python): Fix handling of TextIOWrapper in write_csv Jul 5, 2024
@ritchie46 ritchie46 merged commit 221bea8 into pola-rs:main Jul 5, 2024
19 of 20 checks passed
henryharbeck pushed a commit to henryharbeck/polars that referenced this pull request Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants