Patchwork [2,of,2] util: add length argument to util.buffer()

login
register
mail settings
Submitter Pulkit Goyal
Date Jan. 15, 2017, 8:12 a.m.
Message ID <79f98c1b8d718bfa94f1.1484467972@pulkit-goyal>
Download mbox | patch
Permalink /patch/18222/
State Accepted
Headers show

Comments

Pulkit Goyal - Jan. 15, 2017, 8:12 a.m.
# HG changeset patch
# User Pulkit Goyal <7895pulkit@gmail.com>
# Date 1484404515 -19800
#      Sat Jan 14 20:05:15 2017 +0530
# Node ID 79f98c1b8d718bfa94f12eb62872598ad7ec9c2e
# Parent  9dc4d056d85a041b2b28587b3b84608b1f0903a5
util: add length argument to util.buffer()

util.buffer() either returns inbuilt buffer function or defines a new one which
slices. The inbuilt buffer() also has a length argument which is missing from
the ones whe defined. This patch adds that length argument.
Yuya Nishihara - Jan. 15, 2017, 8:55 a.m.
On Sun, 15 Jan 2017 13:42:52 +0530, Pulkit Goyal wrote:
> # HG changeset patch
> # User Pulkit Goyal <7895pulkit@gmail.com>
> # Date 1484404515 -19800
> #      Sat Jan 14 20:05:15 2017 +0530
> # Node ID 79f98c1b8d718bfa94f12eb62872598ad7ec9c2e
> # Parent  9dc4d056d85a041b2b28587b3b84608b1f0903a5
> util: add length argument to util.buffer()
> 
> util.buffer() either returns inbuilt buffer function or defines a new one which
> slices. The inbuilt buffer() also has a length argument which is missing from
> the ones whe defined. This patch adds that length argument.
> 
> diff -r 9dc4d056d85a -r 79f98c1b8d71 mercurial/util.py
> --- a/mercurial/util.py	Sat Jan 14 01:23:07 2017 +0530
> +++ b/mercurial/util.py	Sat Jan 14 20:05:15 2017 +0530
> @@ -238,10 +238,14 @@
>      buffer = buffer
>  except NameError:
>      if not pycompat.ispy3:
> -        def buffer(sliceable, offset=0):
> +        def buffer(sliceable, offset=0, length=None):
> +            if length:
> +                return sliceable[offset:offset + length]
>              return sliceable[offset:]
>      else:
> -        def buffer(sliceable, offset=0):
> +        def buffer(sliceable, offset=0, length=None):
> +            if length:
> +                return memoryview(sliceable)[offset:offset + length]
>              return memoryview(sliceable)[offset:]

Here length=0 and None have different meanings. Can you resend V2?

Patch

diff -r 9dc4d056d85a -r 79f98c1b8d71 mercurial/util.py
--- a/mercurial/util.py	Sat Jan 14 01:23:07 2017 +0530
+++ b/mercurial/util.py	Sat Jan 14 20:05:15 2017 +0530
@@ -238,10 +238,14 @@ 
     buffer = buffer
 except NameError:
     if not pycompat.ispy3:
-        def buffer(sliceable, offset=0):
+        def buffer(sliceable, offset=0, length=None):
+            if length:
+                return sliceable[offset:offset + length]
             return sliceable[offset:]
     else:
-        def buffer(sliceable, offset=0):
+        def buffer(sliceable, offset=0, length=None):
+            if length:
+                return memoryview(sliceable)[offset:offset + length]
             return memoryview(sliceable)[offset:]
 
 closefds = pycompat.osname == 'posix'