diff --git a/hgext/hgk.py b/hgext/hgk.py --- a/hgext/hgk.py +++ b/hgext/hgk.py @@ -48,6 +48,7 @@ commands, obsolete, patch, + pycompat, registrar, scmutil, ) @@ -96,7 +97,7 @@ while True: if opts['stdin']: try: - line = raw_input().split(' ') + line = pycompat.bytesinput().split(' ') node1 = line[0] if len(line) > 1: node2 = line[1] @@ -177,7 +178,7 @@ prefix = "" if opts['stdin']: try: - (type, r) = raw_input().split(' ') + (type, r) = pycompat.bytesinput().split(' ') prefix = " " except EOFError: return @@ -195,7 +196,7 @@ catcommit(ui, repo, n, prefix) if opts['stdin']: try: - (type, r) = raw_input().split(' ') + (type, r) = pycompat.bytesinput().split(' ') except EOFError: break else: diff --git a/mercurial/pycompat.py b/mercurial/pycompat.py --- a/mercurial/pycompat.py +++ b/mercurial/pycompat.py @@ -69,6 +69,23 @@ stdout = sys.stdout.buffer stderr = sys.stderr.buffer + class noclosetextio(io.TextIOWrapper): + def __del__(self): + """Override __del__ so it doesn't close the underlying stream.""" + + def bytesinput(*args, **kwargs): + origs = {} + try: + for stream in 'stdin stdout stderr'.split(): + s = getattr(sys, stream) + origs[stream] = s + if not isinstance(s, io.TextIOBase): + setattr(sys, stream, noclosetextio(s)) + return bytestr(input(*args, **kwargs)) + finally: + for stream, restore in origs.items(): + setattr(sys, stream, restore) + # Since Python 3 converts argv to wchar_t type by Py_DecodeLocale() on Unix, # we can use os.fsencode() to get back bytes argv. # @@ -303,6 +320,7 @@ stdin = sys.stdin stdout = sys.stdout stderr = sys.stderr + bytesinput = raw_input if getattr(sys, 'argv', None) is not None: sysargv = sys.argv sysplatform = sys.platform diff --git a/mercurial/ui.py b/mercurial/ui.py --- a/mercurial/ui.py +++ b/mercurial/ui.py @@ -1219,7 +1219,7 @@ # prompt ' ' must exist; otherwise readline may delete entire line # - http://bugs.python.org/issue12833 with self.timeblockedsection('stdio'): - line = raw_input(' ') + line = pycompat.bytesinput(r' ') sys.stdin = oldin sys.stdout = oldout