From 53211f7bb407750d23b9c01b217c4d47e108bf95 Mon Sep 17 00:00:00 2001 From: Gabriel Mazetto Date: Wed, 24 Aug 2016 15:42:53 +0200 Subject: [PATCH 1/2] Makes rest2html work in both python2 and python3 --- lib/github/commands/rest2html | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/github/commands/rest2html b/lib/github/commands/rest2html index 7ecfe272..958cd3cf 100755 --- a/lib/github/commands/rest2html +++ b/lib/github/commands/rest2html @@ -31,9 +31,11 @@ import sys import os # This fixes docutils failing with unicode parameters to CSV-Table. The -S -# switch and the following 2 lines can be removed after upgrading to python 3. -reload(sys) -sys.setdefaultencoding('utf-8') +# switch and the following 3 lines can be removed after upgrading to python 3. +if sys.version_info[0] < 3: + reload(sys) + sys.setdefaultencoding('utf-8') + import site try: From 62ff5993081920ad75016f2b87019dd14e3af137 Mon Sep 17 00:00:00 2001 From: Gabriel Mazetto Date: Tue, 6 Sep 2016 21:13:53 +0200 Subject: [PATCH 2/2] Handle sys.stdin and sys.stdout encoding issues in python 3.x This code will keep current behavior for python 2.x. --- lib/github/commands/rest2html | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/github/commands/rest2html b/lib/github/commands/rest2html index 958cd3cf..db3b1bfa 100755 --- a/lib/github/commands/rest2html +++ b/lib/github/commands/rest2html @@ -45,6 +45,7 @@ except: pass import codecs +import io from docutils import nodes from docutils.parsers.rst import directives, roles @@ -164,7 +165,11 @@ def main(): except IOError: # given filename could not be found return '' except IndexError: # no filename given - text = sys.stdin.read() + if sys.version_info[0] < 3: # python 2.x + text = sys.stdin.read() + else: # python 3 + input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8') + text = input_stream.read() writer = Writer() writer.translator_class = GitHubHTMLTranslator @@ -187,5 +192,9 @@ def main(): return '' if __name__ == '__main__': - sys.stdout.write("%s%s" % (main(), "\n")) + if sys.version_info[0] < 3: # python 2.x + sys.stdout.write("%s%s" % (main(), "\n")) + else: # python 3 + output_stream = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') + output_stream.write("%s%s" % (main(), "\n")) sys.stdout.flush()