Patchwork D8781: infinitepush: fix `{get,put}_args` formatting on Python 3

login
register
mail settings
Submitter phabricator
Date July 21, 2020, 4:40 p.m.
Message ID <differential-rev-PHID-DREV-idvbgonulpwkaxud75k4-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/46825/
State Superseded
Headers show

Comments

phabricator - July 21, 2020, 4:40 p.m.
sheehan created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  Calling `.format()` on a byte-string does not work, thus
  causing an exception on Python 3. This commit adds a function
  to paper over the difference.

REPOSITORY
  rHG Mercurial

BRANCH
  stable

REVISION DETAIL
  https://phab.mercurial-scm.org/D8781

AFFECTED FILES
  hgext/infinitepush/store.py

CHANGE DETAILS




To: sheehan, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/hgext/infinitepush/store.py b/hgext/infinitepush/store.py
--- a/hgext/infinitepush/store.py
+++ b/hgext/infinitepush/store.py
@@ -106,6 +106,23 @@ 
             return None
 
 
+def format_placeholders_args(args, filename=None, handle=None):
+    """Formats `args` with Infinitepush replacements.
+
+    Hack to get `str.format()`-ed strings working in a BC way with
+    bytes.
+    """
+    formatted_args = []
+    for arg in args:
+        if filename and arg == b'{filename}':
+            formatted_args.append(filename)
+        elif handle and arg == b'{handle}':
+            formatted_args.append(handle)
+        else:
+            formatted_args.append(arg)
+    return formatted_args
+
+
 class externalbundlestore(abstractbundlestore):
     def __init__(self, put_binary, put_args, get_binary, get_args):
         """
@@ -144,9 +161,9 @@ 
             temp.write(data)
             temp.flush()
             temp.seek(0)
-            formatted_args = [
-                arg.format(filename=temp.name) for arg in self.put_args
-            ]
+            formatted_args = format_placeholders_args(
+                self.put_args, filename=temp.name
+            )
             returncode, stdout, stderr = self._call_binary(
                 [self.put_binary] + formatted_args
             )
@@ -166,12 +183,10 @@ 
     def read(self, handle):
         # Won't work on windows because you can't open file second time without
         # closing it
-        # TODO: rewrite without str.format()
         with pycompat.namedtempfile() as temp:
-            formatted_args = [
-                arg.format(filename=temp.name, handle=handle)
-                for arg in self.get_args
-            ]
+            formatted_args = format_placeholders_args(
+                self.get_args, filename=temp.name, handle=handle
+            )
             returncode, stdout, stderr = self._call_binary(
                 [self.get_binary] + formatted_args
             )