@@ -186,23 +186,25 @@ def simplefilter(action, category=Warning, lineno=0, append=False):
186
186
_add_filter (action , None , category , None , lineno , append = append )
187
187
188
188
def _add_filter (* item , append ):
189
- # Remove possible duplicate filters, so new one will be placed
190
- # in correct place. If append=True and duplicate exists, do nothing.
191
- if not append :
192
- try :
193
- filters .remove (item )
194
- except ValueError :
195
- pass
196
- filters .insert (0 , item )
197
- else :
198
- if item not in filters :
199
- filters .append (item )
200
- _filters_mutated ()
189
+ with _lock :
190
+ if not append :
191
+ # Remove possible duplicate filters, so new one will be placed
192
+ # in correct place. If append=True and duplicate exists, do nothing.
193
+ try :
194
+ filters .remove (item )
195
+ except ValueError :
196
+ pass
197
+ filters .insert (0 , item )
198
+ else :
199
+ if item not in filters :
200
+ filters .append (item )
201
+ _filters_mutated ()
201
202
202
203
def resetwarnings ():
203
204
"""Clear the list of warning filters, so that no filters are active."""
204
- filters [:] = []
205
- _filters_mutated ()
205
+ with _lock :
206
+ filters [:] = []
207
+ _filters_mutated ()
206
208
207
209
class _OptionError (Exception ):
208
210
"""Exception used by option processing helpers."""
@@ -488,11 +490,12 @@ def __enter__(self):
488
490
if self ._entered :
489
491
raise RuntimeError ("Cannot enter %r twice" % self )
490
492
self ._entered = True
491
- self ._filters = self ._module .filters
492
- self ._module .filters = self ._filters [:]
493
- self ._module ._filters_mutated ()
494
- self ._showwarning = self ._module .showwarning
495
- self ._showwarnmsg_impl = self ._module ._showwarnmsg_impl
493
+ with _lock :
494
+ self ._filters = self ._module .filters
495
+ self ._module .filters = self ._filters [:]
496
+ self ._module ._filters_mutated ()
497
+ self ._showwarning = self ._module .showwarning
498
+ self ._showwarnmsg_impl = self ._module ._showwarnmsg_impl
496
499
if self ._filter is not None :
497
500
simplefilter (* self ._filter )
498
501
if self ._record :
@@ -508,10 +511,11 @@ def __enter__(self):
508
511
def __exit__ (self , * exc_info ):
509
512
if not self ._entered :
510
513
raise RuntimeError ("Cannot exit %r without entering first" % self )
511
- self ._module .filters = self ._filters
512
- self ._module ._filters_mutated ()
513
- self ._module .showwarning = self ._showwarning
514
- self ._module ._showwarnmsg_impl = self ._showwarnmsg_impl
514
+ with _lock :
515
+ self ._module .filters = self ._filters
516
+ self ._module ._filters_mutated ()
517
+ self ._module .showwarning = self ._showwarning
518
+ self ._module ._showwarnmsg_impl = self ._showwarnmsg_impl
515
519
516
520
517
521
class deprecated :
@@ -701,15 +705,31 @@ def extract():
701
705
# If either if the compiled regexs are None, match anything.
702
706
try :
703
707
from _warnings import (filters , _defaultaction , _onceregistry ,
704
- warn , warn_explicit , _filters_mutated )
708
+ warn , warn_explicit , _filters_mutated ,
709
+ _acquire_lock , _release_lock ,
710
+ )
705
711
defaultaction = _defaultaction
706
712
onceregistry = _onceregistry
707
713
_warnings_defaults = True
714
+
715
+ class _Lock :
716
+ def __enter__ (self ):
717
+ _acquire_lock ()
718
+ return self
719
+
720
+ def __exit__ (self , * args ):
721
+ _release_lock ()
722
+
723
+ _lock = _Lock ()
724
+
708
725
except ImportError :
709
726
filters = []
710
727
defaultaction = "default"
711
728
onceregistry = {}
712
729
730
+ import _thread
731
+ _lock = _thread .LockType ()
732
+
713
733
_filters_version = 1
714
734
715
735
def _filters_mutated ():
0 commit comments