-
Notifications
You must be signed in to change notification settings - Fork 122
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
prevent universal_newlines modifying --draft output #525
base: trunk
Are you sure you want to change the base?
Conversation
News fragments are read in binary mode, so on Windows they keep their `\r\n` newlines. When these are echoed to the console in `--draft` mode, Python's universal newlines support converts the `\n` to `os.linesep` (`\r\n`), leaving them as `\r\r\n`. If this output is then read by Python again (in text mode), that same newline support converts both `\r` and `\r\n` into `\n`, causing all newlines to be doubled up (`\n\n`). This commit echoes the `--draft` output as utf8 encoded `bytes` rather than a `str` to prevent this from happening. This also matches the behaviour of writing to a newsfile in non-draft mode.
Wouldn't the better thing to do here be just stop opening fragments in binary mode? In - with open(full_filename, "rb") as f:
- data = f.read().decode("utf8", "replace")
+ with open(full_filename, encoding="utf8", errors="replace") as f:
+ data = f.read() |
I can confirm that that would solve my use case, but I assumed that there was some good reason for reading the fragments in binary mode in the first place. I knew that changing the output at this level would not have any side effects on the rest of |
Well if it passes the test suite then it should be fine 😂 |
I think that the fragments should be just text, so is safe to open them in text mode. As mentioned by Chris, if the change passes the test it should be good enough. But it's very important to add a new test that would fail with the code from Otherwise, in the future, someone else might revert this change...and the tests would still be green and the PR could be approved. I don't know what test to suggest ... try to get some input similar to what you have on Windows and follow the same steps. I can see that we run the tests on Windows, so we should be covered. Fell free to skip the tests when running on Linux. Thanks |
@@ -258,7 +258,8 @@ def __main( | |||
"What is seen below is what would be written.\n", | |||
err=to_err, | |||
) | |||
click.echo(content) | |||
# output as bytes to prevent universal_newlines modifying content #453 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to have a self contained comment, that explains why this is needed without having to search the internet / GitHub :)
I still don't fully understand what went wrong here and what is the best way to fix it..
As mentioned by Chris, I think that a better idea is to fix this inside render_fragments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the changes.
This needs a release notes fragment and at least one new test, which is executed at least on Windows
Description
Fixes #524
News fragments are read in binary mode, so on Windows they keep their
\r\n
newlines. When these are echoed to the console in--draft
mode, Python's universal newlines support converts the\n
toos.linesep
(\r\n
), leaving them as\r\r\n
. If this output is then read by Python again (in text mode), that same newline support converts both\r
and\r\n
into\n
, causing all newlines to be doubled up (\n\n
). This commit echoes the--draft
output as utf8 encodedbytes
rather than astr
to prevent this from happening. This also matches the behaviour of writing to a newsfile in non-draft mode.@adiroiban please let me know what tests (if any) you think would be appropriate for this change and I will add them. I will also add a news fragment detailing the change if you are minded to accept the change.
Checklist
src/towncrier/newsfragments/
. Describe yourchange and include important information. Your change will be included in the public release notes.
docs/tutorial.rst
is still up-to-date.docs/cli.rst
reflects those changes.docs/configuration.rst
reflects those changes.