2020-02-25 07:00:09 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-04-03 17:37:07 +00:00
|
|
|
"""
|
|
|
|
Test for unwanted reference cycles
|
|
|
|
|
|
|
|
"""
|
|
|
|
import pyqtgraph as pg
|
|
|
|
import numpy as np
|
2021-02-05 04:18:53 +00:00
|
|
|
import weakref
|
2021-02-12 08:09:25 +00:00
|
|
|
import warnings
|
2014-04-03 17:37:07 +00:00
|
|
|
app = pg.mkQApp()
|
|
|
|
|
2014-04-27 17:07:31 +00:00
|
|
|
def assert_alldead(refs):
|
|
|
|
for ref in refs:
|
|
|
|
assert ref() is None
|
2014-04-03 17:37:07 +00:00
|
|
|
|
2014-04-27 17:27:25 +00:00
|
|
|
def qObjectTree(root):
|
|
|
|
"""Return root and its entire tree of qobject children"""
|
|
|
|
childs = [root]
|
|
|
|
for ch in pg.QtCore.QObject.children(root):
|
|
|
|
childs += qObjectTree(ch)
|
|
|
|
return childs
|
|
|
|
|
2014-04-27 17:07:31 +00:00
|
|
|
def mkrefs(*objs):
|
2014-04-27 17:27:25 +00:00
|
|
|
"""Return a list of weakrefs to each object in *objs.
|
|
|
|
QObject instances are expanded to include all child objects.
|
|
|
|
"""
|
|
|
|
allObjs = {}
|
|
|
|
for obj in objs:
|
2021-05-28 14:39:00 +00:00
|
|
|
obj = qObjectTree(obj) if isinstance(obj, pg.QtCore.QObject) else [obj]
|
2014-04-27 17:27:25 +00:00
|
|
|
for o in obj:
|
|
|
|
allObjs[id(o)] = o
|
2020-02-26 18:06:02 +00:00
|
|
|
return [weakref.ref(obj) for obj in allObjs.values()]
|
2014-04-03 17:37:07 +00:00
|
|
|
|
2015-07-17 18:49:01 +00:00
|
|
|
|
2014-04-14 02:51:54 +00:00
|
|
|
def test_PlotWidget():
|
2014-04-27 17:07:31 +00:00
|
|
|
def mkobjs(*args, **kwds):
|
2021-02-12 08:09:25 +00:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter("ignore")
|
|
|
|
w = pg.PlotWidget(*args, **kwds)
|
2021-06-20 02:51:44 +00:00
|
|
|
data = np.array([1,5,2,4,3])
|
2014-04-27 17:07:31 +00:00
|
|
|
c = w.plot(data, name='stuff')
|
|
|
|
w.addLegend()
|
|
|
|
|
|
|
|
# test that connections do not keep objects alive
|
|
|
|
w.plotItem.vb.sigRangeChanged.connect(mkrefs)
|
|
|
|
app.focusChanged.connect(w.plotItem.vb.invertY)
|
|
|
|
|
|
|
|
# return weakrefs to a bunch of objects that should die when the scope exits.
|
|
|
|
return mkrefs(w, c, data, w.plotItem, w.plotItem.vb, w.plotItem.getMenu(), w.plotItem.getAxis('left'))
|
2014-04-14 02:51:54 +00:00
|
|
|
|
2021-05-28 14:39:00 +00:00
|
|
|
for _ in range(5):
|
2014-04-27 17:07:31 +00:00
|
|
|
assert_alldead(mkobjs())
|
2021-02-12 08:09:25 +00:00
|
|
|
|
|
|
|
def test_GraphicsWindow():
|
|
|
|
def mkobjs():
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter("ignore")
|
|
|
|
w = pg.GraphicsWindow()
|
|
|
|
p1 = w.addPlot()
|
|
|
|
v1 = w.addViewBox()
|
|
|
|
return mkrefs(w, p1, v1)
|
2014-04-14 02:51:54 +00:00
|
|
|
|
2021-05-28 14:39:00 +00:00
|
|
|
for _ in range(5):
|
2021-02-12 08:09:25 +00:00
|
|
|
assert_alldead(mkobjs())
|
|
|
|
|
2014-04-14 02:51:54 +00:00
|
|
|
def test_ImageView():
|
2014-04-27 17:07:31 +00:00
|
|
|
def mkobjs():
|
|
|
|
iv = pg.ImageView()
|
|
|
|
data = np.zeros((10,10,5))
|
|
|
|
iv.setImage(data)
|
|
|
|
|
|
|
|
return mkrefs(iv, iv.imageItem, iv.view, iv.ui.histogram, data)
|
2021-02-05 04:18:53 +00:00
|
|
|
|
2021-05-28 14:39:00 +00:00
|
|
|
for _ in range(5):
|
2014-04-27 17:07:31 +00:00
|
|
|
assert_alldead(mkobjs())
|