diff --git a/docs/tutorial.rst b/docs/tutorial.rst index 75f99cf0..7138cd9e 100644 --- a/docs/tutorial.rst +++ b/docs/tutorial.rst @@ -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 ---------------------------------- diff --git a/src/towncrier/_builder.py b/src/towncrier/_builder.py index a72a3982..fefdf838 100644 --- a/src/towncrier/_builder.py +++ b/src/towncrier/_builder.py @@ -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( diff --git a/src/towncrier/_writer.py b/src/towncrier/_writer.py index 4145e3d4..6bbc5bfa 100644 --- a/src/towncrier/_writer.py +++ b/src/towncrier/_writer.py @@ -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. @@ -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) diff --git a/src/towncrier/build.py b/src/towncrier/build.py index 3a07bae1..f8e4175e 100644 --- a/src/towncrier/build.py +++ b/src/towncrier/build.py @@ -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: diff --git a/src/towncrier/create.py b/src/towncrier/create.py index 362b2ba9..2d2ec65b 100644 --- a/src/towncrier/create.py +++ b/src/towncrier/create.py @@ -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") diff --git a/src/towncrier/newsfragments/561.bugfix.rst b/src/towncrier/newsfragments/561.bugfix.rst new file mode 100644 index 00000000..9e0d382e --- /dev/null +++ b/src/towncrier/newsfragments/561.bugfix.rst @@ -0,0 +1 @@ +Add explicit encoding to read_text. diff --git a/src/towncrier/test/helpers.py b/src/towncrier/test/helpers.py index c3bd9604..77cea36d 100644 --- a/src/towncrier/test/helpers.py +++ b/src/towncrier/test/helpers.py @@ -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]: