@@ -638,6 +638,97 @@ class NonWarningSubclass:
638638 self .module .warn ('good warning category' , MyWarningClass )
639639 self .assertIsInstance (cm .warning , Warning )
640640
641+ def check_module_globals (self , module_globals ):
642+ with original_warnings .catch_warnings (module = self .module , record = True ) as w :
643+ self .module .filterwarnings ('default' )
644+ self .module .warn_explicit (
645+ 'eggs' , UserWarning , 'bar' , 1 ,
646+ module_globals = module_globals )
647+ self .assertEqual (len (w ), 1 )
648+ self .assertEqual (w [0 ].category , UserWarning )
649+ self .assertEqual (str (w [0 ].message ), 'eggs' )
650+
651+ def check_module_globals_error (self , module_globals , errmsg , errtype = ValueError ):
652+ if self .module is py_warnings :
653+ self .check_module_globals (module_globals )
654+ return
655+ with original_warnings .catch_warnings (module = self .module , record = True ) as w :
656+ self .module .filterwarnings ('always' )
657+ with self .assertRaisesRegex (errtype , re .escape (errmsg )):
658+ self .module .warn_explicit (
659+ 'eggs' , UserWarning , 'bar' , 1 ,
660+ module_globals = module_globals )
661+ self .assertEqual (len (w ), 0 )
662+
663+ def check_module_globals_deprecated (self , module_globals , msg ):
664+ if self .module is py_warnings :
665+ self .check_module_globals (module_globals )
666+ return
667+ with original_warnings .catch_warnings (module = self .module , record = True ) as w :
668+ self .module .filterwarnings ('always' )
669+ self .module .warn_explicit (
670+ 'eggs' , UserWarning , 'bar' , 1 ,
671+ module_globals = module_globals )
672+ self .assertEqual (len (w ), 2 )
673+ self .assertEqual (w [0 ].category , DeprecationWarning )
674+ self .assertEqual (str (w [0 ].message ), msg )
675+ self .assertEqual (w [1 ].category , UserWarning )
676+ self .assertEqual (str (w [1 ].message ), 'eggs' )
677+
678+ def test_gh86298_no_loader_and_no_spec (self ):
679+ self .check_module_globals ({'__name__' : 'bar' })
680+
681+ def test_gh86298_loader_is_none_and_no_spec (self ):
682+ self .check_module_globals ({'__name__' : 'bar' , '__loader__' : None })
683+
684+ def test_gh86298_no_loader_and_spec_is_none (self ):
685+ self .check_module_globals_error (
686+ {'__name__' : 'bar' , '__spec__' : None },
687+ 'Module globals is missing a __spec__.loader' )
688+
689+ def test_gh86298_loader_is_none_and_spec_is_none (self ):
690+ self .check_module_globals_error (
691+ {'__name__' : 'bar' , '__loader__' : None , '__spec__' : None },
692+ 'Module globals is missing a __spec__.loader' )
693+
694+ def test_gh86298_loader_is_none_and_spec_loader_is_none (self ):
695+ self .check_module_globals_error (
696+ {'__name__' : 'bar' , '__loader__' : None ,
697+ '__spec__' : types .SimpleNamespace (loader = None )},
698+ 'Module globals is missing a __spec__.loader' )
699+
700+ def test_gh86298_no_spec (self ):
701+ self .check_module_globals_deprecated (
702+ {'__name__' : 'bar' , '__loader__' : object ()},
703+ 'Module globals is missing a __spec__.loader' )
704+
705+ def test_gh86298_spec_is_none (self ):
706+ self .check_module_globals_deprecated (
707+ {'__name__' : 'bar' , '__loader__' : object (), '__spec__' : None },
708+ 'Module globals is missing a __spec__.loader' )
709+
710+ def test_gh86298_no_spec_loader (self ):
711+ self .check_module_globals_deprecated (
712+ {'__name__' : 'bar' , '__loader__' : object (),
713+ '__spec__' : types .SimpleNamespace ()},
714+ 'Module globals is missing a __spec__.loader' )
715+
716+ def test_gh86298_loader_and_spec_loader_disagree (self ):
717+ self .check_module_globals_deprecated (
718+ {'__name__' : 'bar' , '__loader__' : object (),
719+ '__spec__' : types .SimpleNamespace (loader = object ())},
720+ 'Module globals; __loader__ != __spec__.loader' )
721+
722+ def test_gh86298_no_loader_and_no_spec_loader (self ):
723+ self .check_module_globals_error (
724+ {'__name__' : 'bar' , '__spec__' : types .SimpleNamespace ()},
725+ 'Module globals is missing a __spec__.loader' , AttributeError )
726+
727+ def test_gh86298_no_loader_with_spec_loader_okay (self ):
728+ self .check_module_globals (
729+ {'__name__' : 'bar' ,
730+ '__spec__' : types .SimpleNamespace (loader = object ())})
731+
641732class CWarnTests (WarnTests , unittest .TestCase ):
642733 module = c_warnings
643734
0 commit comments