Skip to content

Commit 09620a6

Browse files
authored
Lazy open output files, and always close them (#314)
1 parent 6061b3e commit 09620a6

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

readme_renderer/__main__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def main(cli_args: Optional[List[str]] = None) -> None:
1818
help="README format (inferred from input file name or package)")
1919
parser.add_argument('input', help="Input README file or package name")
2020
parser.add_argument('-o', '--output', help="Output file (default: stdout)",
21-
type=argparse.FileType('w'), default='-')
21+
default='-')
2222
args = parser.parse_args(cli_args)
2323

2424
content_format = args.format
@@ -55,7 +55,11 @@ def main(cli_args: Optional[List[str]] = None) -> None:
5555
"`rst`, or `txt`)")
5656
if rendered is None:
5757
sys.exit(1)
58-
print(rendered, file=args.output)
58+
if args.output == "-":
59+
print(rendered, file=sys.stdout)
60+
else:
61+
with open(args.output, "w") as fp:
62+
print(rendered, file=fp)
5963

6064

6165
if __name__ == '__main__':

tox.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ deps =
1515
pytest
1616
pytest-cov
1717
pytest-icdiff
18+
setenv =
19+
# Display up to 20 frames in backtraces when showing ResourceWarnings.
20+
PYTHONTRACEMALLOC=20
1821
commands =
19-
pytest --strict-markers --cov {posargs}
22+
pytest -Wall --strict-markers --cov {posargs}
2023
extras = md
2124

2225
[testenv:mypy]

0 commit comments

Comments
 (0)