-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathutil.py
More file actions
25 lines (22 loc) · 696 Bytes
/
util.py
File metadata and controls
25 lines (22 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# -*- coding: utf-8 -*-
import os
import codecs
import sys
def safe_print(text):
'''
This prints text to the terminal, regardless of the OS.
It uses sys.stdout plus the codecs module to enforce UTF-8.
'''
if os.name == 'nt': # Windows
codec = codecs.lookup('cp437')
else: # Mac / Linux
codec = codecs.lookup('utf8')
wrapped_stdout = codec.streamwriter(sys.stdout, errors='replace')
wrapped_stdout.write(text)
wrapped_stdout.write('\n')
def test_safe_print():
'''
This has a bunch of possibly scary Unicode test cases to make
sure that safe_print works on your platform.
'''
safe_print(u'☂☀♠')