-
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
Fragment files with \r\n line endings get written out as \r\r\n #524
Comments
Many thanks for the report and analysis. This functionality looks very useful :) I am not familiar with At the same time, I think that the maybe we can have something like but why do you use I think that now there is also |
@adiroiban thanks for the feedback. I should point out that I am just a user of I think it would be possible to use the I have made the change to |
I am using the https://github.com/sphinx-contrib/sphinxcontrib-towncrier Sphinx extension which embeds the latest
towncrier
draft changelog into the built changelog. This works nicely but there is an odd effect that when a changelog fragment has multiple lines separated by \r\n line endings, the draft changelog gets written with those line endings replaced by a double carriage return \r\r\n then reading that back in Python converts it to\n\n
so that each line of the fragment gets its own paragraph in the rendered changelog.This effect seems to be caused by mixing binary reading of fragments with text output, as alluded to in this issue #420 by @hynek. I should note that this is all on Windows. Python has a rather weird behaviour trying to handle newlines in text because it forces all newlines to be
\n
internally. So reading a file as text it will convert any\r\n
newlines into simply\n
. Correspondingly, when it writes out the string as text it will automatically revert the\n
to a\r\n
string. But if the file is read in binary then it will keep the\r\n
form for all newlines, then when it is written out as text the\n
is converted to\r\n
, leaving the written bytes as\r\r\n
.You can reproduce this with this very simple example code:
Of course, when the content is written straight to the console then you don't see this issue, so it is normally fine for
--draft
, but when the content is read back in to Python for further use then it is a problem (on Windows). The simplest solution to this problem would be to encode the draft output before echoing it here:towncrier/src/towncrier/build.py
Line 261 in 3cef9a1
click
can handle writing thebytes
just as easily as astr
, but this preserves the newlines without adding the extra\r
and makes the problem go away. I can't see any obvious side effects for any other use case of doing this encode step. Happy to put together a PR if desired, although this is only a 1 line change.The text was updated successfully, but these errors were encountered: