Skip to content

Commit 86dca24

Browse files
yangskyboxlabsgitster
authored andcommitted
git-p4: encode/decode communication with git for python3
Under python3, calls to write() on the stream to `git fast-import` must be encoded. This patch wraps the IO object such that this encoding is done transparently. Conversely, any text data read from subprocesses must also be decoded before running through the rest of the pipeline. Signed-off-by: Yang Zhao <[email protected]> Reviewed-by: Ben Keene <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6cec21a commit 86dca24

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

git-p4.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,21 @@ def read_pipe_full(c):
183183
(out, err) = p.communicate()
184184
return (p.returncode, out, decode_text_stream(err))
185185

186-
def read_pipe(c, ignore_error=False):
186+
def read_pipe(c, ignore_error=False, raw=False):
187187
""" Read output from command. Returns the output text on
188188
success. On failure, terminates execution, unless
189189
ignore_error is True, when it returns an empty string.
190+
191+
If raw is True, do not attempt to decode output text.
190192
"""
191193
(retcode, out, err) = read_pipe_full(c)
192194
if retcode != 0:
193195
if ignore_error:
194196
out = ""
195197
else:
196198
die('Command failed: %s\nError: %s' % (str(c), err))
199+
if not raw:
200+
out = decode_text_stream(out)
197201
return out
198202

199203
def read_pipe_text(c):
@@ -220,7 +224,6 @@ def read_pipe_lines(c):
220224
val = [decode_text_stream(line) for line in pipe.readlines()]
221225
if pipe.close() or p.wait():
222226
die('Command failed: %s' % str(c))
223-
224227
return val
225228

226229
def p4_read_pipe_lines(c):
@@ -616,7 +619,8 @@ def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
616619
stdin_file.write(stdin)
617620
else:
618621
for i in stdin:
619-
stdin_file.write(i + '\n')
622+
stdin_file.write(encode_text_stream(i))
623+
stdin_file.write(b'\n')
620624
stdin_file.flush()
621625
stdin_file.seek(0)
622626

@@ -1245,7 +1249,7 @@ def generatePointer(self, contentFile):
12451249
['git', 'lfs', 'pointer', '--file=' + contentFile],
12461250
stdout=subprocess.PIPE
12471251
)
1248-
pointerFile = pointerProcess.stdout.read()
1252+
pointerFile = decode_text_stream(pointerProcess.stdout.read())
12491253
if pointerProcess.wait():
12501254
os.remove(contentFile)
12511255
die('git-lfs pointer command failed. Did you install the extension?')
@@ -3538,6 +3542,15 @@ def openStreams(self):
35383542
self.gitStream = self.importProcess.stdin
35393543
self.gitError = self.importProcess.stderr
35403544

3545+
if bytes is not str:
3546+
# Wrap gitStream.write() so that it can be called using `str` arguments
3547+
def make_encoded_write(write):
3548+
def encoded_write(s):
3549+
return write(s.encode() if isinstance(s, str) else s)
3550+
return encoded_write
3551+
3552+
self.gitStream.write = make_encoded_write(self.gitStream.write)
3553+
35413554
def closeStreams(self):
35423555
self.gitStream.close()
35433556
if self.importProcess.wait() != 0:

0 commit comments

Comments
 (0)