prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  SQS queue name gets mangled
  Using Celery/Kombu in an environment with _future_ strings support you end up with mangled queue names in SQS.
  
  The code that tries to translate invalid SQS queue characters causes the byte string to get converted to a string representation such as b'my_queue_name' and then translated that to 'b_my_queue_name'
  
  I am finding this because we are using Apache Airflow in a Python 2.7 environment using SQS queues as the broker.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `kombu/utils/encoding.py`
  Content:
  ```
  1 # -*- coding: utf-8 -*-
  2 """Text encoding utilities.
  3 
  4 Utilities to encode text, and to safely emit text from running
  5 applications without crashing from the infamous
  6 :exc:`UnicodeDecodeError` exception.
  7 """
  8 from __future__ import absolute_import, unicode_literals
  9 
  10 import sys
  11 import traceback
  12 
  13 from kombu.five import text_t
  14 
  15 is_py3k = sys.version_info >= (3, 0)
  16 
  17 #: safe_str takes encoding from this file by default.
  18 #: :func:`set_default_encoding_file` can used to set the
  19 #: default output file.
  20 default_encoding_file = None
  21 
  22 
  23 def set_default_encoding_file(file):
  24     """Set file used to get codec information."""
  25     global default_encoding_file
  26     default_encoding_file = file
  27 
  28 
  29 def get_default_encoding_file():
  30     """Get file used to get codec information."""
  31     return default_encoding_file
  32 
  33 
  34 if sys.platform.startswith('java'):     # pragma: no cover
  35 
  36     def default_encoding(file=None):
  37         """Get default encoding."""
  38         return 'utf-8'
  39 else:
  40 
  41     def default_encoding(file=None):  # noqa
  42         """Get default encoding."""
  43         file = file or get_default_encoding_file()
  44         return getattr(file, 'encoding', None) or sys.getfilesystemencoding()
  45 
  46 if is_py3k:  # pragma: no cover
  47 
  48     def str_to_bytes(s):
  49         """Convert str to bytes."""
  50         if isinstance(s, str):
  51             return s.encode()
  52         return s
  53 
  54     def bytes_to_str(s):
  55         """Convert bytes to str."""
  56         if isinstance(s, bytes):
  57             return s.decode()
  58         return s
  59 
  60     def from_utf8(s, *args, **kwargs):
  61         """Get str from utf-8 encoding."""
  62         return s
  63 
  64     def ensure_bytes(s):
  65         """Ensure s is bytes, not str."""
  66         if not isinstance(s, bytes):
  67             return str_to_bytes(s)
  68         return s
  69 
  70     def default_encode(obj):
  71         """Encode using default encoding."""
  72         return obj
  73 
  74     str_t = str
  75 
  76 else:
  77 
  78     def str_to_bytes(s):                # noqa
  79         """Convert str to bytes."""
  80         if isinstance(s, unicode):
  81             return s.encode()
  82         return s
  83 
  84     def bytes_to_str(s):                # noqa
  85         """Convert bytes to str."""
  86         return s
  87 
  88     def from_utf8(s, *args, **kwargs):  # noqa
  89         """Convert utf-8 to ASCII."""
  90         return s.encode('utf-8', *args, **kwargs)
  91 
  92     def default_encode(obj, file=None):            # noqa
  93         """Get default encoding."""
  94         return unicode(obj, default_encoding(file))
  95 
  96     str_t = unicode
  97     ensure_bytes = str_to_bytes
  98 
  99 
  100 try:
  101     bytes_t = bytes
  102 except NameError:  # pragma: no cover
  103     bytes_t = str  # noqa
  104 
  105 
  106 def safe_str(s, errors='replace'):
  107     """Safe form of str(), void of unicode errors."""
  108     s = bytes_to_str(s)
  109     if not isinstance(s, (text_t, bytes)):
  110         return safe_repr(s, errors)
  111     return _safe_str(s, errors)
  112 
  113 
  114 if is_py3k:  # pragma: no cover
  115 
  116     def _safe_str(s, errors='replace', file=None):
  117         if isinstance(s, str):
  118             return s
  119         try:
  120             return str(s)
  121         except Exception as exc:
  122             return '<Unrepresentable {0!r}: {1!r} {2!r}>'.format(
  123                 type(s), exc, '\n'.join(traceback.format_stack()))
  124 else:
  125     def _safe_str(s, errors='replace', file=None):  # noqa
  126         encoding = default_encoding(file)
  127         try:
  128             if isinstance(s, unicode):
  129                 return s.encode(encoding, errors)
  130             return unicode(s, encoding, errors)
  131         except Exception as exc:
  132             return '<Unrepresentable {0!r}: {1!r} {2!r}>'.format(
  133                 type(s), exc, '\n'.join(traceback.format_stack()))
  134 
  135 
  136 def safe_repr(o, errors='replace'):
  137     """Safe form of repr, void of Unicode errors."""
  138     try:
  139         return repr(o)
  140     except Exception:
  141         return _safe_str(o, errors)
  142
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/kombu/utils/encoding.py b/kombu/utils/encoding.py
  --- a/kombu/utils/encoding.py
  +++ b/kombu/utils/encoding.py
  @@ -122,11 +122,18 @@
               return '<Unrepresentable {0!r}: {1!r} {2!r}>'.format(
                   type(s), exc, '\n'.join(traceback.format_stack()))
   else:
  +    def _ensure_str(s, encoding, errors):
  +        if isinstance(s, bytes):
  +            return s.decode(encoding, errors)
  +        return s
  +
  +
       def _safe_str(s, errors='replace', file=None):  # noqa
           encoding = default_encoding(file)
           try:
               if isinstance(s, unicode):
  -                return s.encode(encoding, errors)
  +                return _ensure_str(s.encode(encoding, errors),
  +                                   encoding, errors)
               return unicode(s, encoding, errors)
           except Exception as exc:
               return '<Unrepresentable {0!r}: {1!r} {2!r}>'.format(
  
verification_info: {"golden_diff": "diff --git a/kombu/utils/encoding.py b/kombu/utils/encoding.py\n--- a/kombu/utils/encoding.py\n+++ b/kombu/utils/encoding.py\n@@ -122,11 +122,18 @@\n             return '<Unrepresentable {0!r}: {1!r} {2!r}>'.format(\n                 type(s), exc, '\\n'.join(traceback.format_stack()))\n else:\n+    def _ensure_str(s, encoding, errors):\n+        if isinstance(s, bytes):\n+            return s.decode(encoding, errors)\n+        return s\n+\n+\n     def _safe_str(s, errors='replace', file=None):  # noqa\n         encoding = default_encoding(file)\n         try:\n             if isinstance(s, unicode):\n-                return s.encode(encoding, errors)\n+                return _ensure_str(s.encode(encoding, errors),\n+                                   encoding, errors)\n             return unicode(s, encoding, errors)\n         except Exception as exc:\n             return '<Unrepresentable {0!r}: {1!r} {2!r}>'.format(\n", "issue": "SQS queue name gets mangled\nUsing Celery/Kombu in an environment with _future_ strings support you end up with mangled queue names in SQS.\r\n\r\nThe code that tries to translate invalid SQS queue characters causes the byte string to get converted to a string representation such as b'my_queue_name' and then translated that to 'b_my_queue_name'\r\n\r\nI am finding this because we are using Apache Airflow in a Python 2.7 environment using SQS queues as the broker.\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n\"\"\"Text encoding utilities.\n\nUtilities to encode text, and to safely emit text from running\napplications without crashing from the infamous\n:exc:`UnicodeDecodeError` exception.\n\"\"\"\nfrom __future__ import absolute_import, unicode_literals\n\nimport sys\nimport traceback\n\nfrom kombu.five import text_t\n\nis_py3k = sys.version_info >= (3, 0)\n\n#: safe_str takes encoding from this file by default.\n#: :func:`set_default_encoding_file` can used to set the\n#: default output file.\ndefault_encoding_file = None\n\n\ndef set_default_encoding_file(file):\n    \"\"\"Set file used to get codec information.\"\"\"\n    global default_encoding_file\n    default_encoding_file = file\n\n\ndef get_default_encoding_file():\n    \"\"\"Get file used to get codec information.\"\"\"\n    return default_encoding_file\n\n\nif sys.platform.startswith('java'):     # pragma: no cover\n\n    def default_encoding(file=None):\n        \"\"\"Get default encoding.\"\"\"\n        return 'utf-8'\nelse:\n\n    def default_encoding(file=None):  # noqa\n        \"\"\"Get default encoding.\"\"\"\n        file = file or get_default_encoding_file()\n        return getattr(file, 'encoding', None) or sys.getfilesystemencoding()\n\nif is_py3k:  # pragma: no cover\n\n    def str_to_bytes(s):\n        \"\"\"Convert str to bytes.\"\"\"\n        if isinstance(s, str):\n            return s.encode()\n        return s\n\n    def bytes_to_str(s):\n        \"\"\"Convert bytes to str.\"\"\"\n        if isinstance(s, bytes):\n            return s.decode()\n        return s\n\n    def from_utf8(s, *args, **kwargs):\n        \"\"\"Get str from utf-8 encoding.\"\"\"\n        return s\n\n    def ensure_bytes(s):\n        \"\"\"Ensure s is bytes, not str.\"\"\"\n        if not isinstance(s, bytes):\n            return str_to_bytes(s)\n        return s\n\n    def default_encode(obj):\n        \"\"\"Encode using default encoding.\"\"\"\n        return obj\n\n    str_t = str\n\nelse:\n\n    def str_to_bytes(s):                # noqa\n        \"\"\"Convert str to bytes.\"\"\"\n        if isinstance(s, unicode):\n            return s.encode()\n        return s\n\n    def bytes_to_str(s):                # noqa\n        \"\"\"Convert bytes to str.\"\"\"\n        return s\n\n    def from_utf8(s, *args, **kwargs):  # noqa\n        \"\"\"Convert utf-8 to ASCII.\"\"\"\n        return s.encode('utf-8', *args, **kwargs)\n\n    def default_encode(obj, file=None):            # noqa\n        \"\"\"Get default encoding.\"\"\"\n        return unicode(obj, default_encoding(file))\n\n    str_t = unicode\n    ensure_bytes = str_to_bytes\n\n\ntry:\n    bytes_t = bytes\nexcept NameError:  # pragma: no cover\n    bytes_t = str  # noqa\n\n\ndef safe_str(s, errors='replace'):\n    \"\"\"Safe form of str(), void of unicode errors.\"\"\"\n    s = bytes_to_str(s)\n    if not isinstance(s, (text_t, bytes)):\n        return safe_repr(s, errors)\n    return _safe_str(s, errors)\n\n\nif is_py3k:  # pragma: no cover\n\n    def _safe_str(s, errors='replace', file=None):\n        if isinstance(s, str):\n            return s\n        try:\n            return str(s)\n        except Exception as exc:\n            return '<Unrepresentable {0!r}: {1!r} {2!r}>'.format(\n                type(s), exc, '\\n'.join(traceback.format_stack()))\nelse:\n    def _safe_str(s, errors='replace', file=None):  # noqa\n        encoding = default_encoding(file)\n        try:\n            if isinstance(s, unicode):\n                return s.encode(encoding, errors)\n            return unicode(s, encoding, errors)\n        except Exception as exc:\n            return '<Unrepresentable {0!r}: {1!r} {2!r}>'.format(\n                type(s), exc, '\\n'.join(traceback.format_stack()))\n\n\ndef safe_repr(o, errors='replace'):\n    \"\"\"Safe form of repr, void of Unicode errors.\"\"\"\n    try:\n        return repr(o)\n    except Exception:\n        return _safe_str(o, errors)\n", "path": "kombu/utils/encoding.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n\"\"\"Text encoding utilities.\n\nUtilities to encode text, and to safely emit text from running\napplications without crashing from the infamous\n:exc:`UnicodeDecodeError` exception.\n\"\"\"\nfrom __future__ import absolute_import, unicode_literals\n\nimport sys\nimport traceback\n\nfrom kombu.five import text_t\n\nis_py3k = sys.version_info >= (3, 0)\n\n#: safe_str takes encoding from this file by default.\n#: :func:`set_default_encoding_file` can used to set the\n#: default output file.\ndefault_encoding_file = None\n\n\ndef set_default_encoding_file(file):\n    \"\"\"Set file used to get codec information.\"\"\"\n    global default_encoding_file\n    default_encoding_file = file\n\n\ndef get_default_encoding_file():\n    \"\"\"Get file used to get codec information.\"\"\"\n    return default_encoding_file\n\n\nif sys.platform.startswith('java'):     # pragma: no cover\n\n    def default_encoding(file=None):\n        \"\"\"Get default encoding.\"\"\"\n        return 'utf-8'\nelse:\n\n    def default_encoding(file=None):  # noqa\n        \"\"\"Get default encoding.\"\"\"\n        file = file or get_default_encoding_file()\n        return getattr(file, 'encoding', None) or sys.getfilesystemencoding()\n\nif is_py3k:  # pragma: no cover\n\n    def str_to_bytes(s):\n        \"\"\"Convert str to bytes.\"\"\"\n        if isinstance(s, str):\n            return s.encode()\n        return s\n\n    def bytes_to_str(s):\n        \"\"\"Convert bytes to str.\"\"\"\n        if isinstance(s, bytes):\n            return s.decode()\n        return s\n\n    def from_utf8(s, *args, **kwargs):\n        \"\"\"Get str from utf-8 encoding.\"\"\"\n        return s\n\n    def ensure_bytes(s):\n        \"\"\"Ensure s is bytes, not str.\"\"\"\n        if not isinstance(s, bytes):\n            return str_to_bytes(s)\n        return s\n\n    def default_encode(obj):\n        \"\"\"Encode using default encoding.\"\"\"\n        return obj\n\n    str_t = str\n\nelse:\n\n    def str_to_bytes(s):                # noqa\n        \"\"\"Convert str to bytes.\"\"\"\n        if isinstance(s, unicode):\n            return s.encode()\n        return s\n\n    def bytes_to_str(s):                # noqa\n        \"\"\"Convert bytes to str.\"\"\"\n        return s\n\n    def from_utf8(s, *args, **kwargs):  # noqa\n        \"\"\"Convert utf-8 to ASCII.\"\"\"\n        return s.encode('utf-8', *args, **kwargs)\n\n    def default_encode(obj, file=None):            # noqa\n        \"\"\"Get default encoding.\"\"\"\n        return unicode(obj, default_encoding(file))\n\n    str_t = unicode\n    ensure_bytes = str_to_bytes\n\n\ntry:\n    bytes_t = bytes\nexcept NameError:  # pragma: no cover\n    bytes_t = str  # noqa\n\n\ndef safe_str(s, errors='replace'):\n    \"\"\"Safe form of str(), void of unicode errors.\"\"\"\n    s = bytes_to_str(s)\n    if not isinstance(s, (text_t, bytes)):\n        return safe_repr(s, errors)\n    return _safe_str(s, errors)\n\n\nif is_py3k:  # pragma: no cover\n\n    def _safe_str(s, errors='replace', file=None):\n        if isinstance(s, str):\n            return s\n        try:\n            return str(s)\n        except Exception as exc:\n            return '<Unrepresentable {0!r}: {1!r} {2!r}>'.format(\n                type(s), exc, '\\n'.join(traceback.format_stack()))\nelse:\n    def _ensure_str(s, encoding, errors):\n        if isinstance(s, bytes):\n            return s.decode(encoding, errors)\n        return s\n\n\n    def _safe_str(s, errors='replace', file=None):  # noqa\n        encoding = default_encoding(file)\n        try:\n            if isinstance(s, unicode):\n                return _ensure_str(s.encode(encoding, errors),\n                                   encoding, errors)\n            return unicode(s, encoding, errors)\n        except Exception as exc:\n            return '<Unrepresentable {0!r}: {1!r} {2!r}>'.format(\n                type(s), exc, '\\n'.join(traceback.format_stack()))\n\n\ndef safe_repr(o, errors='replace'):\n    \"\"\"Safe form of repr, void of Unicode errors.\"\"\"\n    try:\n        return repr(o)\n    except Exception:\n        return _safe_str(o, errors)\n", "path": "kombu/utils/encoding.py"}]}