# HG changeset patch # User Yuya Nishihara # Date 1560655867 -32400 # Node ID b6387a65851d4421d5580b1a4db4c55366a94ec8 # 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 diff -r d532292eff22 -r b6387a65851d mercurial/utils/cborutil.py --- a/mercurial/utils/cborutil.py Fri May 31 22:37:14 2019 -0700 +++ b/mercurial/utils/cborutil.py Sun Jun 16 12:31:07 2019 +0900 @@ -214,6 +214,14 @@ 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 -r d532292eff22 -r b6387a65851d tests/test-template-functions.t --- a/tests/test-template-functions.t Fri May 31 22:37:14 2019 -0700 +++ b/tests/test-template-functions.t Sun Jun 16 12:31:07 2019 +0900 @@ -1562,6 +1562,20 @@ $ 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