Skip to content

Commit

Permalink
Add compatibility shim for Python 3.9 and earlier.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Apr 28, 2024
1 parent f013fba commit 9d36ea1
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/towncrier/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@

from __future__ import annotations

import sys

from pathlib import Path


if sys.version_info < (3, 10):

def _newline_write_text(path, content, **kwargs):
with path.open("w", **kwargs) as strm:
strm.write(content)

else:

def _newline_write_text(path, content, **kwargs):
path.write_text(content, **kwargs)


def append_to_newsfile(
directory: str,
filename: str,
Expand All @@ -37,7 +51,8 @@ def append_to_newsfile(
if top_line and top_line in prev_body:
raise ValueError("It seems you've already produced newsfiles for this version?")

news_file.write_text(
_newline_write_text(
news_file,
# If there is no previous body that means we're writing a brand new news file.
# We don't want extra whitespace at the end of this new file.
header + (content + prev_body if prev_body else content.rstrip() + "\n"),
Expand Down

0 comments on commit 9d36ea1

Please sign in to comment.