aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--markdown/__init__.py11
1 files changed, 9 insertions, 2 deletions
diff --git a/markdown/__init__.py b/markdown/__init__.py
index 58993dd..d90d6c0 100644
--- a/markdown/__init__.py
+++ b/markdown/__init__.py
@@ -379,8 +379,15 @@ class Markdown:
output_file.write(html)
# Don't close here. User may want to write more.
else:
- stdout = codecs.getwriter(encoding)(sys.stdout, errors="xmlcharrefreplace")
- stdout.write(html)
+ if sys.stdout.encoding:
+ # If we are in Python 3 or if we are not piping output:
+ sys.stdout.write(html)
+ else:
+ # In python 2.x if you pipe output on command line,
+ # sys.stdout.encoding is None. So lets set it:
+ writer = codecs.getwriter(encoding)
+ stdout = writer(sys.stdout, errors="xmlcharrefreplace")
+ stdout.write(html)
return self