Skip to content

Commit 65e6fe4

Browse files
tacaswellQuLogic
andcommitted
MNT: Also handle general descriptors in _setattor_cm
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
1 parent a436253 commit 65e6fe4

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

lib/matplotlib/cbook/__init__.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import contextlib
1212
import functools
1313
import gzip
14+
import inspect
1415
import itertools
1516
import math
1617
import operator
@@ -1961,6 +1962,10 @@ def _array_patch_perimeters(x, rstride, cstride):
19611962
def _setattr_cm(obj, **kwargs):
19621963
"""
19631964
Temporarily set some attributes; restore original state at context exit.
1965+
1966+
.. warning ::
1967+
1968+
This is not threadsafe.
19641969
"""
19651970
sentinel = object()
19661971
origs = {}
@@ -1978,14 +1983,18 @@ def _setattr_cm(obj, **kwargs):
19781983
# we want to set the original value back.
19791984
if isinstance(cls_orig, property):
19801985
origs[attr] = orig
1986+
1987+
# detect when we have Python Descriptors that supports
1988+
# setting so we will need to restore it
1989+
#
1990+
# https://docs.python.org/3/howto/descriptor.html
1991+
if hasattr(inspect.getattr_static(type(obj), attr), '__set__'):
1992+
origs[attr] = orig
1993+
19811994
# otherwise this is _something_ we are going to shadow at
19821995
# the instance dict level from higher up in the MRO. We
19831996
# are going to assume we can delattr(obj, attr) to clean
1984-
# up after ourselves. It is possible that this code will
1985-
# fail if used with a non-property custom descriptor which
1986-
# implements __set__ (and __delete__ does not act like a
1987-
# stack). However, this is an internal tool and we do not
1988-
# currently have any custom descriptors.
1997+
# up after ourselves.
19891998
else:
19901999
origs[attr] = sentinel
19912000

0 commit comments

Comments
 (0)