We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
_ModuleWithDeprecations doesn't handle patching properly.
`_ModuleWithDeprecations` catches `__getattr__` and `__setattr__` to patch through to the underlying module, but does not intercept `__delattr__`. That means that if you're using something like `mock.patch`, the mock successfully lands in place, but cannot be removed: the mock was applied to the underlying module, but the delete comes from the proxy.

Should be easily fixed.


--- 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 struct
11 import sys
12 import warnings
13 
14 
15 # the functions deprecated in 1.0 are on an arbitrarily extended deprecation
16 # cycle and should not be removed until we agree on when that cycle ends.
17 DeprecatedIn10 = DeprecationWarning
18 DeprecatedIn12 = DeprecationWarning
19 
20 
21 def read_only_property(name):
22     return property(lambda self: getattr(self, name))
23 
24 
25 def register_interface(iface):
26     def register_decorator(klass):
27         verify_interface(iface, klass)
28         iface.register(klass)
29         return klass
30     return register_decorator
31 
32 
33 if hasattr(int, "from_bytes"):
34     int_from_bytes = int.from_bytes
35 else:
36     def int_from_bytes(data, byteorder, signed=False):
37         assert byteorder == 'big'
38         assert not signed
39 
40         if len(data) % 4 != 0:
41             data = (b'\x00' * (4 - (len(data) % 4))) + data
42 
43         result = 0
44 
45         while len(data) > 0:
46             digit, = struct.unpack('>I', data[:4])
47             result = (result << 32) + digit
48             # TODO: this is quadratic in the length of data
49             data = data[4:]
50 
51         return result
52 
53 
54 def int_to_bytes(integer, length=None):
55     hex_string = '%x' % integer
56     if length is None:
57         n = len(hex_string)
58     else:
59         n = length * 2
60     return binascii.unhexlify(hex_string.zfill(n + (n & 1)))
61 
62 
63 class InterfaceNotImplemented(Exception):
64     pass
65 
66 
67 if hasattr(inspect, "signature"):
68     signature = inspect.signature
69 else:
70     signature = inspect.getargspec
71 
72 
73 def verify_interface(iface, klass):
74     for method in iface.__abstractmethods__:
75         if not hasattr(klass, method):
76             raise InterfaceNotImplemented(
77                 "{0} is missing a {1!r} method".format(klass, method)
78             )
79         if isinstance(getattr(iface, method), abc.abstractproperty):
80             # Can't properly verify these yet.
81             continue
82         sig = signature(getattr(iface, method))
83         actual = signature(getattr(klass, method))
84         if sig != actual:
85             raise InterfaceNotImplemented(
86                 "{0}.{1}'s signature differs from the expected. Expected: "
87                 "{2!r}. Received: {3!r}".format(
88                     klass, method, sig, actual
89                 )
90             )
91 
92 
93 if sys.version_info >= (2, 7):
94     def bit_length(x):
95         return x.bit_length()
96 else:
97     def bit_length(x):
98         return len(bin(x)) - (2 + (x <= 0))
99 
100 
101 class _DeprecatedValue(object):
102     def __init__(self, value, message, warning_class):
103         self.value = value
104         self.message = message
105         self.warning_class = warning_class
106 
107 
108 class _ModuleWithDeprecations(object):
109     def __init__(self, module):
110         self.__dict__["_module"] = module
111 
112     def __getattr__(self, attr):
113         obj = getattr(self._module, attr)
114         if isinstance(obj, _DeprecatedValue):
115             warnings.warn(obj.message, obj.warning_class, stacklevel=2)
116             obj = obj.value
117         return obj
118 
119     def __setattr__(self, attr, value):
120         setattr(self._module, attr, value)
121 
122     def __dir__(self):
123         return ["_module"] + dir(self._module)
124 
125 
126 def deprecated(value, module_name, message, warning_class):
127     module = sys.modules[module_name]
128     if not isinstance(module, _ModuleWithDeprecations):
129         sys.modules[module_name] = module = _ModuleWithDeprecations(module)
130     return _DeprecatedValue(value, message, warning_class)
131
```
--- 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
@@ -119,6 +119,13 @@
     def __setattr__(self, attr, value):
         setattr(self._module, attr, value)
 
+    def __delattr__(self, attr):
+        obj = getattr(self._module, attr)
+        if isinstance(obj, _DeprecatedValue):
+            warnings.warn(obj.message, obj.warning_class, stacklevel=2)
+
+        delattr(self._module, attr)
+
     def __dir__(self):
         return ["_module"] + dir(self._module)

{"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@@ -119,6 +119,13 @@\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", "issue": "_ModuleWithDeprecations doesn't handle patching properly.\n`_ModuleWithDeprecations` catches `__getattr__` and `__setattr__` to patch through to the underlying module, but does not intercept `__delattr__`. That means that if you're using something like `mock.patch`, the mock successfully lands in place, but cannot be removed: the mock was applied to the underlying module, but the delete comes from the proxy.\n\nShould be easily fixed.\n\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 struct\nimport sys\nimport warnings\n\n\n# the functions deprecated in 1.0 are on an arbitrarily extended deprecation\n# cycle and should not be removed until we agree on when that cycle ends.\nDeprecatedIn10 = DeprecationWarning\nDeprecatedIn12 = DeprecationWarning\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\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        if len(data) % 4 != 0:\n            data = (b'\\x00' * (4 - (len(data) % 4))) + data\n\n        result = 0\n\n        while len(data) > 0:\n            digit, = struct.unpack('>I', data[:4])\n            result = (result << 32) + digit\n            # TODO: this is quadratic in the length of data\n            data = data[4:]\n\n        return result\n\n\ndef 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\nif sys.version_info >= (2, 7):\n    def bit_length(x):\n        return x.bit_length()\nelse:\n    def bit_length(x):\n        return len(bin(x)) - (2 + (x <= 0))\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 __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] = module = _ModuleWithDeprecations(module)\n    return _DeprecatedValue(value, message, warning_class)\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 struct\nimport sys\nimport warnings\n\n\n# the functions deprecated in 1.0 are on an arbitrarily extended deprecation\n# cycle and should not be removed until we agree on when that cycle ends.\nDeprecatedIn10 = DeprecationWarning\nDeprecatedIn12 = DeprecationWarning\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\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        if len(data) % 4 != 0:\n            data = (b'\\x00' * (4 - (len(data) % 4))) + data\n\n        result = 0\n\n        while len(data) > 0:\n            digit, = struct.unpack('>I', data[:4])\n            result = (result << 32) + digit\n            # TODO: this is quadratic in the length of data\n            data = data[4:]\n\n        return result\n\n\ndef 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\nif sys.version_info >= (2, 7):\n    def bit_length(x):\n        return x.bit_length()\nelse:\n    def bit_length(x):\n        return len(bin(x)) - (2 + (x <= 0))\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] = module = _ModuleWithDeprecations(module)\n    return _DeprecatedValue(value, message, warning_class)\n", "path": "src/cryptography/utils.py"}]}