Skip to content

Commit 360873d

Browse files
committed
Handle sys.stdin and sys.stdout encoding issues in python 3.x
This code will keep current behavior for python 2.x.
1 parent 53211f7 commit 360873d

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

lib/github/commands/rest2html

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ except:
4545
pass
4646

4747
import codecs
48+
import io
4849

4950
from docutils import nodes
5051
from docutils.parsers.rst import directives, roles
@@ -164,7 +165,11 @@ def main():
164165
except IOError: # given filename could not be found
165166
return ''
166167
except IndexError: # no filename given
167-
text = sys.stdin.read()
168+
if sys.version_info[0] < 3: # python 3
169+
input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
170+
text = input_stream.read()
171+
else: # python 2
172+
text = sys.stdin.read()
168173

169174
writer = Writer()
170175
writer.translator_class = GitHubHTMLTranslator
@@ -187,5 +192,9 @@ def main():
187192
return ''
188193

189194
if __name__ == '__main__':
190-
sys.stdout.write("%s%s" % (main(), "\n"))
195+
if sys.version_info[0] < 3: # python 3
196+
output_stream = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
197+
output_stream.write("%s%s" % (main(), "\n"))
198+
else: # python 2.x
199+
sys.stdout.write("%s%s" % (main(), "\n"))
191200
sys.stdout.flush()

0 commit comments

Comments
 (0)