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

Add explicit encoding to read_text. #577

Merged
merged 7 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/towncrier/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ def __main(
click.echo("Loading template...", err=to_err)
if isinstance(config.template, tuple):
template = (
resources.files(config.template[0]).joinpath(config.template[1]).read_text()
resources.files(config.template[0])
.joinpath(config.template[1])
.read_text(encoding="utf-8")
)
else:
with open(config.template, encoding="utf-8") as tmpl:
Expand Down
2 changes: 1 addition & 1 deletion src/towncrier/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def __main(
click.echo("Aborted creating news fragment due to empty message.")
ctx.exit(1)

with open(segment_file, "w") as f:
with open(segment_file, "w", encoding="utf-8") as f:
f.write(content)
if config.create_eof_newline and content and not content.endswith("\n"):
f.write("\n")
Expand Down
1 change: 1 addition & 0 deletions src/towncrier/newsfragments/561.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add explicit encoding to read_text.
Copy link
Member

Choose a reason for hiding this comment

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

I don't know if anyone is using non UTF-8 encoding in these days for source code.... maybe some legacy Windows shops might still use UTF-16


I am ok with this change, but I think that somewhere in the documentation, we should mention that towncrier can only work with UTF-8 fragment files and it will produce UTF-8 encoded files.

Maybe this can be done as part of the "Philosophy" section

https://towncrier.readthedocs.io/en/latest/#philosophy

Maybe add

It assumes that the source code is stored in UTF-8 files.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know if anyone is using non UTF-8 encoding in these days for source code.... maybe some legacy Windows shops might still use UTF-16

In my experience, that's not been the case, even for text-oriented applications like towncrier. My instinct is it's still a bug fix, but for Windows users, it is technically a breaking change. My advice - hope that there aren't any Windows users relying on locale but be prepared to back out the change and introduce a compatibility shim if someone does encounter an issue. I'm happy to help with that if needed.


Maybe this can be done as part of the "Philosophy" section

I'm a little uneasy adding it to the Philosophy section since that section comes from the readme, and it feels more like an implementation detail. Philosophy doesn't even mention markdown or restructured text, a concern that's going to be a lot more salient to a reader.

In 83b8c02, I've proposed that it be added to the tutorial, near where "markdown" is mentioned and shortly after the user has first encountered creating content for news fragments. Let me know what you think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In e1caa2c, I took it a step further and explicitly mentioned other concerns that expect UTF-8.

Loading