Skip to content

gh-110745: pathlib.Path.read_text add a newline argument #110880

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

Merged
merged 14 commits into from
Nov 21, 2023

Conversation

Okabe-Junya
Copy link
Contributor

@Okabe-Junya Okabe-Junya commented Oct 14, 2023

gh-110745: pathlib.Path.read_text add a newline argument

📚 Documentation preview 📚: https://cpython-previews--110880.org.readthedocs.build/

@ghost
Copy link

ghost commented Oct 14, 2023

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Oct 14, 2023

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@Okabe-Junya Okabe-Junya changed the title gh-110745: feat: pathlib.Path.read_text should include a newline argument gh-110745: pathlib.Path.read_text should include a newline argument Oct 14, 2023
@Okabe-Junya Okabe-Junya marked this pull request as draft October 14, 2023 19:44
@Okabe-Junya Okabe-Junya changed the title gh-110745: pathlib.Path.read_text should include a newline argument gh-110745: pathlib.Path.read_text add a newline argument Oct 15, 2023
@Okabe-Junya Okabe-Junya marked this pull request as ready for review October 15, 2023 08:16
@barneygale
Copy link
Contributor

Looking good! Could you also add a test in Lib/test/test_pathlib.py? Maybe have a look at test_write_text_with_newlines :)

@Okabe-Junya
Copy link
Contributor Author

Thank you for your kind review! I apologize for the delayed response.

As you suggested, I intend to add the appropriate tests. Please give me a moment.

@Okabe-Junya
Copy link
Contributor Author

Only the test on Windows is failing. I wonder why...

@Okabe-Junya
Copy link
Contributor Author

Okabe-Junya commented Oct 24, 2023

Implementing tests using os.linesep seems challenging. Especially on Windows, where the newline code is \r\n, covering this in test cases appears difficult.

Would it be appropriate to separate test cases for each OS?

@barneygale
Copy link
Contributor

Regarding newline=None, this should convert all newlines to \n on read, on all OSs, so your test shouldn't need to reference os.linesep. Maybe something like this?

    def test_read_text_with_newlines(self):
        p = self.cls(BASE, 'fileA')
        p.write_bytes(b'abcde\r\nfghlk\n\rmnopq')
        self.assertEqual(p.read_text(newline=None),   'abcde\nfghlk\n\nmnopq')
        self.assertEqual(p.read_text(newline=''),     'abcde\r\nfghlk\n\rmnopq')
        self.assertEqual(p.read_text(newline='\r'),   'abcde\r\nfghlk\n\rmnopq')
        self.assertEqual(p.read_text(newline='\n'),   'abcde\r\nfghlk\n\rmnopq')
        self.assertEqual(p.read_text(newline='\r\n'), 'abcde\r\nfghlk\n\rmnopq')

@Okabe-Junya
Copy link
Contributor Author

@barneygale

Thank you for your thoughtful comment!!

I've implemented tests based on your feedback, and they now pass!!

Copy link
Contributor

@barneygale barneygale left a comment

Choose a reason for hiding this comment

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

Sorry for my delayed feedback. Last thing to do is to fix the version number, and give yourself some credit in the changelog if you wish :)

@bedevere-app
Copy link

bedevere-app bot commented Nov 21, 2023

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@Okabe-Junya
Copy link
Contributor Author

Thank you for your kind review!! (I might not have mentioned properly. I am sorry for that)

I accept your polite suggestion and am ready to merge this. Thank you.

@Okabe-Junya
Copy link
Contributor Author

I have made the requested changes; please review again

Copy link
Contributor

@barneygale barneygale left a comment

Choose a reason for hiding this comment

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

Looks good, thank you very much!

@barneygale barneygale merged commit 9d70831 into python:main Nov 21, 2023
@Okabe-Junya Okabe-Junya deleted the fix-issue-110745 branch November 22, 2023 03:13
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
…thon#110880)

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Barney Gale <[email protected]>
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
…thon#110880)

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Barney Gale <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants