@matheusbsbs / matheus-bagliano / commits / 484d09c303
Fork 0

git-p4: change the expansion test from basestring to list

Python 3 handles strings differently than Python 2.7. Since Python 2 is reaching it's end of life, a series of changes are being submitted to enable python 3.5 and following support. The current code fails basic tests under python 3.5. Some codepaths can represent a command line the program internally prepares to execute either as a single string (i.e. each token properly quoted, concatenated with $IFS) or as a list of argv[] elements, and there are 9 places where we say "if X is isinstance(_, basestring), then do this thing to handle X as a command line in a single string; if not, X is a command line in a list form". This does not work well with Python 3, as there is no basestring (everything is Unicode now), and even with Python 2, it was not an ideal way to tell the two cases apart, because an internally formed command line could have been in a single Unicode string. Flip the check to say "if X is not a list, then handle X as a command line in a single string; otherwise treat it as a command line in a list form". This will get rid of references to 'basestring', to migrate the code ready for Python 3. Thanks-to: Junio C Hamano <gitster@pobox.com> Signed-off-by: Ben Keene <seraphire@gmail.com> Signed-off-by: Yang Zhao <yang.zhao@skyboxlabs.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ben Keene committed Dec 13, 2019 at 15:52 UTC 484d09c303f271bdbdc2b7ed597e1d8f3959ce0f
1 file changed +9 -9
git-p4.py
+9 -9
@@ -89,7 +89,7 @@ def p4_build_cmd(cmd):
89 # Provide a way to not pass this option by setting git-p4.retries to 0
90 real_cmd += ["-r", str(retries)]
91
92 - if isinstance(cmd,basestring):
92 + if not isinstance(cmd, list):
93 real_cmd = ' '.join(real_cmd) + ' ' + cmd
94 else:
95 real_cmd += cmd
@@ -155,7 +155,7 @@ def write_pipe(c, stdin):
155 if verbose:
156 sys.stderr.write('Writing pipe: %s\n' % str(c))
157
158 - expand = isinstance(c,basestring)
158 + expand = not isinstance(c, list)
159 p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand)
160 pipe = p.stdin
161 val = pipe.write(stdin)
@@ -177,7 +177,7 @@ def read_pipe_full(c):
177 if verbose:
178 sys.stderr.write('Reading pipe: %s\n' % str(c))
179
180 - expand = isinstance(c,basestring)
180 + expand = not isinstance(c, list)
181 p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=expand)
182 (out, err) = p.communicate()
183 return (p.returncode, out, err)
@@ -213,7 +213,7 @@ def read_pipe_lines(c):
213 if verbose:
214 sys.stderr.write('Reading pipe: %s\n' % str(c))
215
216 - expand = isinstance(c, basestring)
216 + expand = not isinstance(c, list)
217 p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
218 pipe = p.stdout
219 val = pipe.readlines()
@@ -256,7 +256,7 @@ def p4_has_move_command():
256 return True
257
258 def system(cmd, ignore_error=False):
259 - expand = isinstance(cmd,basestring)
259 + expand = not isinstance(cmd, list)
260 if verbose:
261 sys.stderr.write("executing %s\n" % str(cmd))
262 retcode = subprocess.call(cmd, shell=expand)
@@ -268,7 +268,7 @@ def system(cmd, ignore_error=False):
268 def p4_system(cmd):
269 """Specifically invoke p4 as the system command. """
270 real_cmd = p4_build_cmd(cmd)
271 - expand = isinstance(real_cmd, basestring)
271 + expand = not isinstance(real_cmd, list)
272 retcode = subprocess.call(real_cmd, shell=expand)
273 if retcode:
274 raise CalledProcessError(retcode, real_cmd)
@@ -506,7 +506,7 @@ def getP4OpenedType(file):
506 # Return the set of all p4 labels
507 def getP4Labels(depotPaths):
508 labels = set()
509 - if isinstance(depotPaths,basestring):
509 + if not isinstance(depotPaths, list):
510 depotPaths = [depotPaths]
511
512 for l in p4CmdList(["labels"] + ["%s..." % p for p in depotPaths]):
@@ -593,7 +593,7 @@ def isModeExecChanged(src_mode, dst_mode):
593 def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
594 errors_as_exceptions=False):
595
596 - if isinstance(cmd,basestring):
596 + if not isinstance(cmd, list):
597 cmd = "-G " + cmd
598 expand = True
599 else:
@@ -610,7 +610,7 @@ def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
610 stdin_file = None
611 if stdin is not None:
612 stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode)
613 - if isinstance(stdin,basestring):
613 + if not isinstance(stdin, list):
614 stdin_file.write(stdin)
615 else:
616 for i in stdin: