Explorar o código

saferepr: bytes.hex() not available on Python 3.4

Ask Solem %!s(int64=8) %!d(string=hai) anos
pai
achega
c3fb931372
Modificáronse 1 ficheiros con 8 adicións e 1 borrados
  1. 8 1
      celery/utils/saferepr.py

+ 8 - 1
celery/utils/saferepr.py

@@ -121,7 +121,14 @@ def _repr_binary_bytes(val):
         return val.decode('utf-8')
     except UnicodeDecodeError:
         # possibly not unicode, but binary data so format as hex.
-        return val.hex()
+        try:
+            ashex = val.hex
+        except AttributeError:  # pragma: no cover
+            # Python 3.4
+            return val.decode('utf-8', errors='backslashreplace')
+        else:
+            # Python 3.5+
+            return ashex()
 
 
 def _format_chars(val, maxlen):