We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
utils.int_from_bytes gives incorrect answers when passed "builtins.bytes" in python 2.7
```
$ mkvirtualenv repro
$ python --version
Python 2.7.12
$ pip install cryptography future
$ python

from cryptography import utils
from builtins import bytes
x = bytes.fromhex('deadbeef')
y = utils.int_from_bytes(x, 'big')
hex(y)
'0x6227deadbeef27'
```

The reason this happens is that `int_from_bytes` (in py27 mode) casts the passed-in value to `bytes`, which, in py27 mode, is an alias for `str`. Passing a `builtins.bytes` value to `str` somewhat insanely wraps the string with `b'` and `'`. These then get parsed by the rest of `int_from_bytes` as if they were part of the original byte string.

I think this is particularly unfortunate since all the "cryptography" functions say they accept and return `bytes` in their docstrings. Ideally it'd be compatible with all three definitions of `bytes`: the py27 alias to `str`, the one from "future", and the py3 one.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `src/cryptography/utils.py`
Content:
```
1 # This file is dual licensed under the terms of the Apache License, Version
2 # 2.0, and the BSD License. See the LICENSE file in the root of this repository
3 # for complete details.
4 
5 from __future__ import absolute_import, division, print_function
6 
7 import abc
8 import binascii
9 import inspect
10 import sys
11 import warnings
12 
13 
14 # We use a UserWarning subclass, instead of DeprecationWarning, because CPython
15 # decided deprecation warnings should be invisble by default.
16 class CryptographyDeprecationWarning(UserWarning):
17     pass
18 
19 
20 # Several APIs were deprecated with no specific end-of-life date because of the
21 # ubiquity of their use. They should not be removed until we agree on when that
22 # cycle ends.
23 PersistentlyDeprecated = CryptographyDeprecationWarning
24 DeprecatedIn21 = CryptographyDeprecationWarning
25 
26 
27 def _check_bytes(name, value):
28     if not isinstance(value, bytes):
29         raise TypeError("{0} must be bytes".format(name))
30 
31 
32 def read_only_property(name):
33     return property(lambda self: getattr(self, name))
34 
35 
36 def register_interface(iface):
37     def register_decorator(klass):
38         verify_interface(iface, klass)
39         iface.register(klass)
40         return klass
41     return register_decorator
42 
43 
44 def register_interface_if(predicate, iface):
45     def register_decorator(klass):
46         if predicate:
47             verify_interface(iface, klass)
48             iface.register(klass)
49         return klass
50     return register_decorator
51 
52 
53 if hasattr(int, "from_bytes"):
54     int_from_bytes = int.from_bytes
55 else:
56     def int_from_bytes(data, byteorder, signed=False):
57         assert byteorder == 'big'
58         assert not signed
59 
60         # call bytes() on data to allow the use of bytearrays
61         return int(bytes(data).encode('hex'), 16)
62 
63 
64 if hasattr(int, "to_bytes"):
65     def int_to_bytes(integer, length=None):
66         return integer.to_bytes(
67             length or (integer.bit_length() + 7) // 8 or 1, 'big'
68         )
69 else:
70     def int_to_bytes(integer, length=None):
71         hex_string = '%x' % integer
72         if length is None:
73             n = len(hex_string)
74         else:
75             n = length * 2
76         return binascii.unhexlify(hex_string.zfill(n + (n & 1)))
77 
78 
79 class InterfaceNotImplemented(Exception):
80     pass
81 
82 
83 if hasattr(inspect, "signature"):
84     signature = inspect.signature
85 else:
86     signature = inspect.getargspec
87 
88 
89 def verify_interface(iface, klass):
90     for method in iface.__abstractmethods__:
91         if not hasattr(klass, method):
92             raise InterfaceNotImplemented(
93                 "{0} is missing a {1!r} method".format(klass, method)
94             )
95         if isinstance(getattr(iface, method), abc.abstractproperty):
96             # Can't properly verify these yet.
97             continue
98         sig = signature(getattr(iface, method))
99         actual = signature(getattr(klass, method))
100         if sig != actual:
101             raise InterfaceNotImplemented(
102                 "{0}.{1}'s signature differs from the expected. Expected: "
103                 "{2!r}. Received: {3!r}".format(
104                     klass, method, sig, actual
105                 )
106             )
107 
108 
109 # No longer needed as of 2.2, but retained because we have external consumers
110 # who use it.
111 def bit_length(x):
112     return x.bit_length()
113 
114 
115 class _DeprecatedValue(object):
116     def __init__(self, value, message, warning_class):
117         self.value = value
118         self.message = message
119         self.warning_class = warning_class
120 
121 
122 class _ModuleWithDeprecations(object):
123     def __init__(self, module):
124         self.__dict__["_module"] = module
125 
126     def __getattr__(self, attr):
127         obj = getattr(self._module, attr)
128         if isinstance(obj, _DeprecatedValue):
129             warnings.warn(obj.message, obj.warning_class, stacklevel=2)
130             obj = obj.value
131         return obj
132 
133     def __setattr__(self, attr, value):
134         setattr(self._module, attr, value)
135 
136     def __delattr__(self, attr):
137         obj = getattr(self._module, attr)
138         if isinstance(obj, _DeprecatedValue):
139             warnings.warn(obj.message, obj.warning_class, stacklevel=2)
140 
141         delattr(self._module, attr)
142 
143     def __dir__(self):
144         return ["_module"] + dir(self._module)
145 
146 
147 def deprecated(value, module_name, message, warning_class):
148     module = sys.modules[module_name]
149     if not isinstance(module, _ModuleWithDeprecations):
150         sys.modules[module_name] = _ModuleWithDeprecations(module)
151     return _DeprecatedValue(value, message, warning_class)
152 
153 
154 def cached_property(func):
155     cached_name = "_cached_{0}".format(func)
156     sentinel = object()
157 
158     def inner(instance):
159         cache = getattr(instance, cached_name, sentinel)
160         if cache is not sentinel:
161             return cache
162         result = func(instance)
163         setattr(instance, cached_name, result)
164         return result
165     return property(inner)
166
```
--- 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()

```

diff --git a/src/cryptography/utils.py b/src/cryptography/utils.py
--- a/src/cryptography/utils.py
+++ b/src/cryptography/utils.py
@@ -57,8 +57,7 @@
         assert byteorder == 'big'
         assert not signed
 
-        # call bytes() on data to allow the use of bytearrays
-        return int(bytes(data).encode('hex'), 16)
+        return int(binascii.hexlify(data), 16)
 
 
 if hasattr(int, "to_bytes"):

{"golden_diff": "diff --git a/src/cryptography/utils.py b/src/cryptography/utils.py\n--- a/src/cryptography/utils.py\n+++ b/src/cryptography/utils.py\n@@ -57,8 +57,7 @@\n         assert byteorder == 'big'\n         assert not signed\n \n-        # call bytes() on data to allow the use of bytearrays\n-        return int(bytes(data).encode('hex'), 16)\n+        return int(binascii.hexlify(data), 16)\n \n \n if hasattr(int, \"to_bytes\"):\n", "issue": "utils.int_from_bytes gives incorrect answers when passed \"builtins.bytes\" in python 2.7\n```\r\n$ mkvirtualenv repro\r\n$ python --version\r\nPython 2.7.12\r\n$ pip install cryptography future\r\n$ python\r\n\r\nfrom cryptography import utils\r\nfrom builtins import bytes\r\nx = bytes.fromhex('deadbeef')\r\ny = utils.int_from_bytes(x, 'big')\r\nhex(y)\r\n'0x6227deadbeef27'\r\n```\r\n\r\nThe reason this happens is that `int_from_bytes` (in py27 mode) casts the passed-in value to `bytes`, which, in py27 mode, is an alias for `str`. Passing a `builtins.bytes` value to `str` somewhat insanely wraps the string with `b'` and `'`. These then get parsed by the rest of `int_from_bytes` as if they were part of the original byte string.\r\n\r\nI think this is particularly unfortunate since all the \"cryptography\" functions say they accept and return `bytes` in their docstrings. Ideally it'd be compatible with all three definitions of `bytes`: the py27 alias to `str`, the one from \"future\", and the py3 one.\n", "before_files": [{"content": "# This file is dual licensed under the terms of the Apache License, Version\n# 2.0, and the BSD License. See the LICENSE file in the root of this repository\n# for complete details.\n\nfrom __future__ import absolute_import, division, print_function\n\nimport abc\nimport binascii\nimport inspect\nimport sys\nimport warnings\n\n\n# We use a UserWarning subclass, instead of DeprecationWarning, because CPython\n# decided deprecation warnings should be invisble by default.\nclass CryptographyDeprecationWarning(UserWarning):\n    pass\n\n\n# Several APIs were deprecated with no specific end-of-life date because of the\n# ubiquity of their use. They should not be removed until we agree on when that\n# cycle ends.\nPersistentlyDeprecated = CryptographyDeprecationWarning\nDeprecatedIn21 = CryptographyDeprecationWarning\n\n\ndef _check_bytes(name, value):\n    if not isinstance(value, bytes):\n        raise TypeError(\"{0} must be bytes\".format(name))\n\n\ndef read_only_property(name):\n    return property(lambda self: getattr(self, name))\n\n\ndef register_interface(iface):\n    def register_decorator(klass):\n        verify_interface(iface, klass)\n        iface.register(klass)\n        return klass\n    return register_decorator\n\n\ndef register_interface_if(predicate, iface):\n    def register_decorator(klass):\n        if predicate:\n            verify_interface(iface, klass)\n            iface.register(klass)\n        return klass\n    return register_decorator\n\n\nif hasattr(int, \"from_bytes\"):\n    int_from_bytes = int.from_bytes\nelse:\n    def int_from_bytes(data, byteorder, signed=False):\n        assert byteorder == 'big'\n        assert not signed\n\n        # call bytes() on data to allow the use of bytearrays\n        return int(bytes(data).encode('hex'), 16)\n\n\nif hasattr(int, \"to_bytes\"):\n    def int_to_bytes(integer, length=None):\n        return integer.to_bytes(\n            length or (integer.bit_length() + 7) // 8 or 1, 'big'\n        )\nelse:\n    def int_to_bytes(integer, length=None):\n        hex_string = '%x' % integer\n        if length is None:\n            n = len(hex_string)\n        else:\n            n = length * 2\n        return binascii.unhexlify(hex_string.zfill(n + (n & 1)))\n\n\nclass InterfaceNotImplemented(Exception):\n    pass\n\n\nif hasattr(inspect, \"signature\"):\n    signature = inspect.signature\nelse:\n    signature = inspect.getargspec\n\n\ndef verify_interface(iface, klass):\n    for method in iface.__abstractmethods__:\n        if not hasattr(klass, method):\n            raise InterfaceNotImplemented(\n                \"{0} is missing a {1!r} method\".format(klass, method)\n            )\n        if isinstance(getattr(iface, method), abc.abstractproperty):\n            # Can't properly verify these yet.\n            continue\n        sig = signature(getattr(iface, method))\n        actual = signature(getattr(klass, method))\n        if sig != actual:\n            raise InterfaceNotImplemented(\n                \"{0}.{1}'s signature differs from the expected. Expected: \"\n                \"{2!r}. Received: {3!r}\".format(\n                    klass, method, sig, actual\n                )\n            )\n\n\n# No longer needed as of 2.2, but retained because we have external consumers\n# who use it.\ndef bit_length(x):\n    return x.bit_length()\n\n\nclass _DeprecatedValue(object):\n    def __init__(self, value, message, warning_class):\n        self.value = value\n        self.message = message\n        self.warning_class = warning_class\n\n\nclass _ModuleWithDeprecations(object):\n    def __init__(self, module):\n        self.__dict__[\"_module\"] = module\n\n    def __getattr__(self, attr):\n        obj = getattr(self._module, attr)\n        if isinstance(obj, _DeprecatedValue):\n            warnings.warn(obj.message, obj.warning_class, stacklevel=2)\n            obj = obj.value\n        return obj\n\n    def __setattr__(self, attr, value):\n        setattr(self._module, attr, value)\n\n    def __delattr__(self, attr):\n        obj = getattr(self._module, attr)\n        if isinstance(obj, _DeprecatedValue):\n            warnings.warn(obj.message, obj.warning_class, stacklevel=2)\n\n        delattr(self._module, attr)\n\n    def __dir__(self):\n        return [\"_module\"] + dir(self._module)\n\n\ndef deprecated(value, module_name, message, warning_class):\n    module = sys.modules[module_name]\n    if not isinstance(module, _ModuleWithDeprecations):\n        sys.modules[module_name] = _ModuleWithDeprecations(module)\n    return _DeprecatedValue(value, message, warning_class)\n\n\ndef cached_property(func):\n    cached_name = \"_cached_{0}\".format(func)\n    sentinel = object()\n\n    def inner(instance):\n        cache = getattr(instance, cached_name, sentinel)\n        if cache is not sentinel:\n            return cache\n        result = func(instance)\n        setattr(instance, cached_name, result)\n        return result\n    return property(inner)\n", "path": "src/cryptography/utils.py"}], "after_files": [{"content": "# This file is dual licensed under the terms of the Apache License, Version\n# 2.0, and the BSD License. See the LICENSE file in the root of this repository\n# for complete details.\n\nfrom __future__ import absolute_import, division, print_function\n\nimport abc\nimport binascii\nimport inspect\nimport sys\nimport warnings\n\n\n# We use a UserWarning subclass, instead of DeprecationWarning, because CPython\n# decided deprecation warnings should be invisble by default.\nclass CryptographyDeprecationWarning(UserWarning):\n    pass\n\n\n# Several APIs were deprecated with no specific end-of-life date because of the\n# ubiquity of their use. They should not be removed until we agree on when that\n# cycle ends.\nPersistentlyDeprecated = CryptographyDeprecationWarning\nDeprecatedIn21 = CryptographyDeprecationWarning\n\n\ndef _check_bytes(name, value):\n    if not isinstance(value, bytes):\n        raise TypeError(\"{0} must be bytes\".format(name))\n\n\ndef read_only_property(name):\n    return property(lambda self: getattr(self, name))\n\n\ndef register_interface(iface):\n    def register_decorator(klass):\n        verify_interface(iface, klass)\n        iface.register(klass)\n        return klass\n    return register_decorator\n\n\ndef register_interface_if(predicate, iface):\n    def register_decorator(klass):\n        if predicate:\n            verify_interface(iface, klass)\n            iface.register(klass)\n        return klass\n    return register_decorator\n\n\nif hasattr(int, \"from_bytes\"):\n    int_from_bytes = int.from_bytes\nelse:\n    def int_from_bytes(data, byteorder, signed=False):\n        assert byteorder == 'big'\n        assert not signed\n\n        return int(binascii.hexlify(data), 16)\n\n\nif hasattr(int, \"to_bytes\"):\n    def int_to_bytes(integer, length=None):\n        return integer.to_bytes(\n            length or (integer.bit_length() + 7) // 8 or 1, 'big'\n        )\nelse:\n    def int_to_bytes(integer, length=None):\n        hex_string = '%x' % integer\n        if length is None:\n            n = len(hex_string)\n        else:\n            n = length * 2\n        return binascii.unhexlify(hex_string.zfill(n + (n & 1)))\n\n\nclass InterfaceNotImplemented(Exception):\n    pass\n\n\nif hasattr(inspect, \"signature\"):\n    signature = inspect.signature\nelse:\n    signature = inspect.getargspec\n\n\ndef verify_interface(iface, klass):\n    for method in iface.__abstractmethods__:\n        if not hasattr(klass, method):\n            raise InterfaceNotImplemented(\n                \"{0} is missing a {1!r} method\".format(klass, method)\n            )\n        if isinstance(getattr(iface, method), abc.abstractproperty):\n            # Can't properly verify these yet.\n            continue\n        sig = signature(getattr(iface, method))\n        actual = signature(getattr(klass, method))\n        if sig != actual:\n            raise InterfaceNotImplemented(\n                \"{0}.{1}'s signature differs from the expected. Expected: \"\n                \"{2!r}. Received: {3!r}\".format(\n                    klass, method, sig, actual\n                )\n            )\n\n\n# No longer needed as of 2.2, but retained because we have external consumers\n# who use it.\ndef bit_length(x):\n    return x.bit_length()\n\n\nclass _DeprecatedValue(object):\n    def __init__(self, value, message, warning_class):\n        self.value = value\n        self.message = message\n        self.warning_class = warning_class\n\n\nclass _ModuleWithDeprecations(object):\n    def __init__(self, module):\n        self.__dict__[\"_module\"] = module\n\n    def __getattr__(self, attr):\n        obj = getattr(self._module, attr)\n        if isinstance(obj, _DeprecatedValue):\n            warnings.warn(obj.message, obj.warning_class, stacklevel=2)\n            obj = obj.value\n        return obj\n\n    def __setattr__(self, attr, value):\n        setattr(self._module, attr, value)\n\n    def __delattr__(self, attr):\n        obj = getattr(self._module, attr)\n        if isinstance(obj, _DeprecatedValue):\n            warnings.warn(obj.message, obj.warning_class, stacklevel=2)\n\n        delattr(self._module, attr)\n\n    def __dir__(self):\n        return [\"_module\"] + dir(self._module)\n\n\ndef deprecated(value, module_name, message, warning_class):\n    module = sys.modules[module_name]\n    if not isinstance(module, _ModuleWithDeprecations):\n        sys.modules[module_name] = _ModuleWithDeprecations(module)\n    return _DeprecatedValue(value, message, warning_class)\n\n\ndef cached_property(func):\n    cached_name = \"_cached_{0}\".format(func)\n    sentinel = object()\n\n    def inner(instance):\n        cache = getattr(instance, cached_name, sentinel)\n        if cache is not sentinel:\n            return cache\n        result = func(instance)\n        setattr(instance, cached_name, result)\n        return result\n    return property(inner)\n", "path": "src/cryptography/utils.py"}]}