Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit136fa20

Browse files
committed
MNT: don't set method objects back onto the objects
If we are monkey-patching methods then we need to be sure that whenwe restore the original attribute we need to make sure we don'tstick the method instance in place (which creates a circularreference).
1 parent4356b67 commit136fa20

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

‎lib/matplotlib/cbook/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
importtypes
2525
importwarnings
2626
importweakref
27+
importinspect
2728

2829
importnumpyasnp
2930

@@ -2039,7 +2040,7 @@ def _setattr_cm(obj, **kwargs):
20392040
yield
20402041
finally:
20412042
forattr,originorigs:
2042-
iforigissentinel:
2043+
iforigissentinelorinspect.ismethod(orig):
20432044
delattr(obj,attr)
20442045
else:
20452046
setattr(obj,attr,orig)

‎lib/matplotlib/tests/test_cbook.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,3 +651,41 @@ def test_check_shape(target, test_shape):
651651
withpytest.raises(ValueError,
652652
match=error_pattern):
653653
cbook._check_shape(target,aardvark=data)
654+
655+
656+
deftest_setattr_cm():
657+
classA:
658+
def__init__(self):
659+
self.aardvark='aardvark'
660+
self._p='p'
661+
662+
defmeth(self):
663+
...
664+
665+
@property
666+
defprop(self):
667+
returnself._p
668+
669+
@prop.setter
670+
defprop(self,val):
671+
self._p=val
672+
673+
a=A()
674+
675+
assertid(a.meth)!=id(a.meth)
676+
assertid(a.aardvark)==id(a.aardvark)
677+
assertid(a.prop)==id(a.prop)
678+
679+
withcbook._setattr_cm(
680+
a,
681+
aardvark='moose',meth=lambda:None,prop='b'
682+
):
683+
assertid(a.meth)==id(a.meth)
684+
assertid(a.aardvark)==id(a.aardvark)
685+
asserta.aardvark=='moose'
686+
asserta.prop=='b'
687+
688+
assertid(a.meth)!=id(a.meth)
689+
assertid(a.aardvark)==id(a.aardvark)
690+
asserta.aardvark=='aardvark'
691+
assertid(a.prop)==id(a.prop)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp