1
1
import functools
2
2
import inspect
3
- import threading
4
3
import types
5
4
from typing import Any , Callable
6
5
@@ -73,7 +72,6 @@ def proxy(*args, **kwargs):
73
72
74
73
class Patch :
75
74
applied_patches : list ["Patch" ] = []
76
- _lock : threading .RLock = threading .RLock ()
77
75
"""Bookkeeping for patches that are applied. You can use this to debug patches. For instance,
78
76
you could write something like::
79
77
@@ -99,27 +97,25 @@ def __init__(self, obj: Any, name: str, new: Any) -> None:
99
97
self .is_applied = False
100
98
101
99
def apply (self ):
102
- with self ._lock :
103
- if self .is_applied :
104
- return
105
-
106
- if self .old and self .name == "__getattr__" :
107
- raise Exception ("You can't patch class types implementing __getattr__" )
108
- if not self .old and self .name != "__getattr__" :
109
- raise AttributeError (f"`{ self .obj .__name__ } ` object has no attribute `{ self .name } `" )
110
- setattr (self .obj , self .name , self .new )
111
- Patch .applied_patches .append (self )
112
- self .is_applied = True
100
+ if self .is_applied :
101
+ return
102
+
103
+ if self .old and self .name == "__getattr__" :
104
+ raise Exception ("You can't patch class types implementing __getattr__" )
105
+ if not self .old and self .name != "__getattr__" :
106
+ raise AttributeError (f"`{ self .obj .__name__ } ` object has no attribute `{ self .name } `" )
107
+ setattr (self .obj , self .name , self .new )
108
+ Patch .applied_patches .append (self )
109
+ self .is_applied = True
113
110
114
111
def undo (self ):
115
- with self ._lock :
116
- if not self .is_applied :
117
- return
118
-
119
- # If we added a method to a class type, we don't have a self.old. We just delete __getattr__
120
- setattr (self .obj , self .name , self .old ) if self .old else delattr (self .obj , self .name )
121
- Patch .applied_patches .remove (self )
122
- self .is_applied = False
112
+ if not self .is_applied :
113
+ return
114
+
115
+ # If we added a method to a class type, we don't have a self.old. We just delete __getattr__
116
+ setattr (self .obj , self .name , self .old ) if self .old else delattr (self .obj , self .name )
117
+ Patch .applied_patches .remove (self )
118
+ self .is_applied = False
123
119
124
120
def __enter__ (self ):
125
121
self .apply ()
0 commit comments