comparison mercurial/encoding.py @ 33839:7d5bc0e5b88f

py3: introduce a wrapper for __builtins__.{raw_,}input() In order to make this work, we have to wrap the io streams in a TextIOWrapper so that __builtins__.input() can do unicode IO on Python 3. We can't just restore the original (unicode) sys.std* because we might be running a cmdserver, and if we blindly restore sys.* to the original values then we end up breaking the cmdserver. Sadly, TextIOWrapper tries to close the underlying stream during its __del__, so we have to make a sublcass to prevent that. If you see errors like: TypeError: a bytes-like object is required, not 'str' On an input() or print() call on Python 3, the substitution of sys.std* is probably the root cause. A previous version of this change tried to put the bytesinput() method in pycompat - it turns out we need to do some encoding handling, so we have to be in a higher layer that's allowed to use mercurial.encoding.encoding. As a result, this is in util for now, with the TextIOWrapper subclass hiding in encoding.py. I'm not sure of a better place for the time being. Differential Revision: https://phab.mercurial-scm.org/D299
author Augie Fackler <augie@google.com>
date Mon, 24 Jul 2017 14:38:40 -0400
parents dabe1f11ae3a
children f18b11534274
comparison
equal deleted inserted replaced
33838:48f3e87ce650 33839:7d5bc0e5b88f
6 # GNU General Public License version 2 or any later version. 6 # GNU General Public License version 2 or any later version.
7 7
8 from __future__ import absolute_import 8 from __future__ import absolute_import
9 9
10 import array 10 import array
11 import io
11 import locale 12 import locale
12 import os 13 import os
13 import unicodedata 14 import unicodedata
14 15
15 from . import ( 16 from . import (
571 # unescape U+DCxx characters 572 # unescape U+DCxx characters
572 if "\xed\xb0\x80" <= c <= "\xed\xb3\xbf": 573 if "\xed\xb0\x80" <= c <= "\xed\xb3\xbf":
573 c = chr(ord(c.decode("utf-8")) & 0xff) 574 c = chr(ord(c.decode("utf-8")) & 0xff)
574 r += c 575 r += c
575 return r 576 return r
577
578 class strio(io.TextIOWrapper):
579 """Wrapper around TextIOWrapper that respects hg's encoding assumptions.
580
581 Also works around Python closing streams.
582 """
583
584 def __init__(self, buffer, **kwargs):
585 kwargs[r'encoding'] = _sysstr(encoding)
586 super(strio, self).__init__(buffer, **kwargs)
587
588 def __del__(self):
589 """Override __del__ so it doesn't close the underlying stream."""