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

Commit64479b4

Browse files
committed
Break reference cycle Line2D <-> Line2D._lineFunc.
Upon drawing, Line2D objects would store a reference to one of their ownbound methods as their `_lineFunc` argument. This would lead to thembeing gc'ed not when going out of scope, but only when the "true" gckicks in; additionally this led to some pickle-related bugs (#3627).One can easily sidestep this problem by not storing this bound method.To check the behavior, try (py3.4+ only):```import gcimport weakreffrom matplotlib import pyplot as pltdef f(): fig, ax = plt.subplots() img = ax.imshow([[0, 1], [2, 3]]) weakref.finalize(img, print, "gc'ing image") l, = plt.plot([0, 1]) weakref.finalize(l, print, "gc'ing line") fig.canvas.draw() img.remove() l.remove()f()print("we have left the function")gc.collect()print("and cleaned up our mess")```Before the patch, the AxesImage is gc'ed when the function exits but theLine2D only upon explicit garbage collection. After the patch, both arecollected immediately.
1 parent2a7f606 commit64479b4

File tree

2 files changed

+2
-25
lines changed

2 files changed

+2
-25
lines changed

‎lib/matplotlib/lines.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,6 @@ def __init__(self, xdata, ydata,
445445

446446
self.set_data(xdata,ydata)
447447

448-
def__getstate__(self):
449-
state=super(Line2D,self).__getstate__()
450-
# _linefunc will be restored on draw time.
451-
state.pop('_lineFunc',None)
452-
returnstate
453-
454448
defcontains(self,mouseevent):
455449
"""
456450
Test whether the mouse event occurred on the line. The pick
@@ -784,7 +778,7 @@ def draw(self, renderer):
784778
iffuncname!='_draw_nothing':
785779
tpath,affine=transf_path.get_transformed_path_and_affine()
786780
iflen(tpath.vertices):
787-
self._lineFunc=getattr(self,funcname)
781+
line_func=getattr(self,funcname)
788782
gc=renderer.new_gc()
789783
self._set_gc_clip(gc)
790784

@@ -807,7 +801,7 @@ def draw(self, renderer):
807801
ifself.get_sketch_params()isnotNone:
808802
gc.set_sketch_params(*self.get_sketch_params())
809803

810-
self._draw_lines(renderer,gc,tpath,affine.frozen())
804+
line_func(renderer,gc,tpath,affine.frozen())
811805
gc.restore()
812806

813807
ifself._markerandself._markersize>0:
@@ -1250,9 +1244,6 @@ def set_dashes(self, seq):
12501244
else:
12511245
self.set_linestyle((0,seq))
12521246

1253-
def_draw_lines(self,renderer,gc,path,trans):
1254-
self._lineFunc(renderer,gc,path,trans)
1255-
12561247
def_draw_solid(self,renderer,gc,path,trans):
12571248
gc.set_linestyle('solid')
12581249
gc.set_dashes(self._dashOffset,self._dashSeq)

‎lib/matplotlib/tests/test_pickle.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -229,20 +229,6 @@ def test_image():
229229
pickle.dump(fig,BytesIO())
230230

231231

232-
@cleanup
233-
deftest_grid():
234-
frommatplotlib.backends.backend_aggimportnew_figure_manager
235-
manager=new_figure_manager(1000)
236-
fig=manager.canvas.figure
237-
ax=fig.add_subplot(1,1,1)
238-
ax.grid()
239-
# Drawing the grid triggers instance methods to be attached
240-
# to the Line2D object (_lineFunc).
241-
manager.canvas.draw()
242-
243-
pickle.dump(ax,BytesIO())
244-
245-
246232
@cleanup
247233
deftest_polar():
248234
ax=plt.subplot(111,polar=True)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp