comparison hgext/hgk.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 46ba2cdda476
children b8f74c4d188f
comparison
equal deleted inserted replaced
33838:48f3e87ce650 33839:7d5bc0e5b88f
48 commands, 48 commands,
49 obsolete, 49 obsolete,
50 patch, 50 patch,
51 registrar, 51 registrar,
52 scmutil, 52 scmutil,
53 util,
53 ) 54 )
54 55
55 cmdtable = {} 56 cmdtable = {}
56 command = registrar.command(cmdtable) 57 command = registrar.command(cmdtable)
57 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for 58 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
94 ## 95 ##
95 96
96 while True: 97 while True:
97 if opts['stdin']: 98 if opts['stdin']:
98 try: 99 try:
99 line = raw_input().split(' ') 100 line = util.bytesinput(ui.fin, ui.fout).split(' ')
100 node1 = line[0] 101 node1 = line[0]
101 if len(line) > 1: 102 if len(line) > 1:
102 node2 = line[1] 103 node2 = line[1]
103 else: 104 else:
104 node2 = None 105 node2 = None
175 # strings 176 # strings
176 # 177 #
177 prefix = "" 178 prefix = ""
178 if opts['stdin']: 179 if opts['stdin']:
179 try: 180 try:
180 (type, r) = raw_input().split(' ') 181 (type, r) = util.bytesinput(ui.fin, ui.fout).split(' ')
181 prefix = " " 182 prefix = " "
182 except EOFError: 183 except EOFError:
183 return 184 return
184 185
185 else: 186 else:
193 return 1 194 return 1
194 n = repo.lookup(r) 195 n = repo.lookup(r)
195 catcommit(ui, repo, n, prefix) 196 catcommit(ui, repo, n, prefix)
196 if opts['stdin']: 197 if opts['stdin']:
197 try: 198 try:
198 (type, r) = raw_input().split(' ') 199 (type, r) = util.bytesinput(ui.fin, ui.fout).split(' ')
199 except EOFError: 200 except EOFError:
200 break 201 break
201 else: 202 else:
202 break 203 break
203 204