Skip to content

Commit 0bca930

Browse files
BenAtPwsseraphire
authored andcommitted
Cast byte strings to unicode strings in python3
Signed-off-by: Ben Keene <[email protected]>
1 parent d9f6f3b commit 0bca930

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

git-p4.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,22 @@
3636
unicode = str
3737
bytes = bytes
3838
basestring = (str,bytes)
39+
isunicode = True
40+
def ustring(text):
41+
"""Returns the byte string as a unicode string"""
42+
if text == '' or text == b'':
43+
return ''
44+
return unicode(text, "utf-8")
3945
else:
4046
# 'unicode' exists, must be Python 2
4147
str = str
4248
unicode = unicode
4349
bytes = str
4450
basestring = basestring
51+
isunicode = False
52+
def ustring(text):
53+
"""Returns the byte string unchanged"""
54+
return text
4555

4656
try:
4757
from subprocess import CalledProcessError
@@ -196,6 +206,8 @@ def read_pipe_full(c):
196206
expand = isinstance(c,basestring)
197207
p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=expand)
198208
(out, err) = p.communicate()
209+
out = ustring(out)
210+
err = ustring(err)
199211
return (p.returncode, out, err)
200212

201213
def read_pipe(c, ignore_error=False):
@@ -263,6 +275,7 @@ def p4_has_move_command():
263275
cmd = p4_build_cmd(["move", "-k", "@from", "@to"])
264276
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
265277
(out, err) = p.communicate()
278+
err = ustring(err)
266279
# return code will be 1 in either case
267280
if err.find("Invalid option") >= 0:
268281
return False
@@ -646,10 +659,18 @@ def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
646659
if skip_info:
647660
if 'code' in entry and entry['code'] == 'info':
648661
continue
662+
if b'code' in entry and entry[b'code'] == b'info':
663+
continue
649664
if cb is not None:
650665
cb(entry)
651666
else:
652-
result.append(entry)
667+
if isunicode:
668+
out = {}
669+
for key, value in entry.items():
670+
out[ustring(key)] = ustring(value)
671+
result.append(out)
672+
else:
673+
result.append(entry)
653674
except EOFError:
654675
pass
655676
exitCode = p4.wait()
@@ -792,7 +813,7 @@ def gitConfig(key, typeSpecifier=None):
792813
cmd += [ key ]
793814
s = read_pipe(cmd, ignore_error=True)
794815
_gitConfig[key] = s.strip()
795-
return _gitConfig[key]
816+
return ustring(_gitConfig[key])
796817

797818
def gitConfigBool(key):
798819
"""Return a bool, using git config --bool. It is True only if the
@@ -860,6 +881,7 @@ def branch_exists(branch):
860881
cmd = [ "git", "rev-parse", "--symbolic", "--verify", branch ]
861882
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
862883
out, _ = p.communicate()
884+
out = ustring(out)
863885
if p.returncode:
864886
return False
865887
# expect exactly one line of output: the branch name

0 commit comments

Comments
 (0)