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

open tomli file with newline='' #1196

Closed
wants to merge 2 commits into from
Closed

Conversation

graingert
Copy link
Contributor

No description provided.

@graingert graingert changed the title noop commit to trigger tomli bytes deprecation warning open tomli file with newline='' Jul 30, 2021
@@ -40,7 +40,7 @@ def read(self, filenames):
filename = os.fspath(filenames)

try:
with open(filename, encoding='utf-8') as fp:
with open(filename, encoding='utf-8', newline='') as fp:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hukkin do you have a test file that breaks without newline=''?

Copy link
Contributor

Choose a reason for hiding this comment

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

Any file that has a CR character not immediately followed by a LF character. The most simple example is a one byte file with just the CR character (0x0D).

That is invalid TOML and should raise an error, but doesn't if newline='' is not set.

Copy link
Contributor Author

@graingert graingert Jul 30, 2021

Choose a reason for hiding this comment

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

Thanks, I'll push a test case tomorrow

@ProsperousHeart
Copy link
Contributor

I know this is a draft, but is it still valid?

@hukkin
Copy link
Contributor

hukkin commented Nov 8, 2024

Yes, this is still valid.

@nedbat
Copy link
Owner

nedbat commented Nov 9, 2024

@hukkin can you show me a toml file that causes a problem? Use hexdump -C to show the actual bytes. I tried a simple file with only \r line endings in Python 3.9, and it was fine:

% hexdump -C pyproject.toml
00000000  5b 74 6f 6f 6c 2e 63 6f  76 65 72 61 67 65 2e 72  |[tool.coverage.r|
00000010  75 6e 5d 0d 62 72 61 6e  63 68 3d 74 72 75 65 0d  |un].branch=true.|
00000020

@hukkin
Copy link
Contributor

hukkin commented Nov 9, 2024

can you show me a toml file that causes a problem?

I don't think it's likely this will ever really cause a problem. But the "problem" is that the file you generated was parsed fine even though it is invalid TOML. A TOMLDecodeError should be raised by tomli/tomllib instead. That can't happen if a CR is translated to LF (which is what open does by default).

@nedbat
Copy link
Owner

nedbat commented Nov 9, 2024

Thanks for the explanation. I don't see the need to enforce this kind of strictness, since no one will have a TOML file like this.

@nedbat nedbat closed this Nov 9, 2024
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.

4 participants