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 all 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
2 changes: 2 additions & 0 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ You should get an output similar to this::

Note: if you configure a Markdown file (for example, ``filename = "CHANGES.md"``) in your configuration file, the titles will be output in Markdown format instead.

Note: all files (news fragments, the news file, the configuration file, and templates) are encoded and are expected to be encoded as UTF-8.


Producing News Files In Production
----------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/towncrier/_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def find_fragments(
full_filename = os.path.join(section_dir, basename)
fragment_filenames.append(full_filename)
with open(full_filename, "rb") as f:
data = f.read().decode("utf8", "replace")
data = f.read().decode("utf-8", "replace")

if (ticket, category, counter) in file_content:
raise ValueError(
Expand Down
4 changes: 2 additions & 2 deletions src/towncrier/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def append_to_newsfile(
# Leave newlines alone. This probably leads to inconsistent newlines,
# because we've loaded existing content with universal newlines, but that's
# the original behavior.
with news_file.open("w", encoding="utf8", newline="") as f:
with news_file.open("w", encoding="utf-8", newline="") as f:
if header:
f.write(header)
# If there is no previous body that means we're writing a brand new news file.
Expand All @@ -66,7 +66,7 @@ def _figure_out_existing_content(

# If we didn't use universal newlines here, we wouldn't find *start_string*
# which usually contains a `\n`.
with news_file.open(encoding="utf8") as f:
with news_file.open(encoding="utf-8") as f:
content = f.read()

t = content.split(start_string, 1)
Expand Down
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.

2 changes: 1 addition & 1 deletion src/towncrier/test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def read_pkg_resource(path: str) -> str:
"""
Read *path* from the towncrier package.
"""
return (resources.files("towncrier") / path).read_text("utf8")
return (resources.files("towncrier") / path).read_text("utf-8")


def with_isolated_runner(fn: Callable[..., Any]) -> Callable[..., Any]:
Expand Down
Loading