Issue #22138: Fix mock.patch behavior when patching descriptors. Restore

original values after patching.

Patch contributed by Sean McCully.
This commit is contained in:
Senthil Kumaran 2016-01-08 23:43:29 -08:00
parent cbe6356c42
commit 81bc927da7
3 changed files with 33 additions and 1 deletions

View file

@ -1332,7 +1332,10 @@ class _patch(object):
setattr(self.target, self.attribute, self.temp_original)
else:
delattr(self.target, self.attribute)
if not self.create and not hasattr(self.target, self.attribute):
if not self.create and (not hasattr(self.target, self.attribute) or
self.attribute in ('__doc__', '__module__',
'__defaults__', '__annotations__',
'__kwdefaults__')):
# needed for proxy objects like django settings
setattr(self.target, self.attribute, self.temp_original)