Skip to content

bpo-41352: Raise UnsupportedOperation for FileIO.readall() in "w" mode #21568

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

evanWang1409
Copy link

@evanWang1409 evanWang1409 commented Jul 20, 2020

check self->readable

added tests for FileIO.read and FileIO.readall

https://bugs.python.org/issue41352

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

Recognized GitHub username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@evanWang1409

This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

check self->readable

added tests for FileIO.read and FileIO.readall
Copy link
Member

@iritkatriel iritkatriel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran the tests with and without the code change. The read test passed with and without the patch, the readall test failed without and passed with. So this seems correct.

What does readall do currently on a file which was opened with "w"? Does it sometimes work?

@iritkatriel
Copy link
Member

You need to add a news entry to this PR. You can created it with this form: https://blurb-it.herokuapp.com/add_blurb

Look at some merged PRs to see what they look like.

@tekknolagi
Copy link
Contributor

Hi @iritkatriel. I worked with @evanWang1409 to submit this patch and can answer your questions.

The read test is to illustrate how a similar function to readall works in comparison. This change does not affect read.

readall currently will just attempt to read from the file, leading to I don't quite remember what results.

Comment on lines 78 to 80
w = os.fdopen(w, "w")
w.write("hello")
w.close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a with statement.

Comment on lines 87 to 89
w = os.fdopen(w, "w")
w.write("hello")
w.close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a with statement.

with self.assertRaises(_io.UnsupportedOperation):
f.read()

def testReadallWithWritingMode(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These new test methods have too much duplicated code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I (or @evanWang1409) can pull out the pipe setup into a separate method, if you would like. Otherwise I'm not sure what else is the right direction here. It's not clear what abstraction would make sense given that there are only two callers right now and this is a small test case.

Comment on lines 702 to 703
if (!self->readable)
return err_mode("reading");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PEP 7 style guide states that "all new C code requires braces".

@MaxwellDupre
Copy link
Contributor

Unfortunately can't checkout:
~/D/G/cpython   $  gh pr checkout 21568
remote: Enumerating objects: 30, done.
remote: Counting objects: 100% (19/19), done.
remote: Total 30 (delta 19), reused 19 (delta 19), pack-reused 11
Unpacking objects: 100% (30/30), 20.54 KiB | 244.00 KiB/s, done.
From https://github.com/python/cpython
! [rejected] refs/pull/21568/head -> master (non-fast-forward)
exit status 1

My clone was up to date yesterday and local copy, not having issues with other PRs.

@python-cla-bot
Copy link

python-cla-bot bot commented Apr 6, 2025

The following commit authors need to sign the Contributor License Agreement:

CLA signed

@evanWang1409
Copy link
Author

The following commit authors need to sign the Contributor License Agreement:

CLA signed

Signed CLA. Will come back and see if this fix is still needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants