diff mercurial/utils/cborutil.py @ 42176:b6387a65851d stable

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'>
author Yuya Nishihara <yuya@tcha.org>
date Sun, 16 Jun 2019 12:31:07 +0900
parents b638219a23c3
children 2372284d9457
line wrap: on
line diff
--- 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)