Skip to content

Commit 0c6691b

Browse files
authored
Add _print_to_console utility function to handle non-utf-8 console encodings (#16)
1 parent 5788314 commit 0c6691b

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

pydocstringformatter/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ def _format_files(self, filepaths: List[Path]) -> None:
8585
is_changed = self._format_file(file) or is_changed
8686

8787
if not is_changed:
88-
print("Nothing to do! All docstrings are correct 🎉")
88+
utils._print_to_console("Nothing to do! All docstrings are correct 🎉\n")

pydocstringformatter/utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pydocstringformatter.utils.file_diference import _generate_diff
1212
from pydocstringformatter.utils.find_docstrings import _is_docstring
1313
from pydocstringformatter.utils.find_python_file import _find_python_files
14+
from pydocstringformatter.utils.output import _print_to_console
1415

1516
__all__ = [
1617
"_find_python_files",
@@ -22,4 +23,5 @@
2223
"PydocstringFormatterError",
2324
"_register_arguments",
2425
"TomlParsingError",
26+
"_print_to_console",
2527
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import sys
2+
3+
4+
def _encode_string(string: str) -> bytes:
5+
"""Encode a string to utf-8.
6+
7+
This can be used to circumvent the issue of the standard encoding
8+
of a windows console not being utf-8.
9+
See: https://github.com/DanielNoord/pydocstringformatter/issues/13
10+
"""
11+
return string.encode("utf-8")
12+
13+
14+
def _print_to_console(string: str) -> None:
15+
"""Print a string to the console while handling edge cases.
16+
17+
This should be used instead of print() whenever we want to
18+
print emoji's or non-ASCII characters.
19+
"""
20+
sys.stdout.buffer.write(_encode_string(string))

tests/test_utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import sys
12
import tokenize
23
from pathlib import Path
34
from typing import List, Tuple
45

6+
import pytest
7+
8+
import pydocstringformatter
59
from pydocstringformatter.utils import _find_python_files, _is_docstring
610

711
HERE = Path(__file__)
@@ -133,3 +137,22 @@ def test_module_docstrings(self) -> None:
133137
docstrings.append((tokeninfo.start, tokeninfo.end))
134138

135139
assert docstrings == [((3, 0), (4, 3))]
140+
141+
142+
def test_encoding_of_console_messages(
143+
capsys: pytest.CaptureFixture[str], test_file: str
144+
) -> None:
145+
"""Test that we can print emoji's to non utf-8 consoles.
146+
147+
Regression test for:
148+
https://github.com/DanielNoord/pydocstringformatter/issues/13
149+
"""
150+
sys.stdout.reconfigure(encoding="cp1252") # type: ignore[attr-defined]
151+
with open(test_file, "w", encoding="utf-8") as file:
152+
file.write('"""A multi-line\ndocstring\n"""')
153+
154+
pydocstringformatter.run_docstring_formatter([test_file, "--write"])
155+
156+
output = capsys.readouterr()
157+
assert output.out == "Nothing to do! All docstrings are correct 🎉\n"
158+
assert not output.err

0 commit comments

Comments
 (0)