33import logging
44import os
55import sys
6+ from contextlib import contextmanager
67from subprocess import check_call
78from subprocess import check_output
89from tempfile import TemporaryDirectory
2122from . import teardown
2223
2324
24- @pytest .fixture (autouse = True , scope = "session" )
25- def ipython_dir ():
26- with TemporaryDirectory (suffix = "dotipython" ) as td :
27- with mock .patch .dict (os .environ , {"IPYTHONDIR" : td }):
28- assert IPython .paths .get_ipython_dir () == td
29- pd = ProfileDir .create_profile_dir_by_name (td , name = "default" )
30- # configure fast heartbeats for quicker tests with small numbers of local engines
31- with open (os .path .join (pd .location , "ipcontroller_config.py" ), "w" ) as f :
32- f .write ("c.HeartMonitor.period = 200" )
25+ @contextmanager
26+ def temporary_ipython_dir ():
27+ # FIXME: cleanup has issues on Windows
28+ # this is *probably* a real bug of holding open files,
29+ # but it is preventing feedback about test failures
30+ td_obj = TemporaryDirectory (suffix = ".ipython" )
31+ td = td_obj .name
32+
33+ with mock .patch .dict (os .environ , {"IPYTHONDIR" : td }):
34+ assert IPython .paths .get_ipython_dir () == td
35+ pd = ProfileDir .create_profile_dir_by_name (td , name = "default" )
36+ # configure fast heartbeats for quicker tests with small numbers of local engines
37+ with open (os .path .join (pd .location , "ipcontroller_config.py" ), "w" ) as f :
38+ f .write ("c.HeartMonitor.period = 200" )
39+ try :
3340 yield td
41+ finally :
42+ try :
43+ td_obj .cleanup ()
44+ except Exception as e :
45+ print (f"Failed to cleanup { td } : { e } " , file = sys .stderr )
46+
47+
48+ @pytest .fixture (autouse = True , scope = "module" )
49+ def ipython_dir (request ):
50+ with temporary_ipython_dir () as ipython_dir :
51+ yield ipython_dir
3452
3553
3654def pytest_collection_modifyitems (items ):
@@ -46,7 +64,7 @@ def pytest_collection_modifyitems(items):
4664 assert not inspect .isasyncgenfunction (item .obj )
4765
4866
49- @pytest .fixture (scope = "session " )
67+ @pytest .fixture (scope = "module " )
5068def cluster (request , ipython_dir ):
5169 """Setup IPython parallel cluster"""
5270 setup ()
@@ -56,12 +74,13 @@ def cluster(request, ipython_dir):
5674 teardown ()
5775
5876
59- @pytest .fixture (scope = 'session ' )
77+ @pytest .fixture (scope = 'module ' )
6078def ipython (ipython_dir ):
6179 config = default_config ()
6280 config .TerminalInteractiveShell .simple_prompt = True
6381 shell = TerminalInteractiveShell .instance (config = config )
64- return shell
82+ yield shell
83+ TerminalInteractiveShell .clear_instance ()
6584
6685
6786@pytest .fixture ()
0 commit comments