Patchwork [STABLE] cborutil: fix streamencode() to handle subtypes

login
register
mail settings
Submitter Yuya Nishihara
Date June 16, 2019, 4:07 a.m.
Message ID <3dd649ffcd1ad2409813.1560658037@mimosa>
Download mbox | patch
Permalink /patch/40538/
State Accepted
Headers show

Comments

Yuya Nishihara - June 16, 2019, 4:07 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1560655867 -32400
#      Sun Jun 16 12:31:07 2019 +0900
# Branch stable
# Node ID 3dd649ffcd1ad2409813bd0977653db02dc3374c
# Parent  d532292eff22b095c994ffedcd4bf98f84f873aa
cborutil: fix streamencode() to handle subtypes

Otherwise the template filter 'cbor' could crash because of bytes subclass:

  ValueError: do not know how to encode
  <class 'mercurial.encoding.safelocalstr'>
Augie Fackler - June 17, 2019, 5:11 p.m.
On Sun, Jun 16, 2019 at 01:07:17PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1560655867 -32400
> #      Sun Jun 16 12:31:07 2019 +0900
> # Branch stable
> # Node ID 3dd649ffcd1ad2409813bd0977653db02dc3374c
> # Parent  d532292eff22b095c994ffedcd4bf98f84f873aa
> cborutil: fix streamencode() to handle subtypes

queued for stable, thanks

Patch

diff --git a/mercurial/utils/cborutil.py b/mercurial/utils/cborutil.py
--- a/mercurial/utils/cborutil.py
+++ b/mercurial/utils/cborutil.py
@@ -214,6 +214,14 @@  def streamencode(v):
     fn = STREAM_ENCODERS.get(v.__class__)
 
     if not fn:
+        # handle subtypes such as encoding.localstr and util.sortdict
+        for ty in STREAM_ENCODERS:
+            if not isinstance(v, ty):
+                continue
+            fn = STREAM_ENCODERS[ty]
+            break
+
+    if not fn:
         raise ValueError('do not know how to encode %s' % type(v))
 
     return fn(v)
diff --git a/tests/test-template-functions.t b/tests/test-template-functions.t
--- a/tests/test-template-functions.t
+++ b/tests/test-template-functions.t
@@ -1562,6 +1562,20 @@  json filter takes input as utf-8b:
   $ HGENCODING=ascii hg log -T "{'`cat latin1`'|json}\n" -l1
   "\udce9"
 
+cbor filter is bytes transparent, which should handle bytes subtypes
+as bytes:
+
+  $ HGENCODING=ascii hg log -T "{branch|cbor}" -r0 \
+  > | "$PYTHON" "$TESTTMP/decodecbor.py"
+  [
+   '?'
+  ]
+  $ HGENCODING=latin-1 hg log -T "{branch|cbor}" -r0 \
+  > | "$PYTHON" "$TESTTMP/decodecbor.py"
+  [
+   '\xe9'
+  ]
+
 utf8 filter:
 
   $ HGENCODING=ascii hg log -T "round-trip: {branch|utf8|hex}\n" -r0