Browse Source

saferepr: Support unicode bytes on Python 2. Closes #3676

Ask Solem 8 years ago
parent
commit
a223318573
2 changed files with 13 additions and 4 deletions
  1. 8 3
      celery/utils/saferepr.py
  2. 5 1
      t/unit/utils/test_saferepr.py

+ 8 - 3
celery/utils/saferepr.py

@@ -139,8 +139,13 @@ def _format_binary_bytes(val, maxlen, ellipsis='...'):
     if maxlen and len(val) > maxlen:
         # we don't want to copy all the data, just take what we need.
         chunk = memoryview(val)[:maxlen].tobytes()
-        return "b'{0}{1}'".format(_repr_binary_bytes(chunk), ellipsis)
-    return "b'{0}'".format(_repr_binary_bytes(val))
+        return _bytes_prefix("'{0}{1}'".format(
+            _repr_binary_bytes(chunk), ellipsis))
+    return _bytes_prefix("'{0}'".format(_repr_binary_bytes(val)))
+
+
+def _bytes_prefix(s):
+    return 'b' + s if IS_PY3 else s
 
 
 def _repr_binary_bytes(val):
@@ -161,7 +166,7 @@ def _repr_binary_bytes(val):
 
 def _format_chars(val, maxlen):
     # type: (AnyStr, int) -> str
-    if IS_PY3 and isinstance(val, bytes):  # pragma: no cover
+    if isinstance(val, bytes):  # pragma: no cover
         return _format_binary_bytes(val, maxlen)
     else:
         return "'{0}'".format(truncate(val, maxlen))

+ 5 - 1
t/unit/utils/test_saferepr.py

@@ -62,7 +62,8 @@ def old_repr(s):
             RE_OLD_SET_REPR.sub(
                 RE_OLD_SET_REPR_REPLACE,
                 RE_OLD_SET_CUSTOM_REPR.sub(
-                    RE_OLD_SET_CUSTOM_REPR_REPLACE, repr(s).replace("u'", "'"),
+                    RE_OLD_SET_CUSTOM_REPR_REPLACE,
+                    repr(s).replace("u'", "'"),
                 )
             ),
         ),
@@ -228,3 +229,6 @@ class test_saferepr:
             def __repr__(self):
                 raise KeyError('foo')
         assert 'Unrepresentable' in saferepr(O())
+
+    def test_bytes_with_unicode(self):
+        assert saferepr([b'foo', 'a®rgs'.encode()])