Patchwork [1,of,5,V2] commandserver: add _readstr and _readlist

login
register
mail settings
Submitter Jun Wu
Date Feb. 16, 2016, 7:50 p.m.
Message ID <4fee628c32ea96ebc9c5.1455652222@x1c>
Download mbox | patch
Permalink /patch/13231/
State Accepted
Delegated to: Yuya Nishihara
Headers show

Comments

Jun Wu - Feb. 16, 2016, 7:50 p.m.
# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1455649905 0
#      Tue Feb 16 19:11:45 2016 +0000
# Node ID 4fee628c32ea96ebc9c51eb3a1c22ef94feb8645
# Parent  e0c52563425cc71d985c7e22fc824857afcb4030
commandserver: add _readstr and _readlist

Reading a string or a list are common operations for commandserver and
chgserver. This patch adds _readstr and _readlist to avoid duplication.

Patch

diff --git a/mercurial/commandserver.py b/mercurial/commandserver.py
--- a/mercurial/commandserver.py
+++ b/mercurial/commandserver.py
@@ -190,6 +190,25 @@ 
 
         return data
 
+    def _readstr(self):
+        """read a string from the channel
+
+        format:
+        data length (uint32), data
+        """
+        length = struct.unpack('>I', self._read(4))[0]
+        if not length:
+            return ''
+        return self._read(length)
+
+    def _readlist(self):
+        """read a list of NULL separated strings from the channel"""
+        s = self._readstr()
+        if s:
+            return s.split('\0')
+        else:
+            return []
+
     def runcommand(self):
         """ reads a list of \0 terminated arguments, executes
         and writes the return code to the result channel """