[PATCH 1 of 5] commandserver: add _readstr and _readlist

Yuya Nishihara yuya at tcha.org
Tue Feb 16 09:02:27 EST 2016


On Mon, 15 Feb 2016 14:42:49 +0000, Jun Wu wrote:
> # HG changeset patch
> # User Jun Wu <quark at fb.com>
> # Date 1455545101 0
> #      Mon Feb 15 14:05:01 2016 +0000
> # Node ID 4c76a3d1c588442154f0d28b215e9a7d9ff0a36e
> # Parent  f17513bc8f81f768f1eb627870b74a4e025fdd9e
> 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.
> 
> diff --git a/mercurial/commandserver.py b/mercurial/commandserver.py
> --- a/mercurial/commandserver.py
> +++ b/mercurial/commandserver.py
> @@ -190,6 +190,19 @@
>  
>          return data
>  
> +    def _readstr(self):
> +        length = struct.unpack('>I', self._read(4))[0]
> +        if not length:
> +            return None
> +        return self._read(length)

Nit: I prefer '' than None because length == 0 can be a valid string. And it
will make _readlist() shorter.

> +    def _readlist(self):
> +        s = self._readstr()
> +        if s is None:
> +            return []
> +        else:
> +            return s.split('\0')


More information about the Mercurial-devel mailing list