Skip to content

Commit a1fda45

Browse files
committed
fix(commandutil): Ensure output is always encoded to utf-8
1 parent 9490ab1 commit a1fda45

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

ladybug/commandutil.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# coding=utf-8
22
"""Utility functions for running the functions of CLI commands outside of the CLI."""
3+
import sys
34
import os
45

56

@@ -49,11 +50,20 @@ def process_content_to_output(content_str, output_file=None):
4950
dir_name = os.path.dirname(os.path.abspath(output_file))
5051
if not os.path.isdir(dir_name):
5152
os.makedirs(dir_name)
52-
with open(output_file, 'w') as of:
53-
of.write(content_str)
53+
if (sys.version_info < (3, 0)): # we need to manually encode it as UTF-8
54+
with open(output_file, 'w') as of:
55+
of.write(content_str.encode('utf-8'))
56+
else:
57+
with open(output_file, 'w', encoding='utf-8') as of:
58+
of.write(content_str)
5459
else:
5560
if 'stdout' not in str(output_file):
5661
dir_name = os.path.dirname(os.path.abspath(output_file.name))
5762
if not os.path.isdir(dir_name):
5863
os.makedirs(dir_name)
59-
output_file.write(content_str)
64+
if (sys.version_info < (3, 0)): # we need to manually encode it as UTF-8
65+
with open(output_file, 'w') as of:
66+
of.write(content_str.encode('utf-8'))
67+
else:
68+
with open(output_file, 'w', encoding='utf-8') as of:
69+
of.write(content_str)

0 commit comments

Comments
 (0)