infinitepush: fix `{get,put}_args` formatting on Python 3 stable
authorConnor Sheehan <sheehan@mozilla.com>
Wed, 22 Apr 2020 18:08:12 -0400
branchstable
changeset 45187 e285655c37c5
parent 45186 a52bf967e90a
child 45188 837840018306
infinitepush: fix `{get,put}_args` formatting on Python 3 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. Differential Revision: https://phab.mercurial-scm.org/D8781
hgext/infinitepush/store.py
--- a/hgext/infinitepush/store.py	Wed Apr 22 17:59:17 2020 -0400
+++ b/hgext/infinitepush/store.py	Wed Apr 22 18:08:12 2020 -0400
@@ -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
             )