comparison mercurial/util.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 fa7e30efe05a
children f18b11534274
comparison
equal deleted inserted replaced
33838:48f3e87ce650 33839:7d5bc0e5b88f
169 # libraries, and sure enough Mercurial is not a library.) 169 # libraries, and sure enough Mercurial is not a library.)
170 os.stat_float_times(False) 170 os.stat_float_times(False)
171 171
172 def safehasattr(thing, attr): 172 def safehasattr(thing, attr):
173 return getattr(thing, attr, _notset) is not _notset 173 return getattr(thing, attr, _notset) is not _notset
174
175 def bytesinput(fin, fout, *args, **kwargs):
176 sin, sout = sys.stdin, sys.stdout
177 try:
178 if pycompat.ispy3:
179 sys.stdin, sys.stdout = encoding.strio(fin), encoding.strio(fout)
180 return encoding.strtolocal(input(*args, **kwargs))
181 else:
182 sys.stdin, sys.stdout = fin, fout
183 return raw_input(*args, **kwargs)
184 finally:
185 sys.stdin, sys.stdout = sin, sout
174 186
175 def bitsfrom(container): 187 def bitsfrom(container):
176 bits = 0 188 bits = 0
177 for bit in container: 189 for bit in container:
178 bits |= bit 190 bits |= bit