2021-04-26 03:53:50 +00:00
|
|
|
import os, sys, shutil, time
|
2017-09-06 16:13:56 +00:00
|
|
|
import pyqtgraph as pg
|
2020-12-20 17:02:21 +00:00
|
|
|
import pytest
|
2018-04-26 20:22:47 +00:00
|
|
|
|
|
|
|
|
2017-09-06 16:13:56 +00:00
|
|
|
pgpath = os.path.join(os.path.dirname(pg.__file__), '..')
|
2019-06-18 18:14:51 +00:00
|
|
|
pgpath_repr = repr(pgpath)
|
2017-09-06 16:13:56 +00:00
|
|
|
|
|
|
|
code = """
|
|
|
|
import sys
|
2019-06-18 18:14:51 +00:00
|
|
|
sys.path.append({path_repr})
|
2017-09-06 16:13:56 +00:00
|
|
|
|
|
|
|
import pyqtgraph as pg
|
|
|
|
|
|
|
|
class C(pg.QtCore.QObject):
|
|
|
|
sig = pg.QtCore.Signal()
|
|
|
|
def fn(self):
|
|
|
|
print("{msg}")
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2017-09-06 16:39:26 +00:00
|
|
|
def remove_cache(mod):
|
|
|
|
if os.path.isfile(mod+'c'):
|
|
|
|
os.remove(mod+'c')
|
|
|
|
cachedir = os.path.join(os.path.dirname(mod), '__pycache__')
|
|
|
|
if os.path.isdir(cachedir):
|
|
|
|
shutil.rmtree(cachedir)
|
|
|
|
|
2020-12-20 17:02:21 +00:00
|
|
|
@pytest.mark.skipif(
|
2021-02-13 17:38:27 +00:00
|
|
|
(
|
|
|
|
(pg.Qt.QT_LIB == "PySide2" and pg.Qt.QtVersion.startswith("5.15"))
|
|
|
|
or (pg.Qt.QT_LIB == "PySide6")
|
|
|
|
) and (sys.version_info > (3, 9))
|
|
|
|
or (sys.version_info >= (3, 10)),
|
|
|
|
reason="Unknown Issue"
|
|
|
|
)
|
2021-04-26 03:53:50 +00:00
|
|
|
@pytest.mark.usefixtures("tmp_module")
|
|
|
|
def test_reload(tmp_module):
|
2017-09-06 16:13:56 +00:00
|
|
|
# write a module
|
2021-04-26 03:53:50 +00:00
|
|
|
mod = os.path.join(tmp_module, 'reload_test_mod.py')
|
2018-04-26 21:43:20 +00:00
|
|
|
print("\nRELOAD FILE:", mod)
|
2021-04-26 00:05:05 +00:00
|
|
|
with open(mod, "w") as file_:
|
|
|
|
file_.write(code.format(path_repr=pgpath_repr, msg="C.fn() Version1"))
|
2017-09-06 16:13:56 +00:00
|
|
|
|
|
|
|
# import the new module
|
2018-04-26 21:43:20 +00:00
|
|
|
import reload_test_mod
|
|
|
|
print("RELOAD MOD:", reload_test_mod.__file__)
|
2017-09-06 16:13:56 +00:00
|
|
|
|
2018-04-26 21:43:20 +00:00
|
|
|
c = reload_test_mod.C()
|
2017-09-06 16:13:56 +00:00
|
|
|
c.sig.connect(c.fn)
|
2021-04-26 00:05:05 +00:00
|
|
|
v1 = (reload_test_mod.C, reload_test_mod.C.sig, reload_test_mod.C.fn, c.sig, c.fn, c.fn.__func__)
|
2017-09-06 16:13:56 +00:00
|
|
|
|
|
|
|
# write again and reload
|
2021-04-26 00:05:05 +00:00
|
|
|
with open(mod, "w") as file_:
|
|
|
|
file_.write(code.format(path_repr=pgpath_repr, msg="C.fn() Version 2"))
|
2020-07-17 07:00:32 +00:00
|
|
|
time.sleep(1.1)
|
|
|
|
#remove_cache(mod)
|
2021-04-26 03:53:50 +00:00
|
|
|
_ = pg.reload.reloadAll(tmp_module, debug=True)
|
2021-04-26 00:05:05 +00:00
|
|
|
v2 = (reload_test_mod.C, reload_test_mod.C.sig, reload_test_mod.C.fn, c.sig, c.fn, c.fn.__func__)
|
2017-09-06 16:13:56 +00:00
|
|
|
|
|
|
|
oldcfn = pg.reload.getPreviousVersion(c.fn)
|
2017-09-06 16:39:26 +00:00
|
|
|
if oldcfn is None:
|
|
|
|
# Function did not reload; are we using pytest's assertion rewriting?
|
|
|
|
raise Exception("Function did not reload. (This can happen when using py.test"
|
2017-09-06 16:58:42 +00:00
|
|
|
" with assertion rewriting; use --assert=plain for this test.)")
|
2021-04-26 00:05:05 +00:00
|
|
|
assert oldcfn.__func__ is v1[2]
|
2018-04-26 20:22:47 +00:00
|
|
|
assert oldcfn.__self__ is c
|
2017-09-06 16:13:56 +00:00
|
|
|
|
|
|
|
# write again and reload
|
2021-04-26 00:05:05 +00:00
|
|
|
with open(mod, "w") as file_:
|
|
|
|
file_.write(code.format(path_repr=pgpath_repr, msg="C.fn() Version2"))
|
2020-07-17 07:00:32 +00:00
|
|
|
time.sleep(1.1)
|
|
|
|
# remove_cache(mod)
|
2021-04-26 03:53:50 +00:00
|
|
|
_ = pg.reload.reloadAll(tmp_module, debug=True)
|
|
|
|
_ = (reload_test_mod.C, reload_test_mod.C.sig, reload_test_mod.C.fn, c.sig, c.fn, c.fn.__func__)
|
2017-09-06 16:13:56 +00:00
|
|
|
|
|
|
|
cfn1 = pg.reload.getPreviousVersion(c.fn)
|
|
|
|
cfn2 = pg.reload.getPreviousVersion(cfn1)
|
|
|
|
|
2021-04-26 00:05:05 +00:00
|
|
|
assert cfn1.__func__ is v2[2]
|
|
|
|
assert cfn2.__func__ is v1[2]
|
2018-04-26 20:22:47 +00:00
|
|
|
assert cfn1.__self__ is c
|
|
|
|
assert cfn2.__self__ is c
|
2018-04-26 20:49:34 +00:00
|
|
|
|
|
|
|
pg.functions.disconnect(c.sig, c.fn)
|
2017-09-06 16:13:56 +00:00
|
|
|
|