Skip to content

Commit 8905ab0

Browse files
committed
Fix Unicode encoding issue on Windows stdout
- Add UTF-8 encoding support for stdout output on Windows - Use sys.stdout.reconfigure() for Python 3.7+ or TextIOWrapper fallback - Fixes UnicodeEncodeError when outputting special characters to stdout - Resolves failing Windows CI tests for test_stdout_output_with_special_characters
1 parent 6e17b4a commit 8905ab0

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/treemapper/writer.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,19 @@ def write_tree_content(f: TextIO) -> None:
4141
write_yaml_node(f, child, " ")
4242

4343
if output_file is None:
44-
# Write to stdout
45-
write_tree_content(sys.stdout)
44+
# Write to stdout with proper UTF-8 encoding
45+
# On Windows, sys.stdout might use cp1252 encoding which can't handle Unicode
46+
# We need to ensure UTF-8 encoding for proper Unicode support
47+
if hasattr(sys.stdout, 'reconfigure'):
48+
# Python 3.7+ has reconfigure method
49+
sys.stdout.reconfigure(encoding='utf-8')
50+
write_tree_content(sys.stdout)
51+
else:
52+
# Fallback for older Python versions or systems without reconfigure
53+
import io
54+
utf8_stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
55+
write_tree_content(utf8_stdout)
56+
utf8_stdout.flush()
4657
logging.info("Directory tree written to stdout")
4758
else:
4859
# Write to file

0 commit comments

Comments
 (0)