1010import sys
1111import time
1212import mmap
13+ import warnings
1314import threading
1415from traceback import format_exception
1516from math import sqrt
2021import warnings
2122import pytest
2223
23- from importlib import reload
24-
2524import joblib
2625from joblib import parallel
2726from joblib import dump , load
3130from joblib .test .common import np , with_numpy
3231from joblib .test .common import with_multiprocessing
3332from joblib .testing import (parametrize , raises , check_subprocess_call ,
34- skipif , SkipTest , warns )
33+ skipif , warns )
3534
3635if mp is not None :
3736 # Loky is not available if multiprocessing is not
@@ -181,7 +180,7 @@ def test_main_thread_renamed_no_warning(backend, monkeypatch):
181180 monkeypatch .setattr (target = threading .current_thread (), name = 'name' ,
182181 value = 'some_new_name_for_the_main_thread' )
183182
184- with warns ( None ) as warninfo :
183+ with warnings . catch_warnings ( record = True ) as warninfo :
185184 results = Parallel (n_jobs = 2 , backend = backend )(
186185 delayed (square )(x ) for x in range (3 ))
187186 assert results == [0 , 1 , 4 ]
@@ -197,18 +196,21 @@ def test_main_thread_renamed_no_warning(backend, monkeypatch):
197196
198197
199198def _assert_warning_nested (backend , inner_n_jobs , expected ):
200- with warnings .catch_warnings (record = True ) as records :
199+ with warnings .catch_warnings (record = True ) as warninfo :
201200 warnings .simplefilter ("always" )
202201 parallel_func (backend = backend , inner_n_jobs = inner_n_jobs )
203202
204- messages = [w .message for w in records ]
203+ warninfo = [w .message for w in warninfo ]
205204 if expected :
206- # with threading, we might see more that one records
207- if messages :
208- return 'backed parallel loops cannot' in messages [0 ].args [0 ]
205+ # with threading, we might see more that one warninfo
206+ if warninfo :
207+ return (
208+ len (warninfo ) == 1 and
209+ 'backed parallel loops cannot' in warninfo [0 ].args [0 ]
210+ )
209211 return False
210212 else :
211- assert not messages
213+ assert not warninfo
212214 return True
213215
214216
@@ -251,11 +253,11 @@ def test_background_thread_parallelism(backend):
251253 is_run_parallel = [False ]
252254
253255 def background_thread (is_run_parallel ):
254- with warns ( None ) as records :
256+ with warnings . catch_warnings ( record = True ) as warninfo :
255257 Parallel (n_jobs = 2 )(
256258 delayed (sleep )(.1 ) for _ in range (4 ))
257- print (len (records ))
258- is_run_parallel [0 ] = len (records ) == 0
259+ print (len (warninfo ))
260+ is_run_parallel [0 ] = len (warninfo ) == 0
259261
260262 t = threading .Thread (target = background_thread , args = (is_run_parallel ,))
261263 t .start ()
@@ -1180,7 +1182,7 @@ def test_memmap_with_big_offset(tmpdir):
11801182
11811183
11821184def test_warning_about_timeout_not_supported_by_backend ():
1183- with warns ( None ) as warninfo :
1185+ with warnings . catch_warnings ( record = True ) as warninfo :
11841186 Parallel (timeout = 1 )(delayed (square )(i ) for i in range (50 ))
11851187 assert len (warninfo ) == 1
11861188 w = warninfo [0 ]
0 commit comments