@@ -186,23 +186,25 @@ def simplefilter(action, category=Warning, lineno=0, append=False):
186186 _add_filter (action , None , category , None , lineno , append = append )
187187
188188def _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 ()
201202
202203def resetwarnings ():
203204 """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 ()
206208
207209class _OptionError (Exception ):
208210 """Exception used by option processing helpers."""
@@ -488,11 +490,12 @@ def __enter__(self):
488490 if self ._entered :
489491 raise RuntimeError ("Cannot enter %r twice" % self )
490492 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
496499 if self ._filter is not None :
497500 simplefilter (* self ._filter )
498501 if self ._record :
@@ -508,10 +511,11 @@ def __enter__(self):
508511 def __exit__ (self , * exc_info ):
509512 if not self ._entered :
510513 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
515519
516520
517521class deprecated :
@@ -701,15 +705,31 @@ def extract():
701705# If either if the compiled regexs are None, match anything.
702706try :
703707 from _warnings import (filters , _defaultaction , _onceregistry ,
704- warn , warn_explicit , _filters_mutated )
708+ warn , warn_explicit , _filters_mutated ,
709+ _acquire_lock , _release_lock ,
710+ )
705711 defaultaction = _defaultaction
706712 onceregistry = _onceregistry
707713 _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+
708725except ImportError :
709726 filters = []
710727 defaultaction = "default"
711728 onceregistry = {}
712729
730+ import _thread
731+ _lock = _thread .LockType ()
732+
713733 _filters_version = 1
714734
715735 def _filters_mutated ():
0 commit comments