git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC PATCH 2/4] git-p4: properly encode/decode communication with git for python 3

From
Yang Zhao <yang.zhao@skyboxlabs.com>
Date
Nov 28, 2019, 01:28 UTC
Message-ID
<20191128012807.3103-3-yang.zhao@skyboxlabs.com>
In-Reply-To
<20191128012807.3103-1-yang.zhao@skyboxlabs.com>

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 <yang.zhao@skyboxlabs.com>
---
 git-p4.py | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/git-p4.py b/git-p4.py
index ead9d816e1..d79cbf7005 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -237,6 +237,9 @@ def read_pipe_lines(c):
     if pipe.close() or p.wait():
         die('Command failed: %s' % str(c))
 
+    if use_encoded_streams:
+        val = [line.decode() for line in val]
+
     return val
 
 def p4_read_pipe_lines(c):
@@ -631,7 +634,7 @@ def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
             stdin_file.write(stdin)
         else:
             for i in stdin:
-                stdin_file.write(i + '\n')
+                stdin_file.write((i + '\n').encode())
         stdin_file.flush()
         stdin_file.seek(0)
 
@@ -3547,6 +3550,15 @@ def openStreams(self):
         self.gitStream = self.importProcess.stdin
         self.gitError = self.importProcess.stderr
 
+        if use_encoded_streams:
+            # Wrap gitStream.write() so that it can be called using `str` arguments
+            def make_encoded_write(write):
+                def encoded_write(s):
+                    return write(s.encode() if isinstance(s, str) else s)
+                return encoded_write
+
+            self.gitStream.write = make_encoded_write(self.gitStream.write)
+
     def closeStreams(self):
         self.gitStream.close()
         if self.importProcess.wait() != 0:
-- 
2.24.0.windows.2
Previous: Yang ZhaoNext: Yang Zhao
Message 2 of 7 in “git-p4: python 3 compatability”
  1. 0/4 git-p4: python 3 compatabilityYang Zhao, Nov 28, 2019
  2. 2/4 git-p4: properly encode/decode communication with git for python 3Yang Zhao, Nov 28, 2019
  3. 1/4 git-p4: decode response from p4 to str for python3Yang Zhao, Nov 28, 2019
  4. 3/4 git-p4: open .gitp4-usercache.txt in text modeYang Zhao, Nov 28, 2019
  5. 4/4 git-p4: use utf-8 encoding for file paths throughoutYang Zhao, Nov 28, 2019
  6. Elijah NewrenNov 28, 2019
  7. Johannes SchindelinNov 28, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.