From 22b227584fcfb41a70c2f1938a484e7a763e0d88 Mon Sep 17 00:00:00 2001 From: Ben Rowland Date: Fri, 23 Jun 2023 13:17:01 +0100 Subject: [PATCH] prevent universal_newlines modifying --draft output 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. --- src/towncrier/build.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/towncrier/build.py b/src/towncrier/build.py index ef7c5f35..ecbb4844 100644 --- a/src/towncrier/build.py +++ b/src/towncrier/build.py @@ -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 + click.echo(content.encode("utf8")) return click.echo("Writing to newsfile...", err=to_err)